码迷,mamicode.com
首页 > Web开发 > 详细

JS -- canvas

时间:2015-08-10 10:33:52      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

JS – canvas

JS – canvas

<canvas id="canva" weight=".." height="..." style="border: 1px solid #f00"></canvas>

var canva  = document.getElementById("canva");
var cxt    = canva.getContext(‘2d‘);
var width  = cxt.canvas.width;
var height = cxt.canvas.height;

var width  = canva.width;
var height = canva.height;

cxt.lineWidth = 2;      线的宽度为 2

画线

cxt.moveTo(20, 20);
cxt.lineTo(90, 90);
cxt.stroke();

三角形

cxt.beginPath();
cxt.moveTo(30, 30);
cxt.lineTo(0,  80);
cxt.lineTo(80, 80);
cxt.closePath();
cxt.fillStyle = "#f00";
cxt.fill();
// cxt.stroke();

矩阵 (左上角, 宽, 高)

cxt.fillStyle = "#f00";
cxt.fillRect(0, 0, 30, 80);
// cxt.strokeRect(0, 0, 30, 80);

圆形 arc(圆心, 半径, 开始的角度, 结束的角度, 是否顺时针)

cxt.beginPath();
cxt.arc(50, 50, 30, 0, 30, false);
cxt.closePath();
cxt.stroke();
cxt.fill();

从图片中画图

var img = document.getElementById("img");
ctx.drawImage(img, X, Y, Width, Height);

新建一张图片

var img1 = new Image();
img1.src = "1.png";
var x = boom.x;
var y = boom.y;
img1.onload = function() {
    cxt.drawImage(img1, x, y, 30, 30);
}

写字

var str = "你好";
cxt.fillStyle = "#f00";
cxt.font = "50px Monoca";
cxt.fillText(str, 0, 50);
cxt.strokeText(str, 0, 50);

刷新

cxt.clearRect(0, 0, width, height);

JS -- canvas

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4717227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!