Javascript 如何在画布中复制动画
我画了一个半圆的动画,我基本上想复制它,然后将副本向下移动60像素,然后给新的一个添加一秒钟的延迟,这样它就可以画一个B html 剧本Javascript 如何在画布中复制动画,javascript,canvas,Javascript,Canvas,我画了一个半圆的动画,我基本上想复制它,然后将副本向下移动60像素,然后给新的一个添加一秒钟的延迟,这样它就可以画一个B html 剧本 var can = document.getElementById('thecanvas'); ctx = can.getContext('2d'); can.width = window.innerWidth; can.height = window.innerHeight; window.drawCircle = function (x, y) {
var can = document.getElementById('thecanvas');
ctx = can.getContext('2d');
can.width = window.innerWidth;
can.height = window.innerHeight;
window.drawCircle = function (x, y) {
segments = 90, /* how many segments will be drawn in the space */
currentSegment = 0,
toRadians = function (deg) {
return (Math.PI / 180) * deg;
},
getTick = function (num) {
var tick = toRadians(180) / segments; /*360=full, 180=semi, 90=quarter... */
return tick * num;
},
segment = function (end) {
end = end || getTick(currentSegment);
ctx.clearRect(0, 0, can.width, can.height);
ctx.beginPath();
ctx.arc(x, y, 60, (1.5 * Math.PI), end + (1.5 * Math.PI), false);
ctx.stroke();
ctx.closePath();
};
ctx.lineWidth = 5;
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
setTimeout(function render() {
segment(getTick(currentSegment));
currentSegment += 1;
if (currentSegment < segments) {
setTimeout(render, 5);
} else {
currentTick = 0;
}
}, 250);
};
drawCircle(100, 100);
这里有一把小提琴首先,您可以将setTimeout函数置于drawCircle方法之外
那么您至少有两个选项:
创建endDraw事件,该事件将在1次绘图结束时调度。当这个事件是handle时,只需再次调用drawCircle方法。要实现这一点,当然需要一个main方法来调用第一个drawCircle
为了获得更好的解决方案,您可以描述呼叫的工作流。ie描述要调用的方法列表,以及每个方法的开始帧:
var workflow = [{method:"drawCircle", frame:0, x:100, y:100}, //the first half circle at the frame 0, x=100, y=100
{method:"drawCircle", frame:1000, x:100, y:190}]; //the second half circle starting at frame 1000, x=100, y=190
您的主计时器将被配置为使用此阵列来知道要执行的调用
var workflow = [{method:"drawCircle", frame:0, x:100, y:100}, //the first half circle at the frame 0, x=100, y=100
{method:"drawCircle", frame:1000, x:100, y:190}]; //the second half circle starting at frame 1000, x=100, y=190