我制作了一个绘制40个矩形的循环,它们都需要高度:400px;但是我想从高度开始:0;用requestAnimationFrame
动画到400,这是我的代码:
function draw(){
height += 5;
if(height > 1){
for (var i = 0; i < dataSales.length; i++) {
geel += 6;
blauw += 6;
groen -= 6;
xPos += 23;
ctx.beginPath();
ctx.fillStyle= "#89C349";
ctx.rect(xPos,595,20,-height);
ctx.fill();
}
}
window.requestAnimationFrame(draw);
}
问题是第二次draw
被调用,xPos = 907
,第三次draw
被称为xPos = 1804
,等等。
您希望xPos
,geel
,blauw
和groen
都被定义为 draw
,以便它们每次重置。这是小提琴。