Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div style="position: absolute;left: 600px;top: 0;"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                //创建canvas标签(节点或对象)s
var cans=document.createElement("canvas");
var cans2=document.createElement("canvas");
//设置宽高
//canvas只接收px单位值,要想canvas自适应宽高可以把canvas挂载到某一自适应节点(父节点宽高自适应)上canvas继承该节点宽高。或者直接把计算过的宽高赋值给canvas
cans.width=500;
cans.height=500;
cans.style.border="1px solid";
cans2.width=500;
cans2.height=500;
cans2.style.border="1px solid";
//挂载到body上
document.querySelector('body').appendChild(cans);
document.querySelector('div').appendChild(cans2);
//创建一个CanvasRenderingContext2D对象作为2D渲染的上下文(这一步非常重要,后面的操作都是通过ctx来完成的)(这一步非常重要,后面的操作都是通过ctx来完成的)
var ctx = cans.getContext("2d");
var ctx2 = cans2.getContext("2d");
//参考线
ctx.save();
ctx.beginPath();
ctx.moveTo(250,0);
ctx.lineTo(250,500);
ctx.moveTo(0,250);
ctx.lineTo(500,250);
ctx.stroke();
ctx.restore();


//实例5
//ctx.globalAlpha=0.2;

ctx.save();
ctx.beginPath();
ctx.globalAlpha=1;
ctx.translate(250,250);
ctx.strokeStyle="orange";
ctx.rect(0,150,150,100);
ctx.stroke();
ctx.stroke();
ctx.restore();

//实例1
ctx.save();
ctx.beginPath();
ctx.strokeStyle="blue";
ctx.rect(20,20,150,100);
ctx.stroke();
ctx.restore();

//实例2
ctx.save();
ctx.beginPath();
ctx.translate(250,250);
ctx.strokeStyle="red";
ctx.rect(0,0,150,100);
ctx.stroke();
ctx.restore();


//实例3
ctx.save();
ctx.beginPath();
ctx.strokeStyle="black";
ctx.moveTo(20,200);
ctx.lineTo(170,200)
ctx.lineTo(170,300)
ctx.lineTo(20,300)
ctx.closePath();
ctx.stroke();
ctx.restore();

ctx.save();
ctx.beginPath();
ctx.strokeStyle="black";
ctx.moveTo(20,350);
ctx.lineTo(170,350);
ctx.lineTo(170,450)
ctx.lineTo(20,450)
ctx.closePath();
ctx.stroke();
ctx.restore();

ctx.beginPath();

//实例4
ctx.save();
ctx.strokeStyle="green";
//ctx.strokeStyle="yellow";
ctx.rect(250,20,150,100);
ctx.stroke();
ctx.restore();

ctx.save();
ctx.strokeStyle="yellow";
//ctx.strokeStyle="green";
ctx.rect(250,140,150,100);
ctx.stroke();
ctx.restore();









//参考线
ctx2.save();
ctx2.beginPath();
ctx2.moveTo(250,0);
ctx2.lineTo(250,500);
ctx2.moveTo(0,250);
ctx2.lineTo(500,250);
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();


//实例1
ctx2.save();
ctx2.beginPath();
ctx2.strokeStyle="blue";
ctx2.rect(20,20,150,100);
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();

//实例2
ctx2.save();
ctx2.beginPath();
ctx2.translate(250,250);
ctx2.strokeStyle="red";
ctx2.rect(0,0,150,100);
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();


//实例3
ctx2.save();
ctx2.beginPath();
ctx2.strokeStyle="black";
ctx2.moveTo(20,200);
ctx2.lineTo(170,200)
ctx2.lineTo(170,300)
ctx2.lineTo(20,300)
ctx2.closePath();
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();

ctx2.save();
ctx2.beginPath();
ctx2.strokeStyle="black";
ctx2.moveTo(20,350);
ctx2.lineTo(170,350);
ctx2.lineTo(170,450)
ctx2.lineTo(20,450)
ctx2.closePath();
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();

//实例4
ctx2.save();
ctx2.beginPath();
ctx2.strokeStyle="green";
ctx2.rect(250,20,150,100);
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();

ctx2.save();
ctx2.beginPath();
ctx2.strokeStyle="green";
ctx2.rect(250,140,150,100);
ctx2.stroke();
//多绘制一次
ctx2.stroke();
ctx2.restore();

              
            
!
999px

Console