绘制 150*100 像素的矩形:
JavaScript:
</>code
- var c=document.getElementById("myCanvas");
- var ctx=c.getContext("2d");
ctx.rect(20,20,150,100);
- ctx.stroke();
Internet Explorer 9、Firefox、Opera、Chrome 以及 Safari 支持 rect() 方法。
注释:Internet Explorer 8 或更早的浏览器不支持 <canvas> 元素。
rect() 方法创建矩形。
提示:请使用 stroke() 或 fill() 方法在画布上实际地绘制矩形。
</>code
- context.rect(x,y,width,height);
参数 | 描述 |
---|---|
x | 矩形左上角的 x 坐标 |
y | 矩形左上角的 y 坐标 |
width | 矩形的宽度,以像素计 |
height | 矩形的高度,以像素计 |
通过 rect() 方法来创建三个矩形:
JavaScript:
</>code
- JavaScript:
- var c=document.getElementById("myCanvas");
- var ctx=c.getContext("2d");
- // 红色矩形
- ctx.beginPath();
- ctx.lineWidth="6";
- ctx.strokeStyle="red";
ctx.rect(5,5,290,140);
- ctx.stroke();
- // 绿色矩形
- ctx.beginPath();
- ctx.lineWidth="4";
- ctx.strokeStyle="green";
ctx.rect(30,30,50,50);
- ctx.stroke();
- // 蓝色矩形
- ctx.beginPath();
- ctx.lineWidth="10";
- ctx.strokeStyle="blue";
ctx.rect(50,50,150,80);
- ctx.stroke();
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛
Powered by 365建站网 RSS地图 HTML地图
copyright © 2013-2024 版权所有 鄂ICP备17013400号