您的位置:首页 > 其它

canvas基础教程知识(1)

2018-03-07 23:34 246 查看
基本用法:使用canvas,必须设定width和height,置顶图形大小区域。不可在style中设定宽高。
在body中:

<canvas id="canvas" width="600" height="400">6</canvas>
接下来要在整块画布中绘图,就要去的上下文联系,需要调用getContext()方法。
js:
    var drawing = document.getElementById("canvas");
    var context = drawing.getContext("2d");
绘制矩形:方法fillRect(x,y,width,height)矩形的x坐标矩形的月坐标,矩形的宽高。
context.fillRect(10,10,200,200);
//给矩形填充颜色fillStyle:属性值可以是字符串、渐变对象或模糊对象;
context.fillStyle = "#ccc";
完整代码:(1)一个填充的矩形
var drawing = document.getElementById("canvas");
var context = drawing.getContext("2d");
context.fillRect(10,10,200,200);
context.fillStyle = "#ccc";
(2)描边矩形:strokeStyle:属性值可以是字符串、渐变对象或模糊对象;

var drawing = document.getElementById("canvas");
var context = drawing.getContext("2d");
context.fillRect(10,10,200,200);
context.strokeStyle = "red";
简单矩形绘制第一讲结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息