【发布时间】:2014-12-10 00:39:09
【问题描述】:
我的画布用于绘制一些不同颜色的线条并为其设置动画,如下所示:http://jsfiddle.net/swknhg3t/
HTML:
<canvas id="myCanvas" width="400px" height="400px"></canvas>
JavaScript:
var elem = document.getElementById('myCanvas');
var c = elem.getContext('2d');
var count = 0;
function draw() {
c.save();
c.clearRect(0, 0, 400, 400);
// red
c.beginPath();
c.moveTo(20, 50);
c.lineCap = "round";
c.lineWidth = 20;
c.lineTo(380, 50);
c.strokeStyle = "#f00";
c.stroke();
// green
c.beginPath();
c.moveTo(20, 350);
c.lineCap = "round";
c.lineWidth = 20;
c.lineTo(380, 350);
c.strokeStyle = "#0f0";
c.stroke();
// blue
c.translate(200, 200);
c.rotate(count * (Math.PI / 180));
c.beginPath();
c.moveTo(0, 0);
c.lineCap = "round";
c.lineWidth = 20;
c.lineTo(180, 0);
c.strokeStyle = "#00f";
c.stroke();
c.restore();
count++;
}
setInterval(draw, 20);
当这些线相交时,我希望它们的颜色相乘(如 Photoshop 滤镜)。
我找到a nice explanation 了解如何按像素进行此操作,但由于我的画布将保存动画,我想知道是否有比遍历每个像素并在每个循环中手动计算颜色值更好的选择。
【问题讨论】:
标签: javascript html canvas colors