刚从画布和 javascript 开始,我无法理解为什么这个片段中的 setTimeout 不起作用。我最初认为它会触发每一帧,因为它包含在循环中。我也尝试在动画功能中移动它,但无济于事。
$(document).ready(function(){
var canvas = $('#myCanvas');
var context = canvas.get(0).getContext('2d');
var Shape = function(x1,y1,x2,y2){
this.x1 = x1
this.y1 = y1
this.x2 = x2
this.y2 = y2
}
var shapes = new Array();
shapes.push(new Shape(0,0,50,50));
shapes.push(new Shape(50,50,50,50));
shapes.push(new Shape(0,100,50,50));
shapes.push(new Shape(50,150,50,50));
shapes.push(new Shape(0,200,50,50));
function animate(){
for (i=0;i<shapes.length;i++){
context.clearRect(0,0,500,500);
context.fillRect(shapes[i].x1,shapes[i].y1,shapes[i].x2,shapes[i].y2);
setTimeout(animate, 500);
};
};
animate();
});