ctrlcv-dev.comgithub

Canvascheatsheet

贡献者:lele88lala,BAI

指令集

获取 context

JS
var canvas = document.getElementById("c");
var c = canvas.getContext("2d");

基础绘制

JS
// x = 10, y = 20, width = 200, height = 100
c.fillStyle = "#ff0000";
c.strokeStyle = "#ff00ff";
JS
c.lineWidth = 5;
c.lineCap = "round";
JS
c.fillRect(10, 20, 200, 100);
JS
c.stroke();
c.fill();

保存与还原

JS
c.save();
JS
c.restore();

动画

JS
onframe: function() {
c.clearRect(0, 0, w, h)
}

转换

JS
c.translate(0, 0);
c.rotate((Math.PI * 2) / 5);
c.scale(1.0, 1.0);

原点旋转:

JS
c.translate(ox, oy);
c.rotate(theta);
c.translate(-ox, -oy);

原点缩放:

JS
c.translate(-ox * x, -oy * y);
c.scale(x, y);
c.translate(ox / x, oy / y);

详见 MDN: Transformations.

绘制图像

JS
c.drawImage(image, dx, dy, [dw, dh]);
/* `image` can be HTML Image/Canvas/Video */

详见 MDN: Images

颜色,、样式、阴影

JS
c.strokeStyle = "#ff00ff";
c.fillStyle = "#ff00ff";
JS
c.shadowOffsetX = 0;
c.shadowOffsetY = 0;
c.shadowOffsetBlur = 3.0;
c.shadowColor = "rgba(0,0,0,0.2)";

详见 MDN: Styles

渐变

JS
gr = c.createLinearGradient(x0, y0, x1, y1);
gr = c.createRadialGradient(x0, y0, r0, x1, y1, r1);
pat = c.createPattern(image, "repeat-x");
JS
c.fillStyle = gr;

绘图

JS
c.beginPath()
c.moveTo(x,y)
c.lineTo(x,y)
c.quadraticCurveTo(cpx,cpy,x,y)
c.bezierCurveTo(cp1x,cp1y,cp2x,cp2y)
c.arcTo(...)
c.arc(...)
c.closePath()

参考