我正在使用Javascript編寫HTML5 Canvas動畫的代碼。我正在使用requestAnimFrame
。動畫對於某個點來說工作正常。但是當我在使用requestAnimFrame
或setTimeout
的函數中添加循環(for或while)時,動畫不起作用。對我來說,添加循環很重要。任何建議,使這成爲可能?HTML5 Canvas動畫中的循環
function animate(lastTime) {
var date = new Date();
var time = date.getTime();
var timeDiff = time - lastTime;
var linearSpeed = 100;
var linearDistEachFrame = linearSpeed * timeDiff/1000;
var currentX = LINE.x;
var currentY = LINE.y;
var newY = currentY + linearDistEachFrame;
var newX = currentX + linearDistEachFrame;
context.beginPath();
context.moveTo(LINE.x, LINE.y);
lastTime = time;
var Xindex=LINE.arrayX.indexOf(newX);
//here am getting error..if i replace this with 'if' its working fine..and even if there is not a single LOC it doesnt work
while(Xindex!=-1) {
//processes the loop
}
context.lineTo(LINE.x, LINE.y);
context.fillStyle = "red";
context.fill();
context.lineWidth = LINE.borderWidth;
context.strokeStyle = "red";
context.stroke();
// request new frame
requestAnimFrame(function() {
animate(lastTime);
});
}
代碼在哪裏?我們應該猜測問題出在哪裏? – mpm 2012-04-16 11:32:06
一些代碼請... ...什麼工作,什麼都沒有......你如何構建這件事可能是 – tipycalFlow 2012-04-16 11:32:06
添加代碼的原因..如果我用'如果'它替換'while'它工作..and即使循環中沒有代碼,它也不起作用... – thecodejack 2012-04-16 11:40:52