我做了一個循環功能,以緩慢淡出圖像。HTML5/Javascript超時未完成功能
var ctx; // this is the canvas already defined
function nextPic(i)
{
trans = 1; //resets transparent var to 1
fadeloop('out'); //starts loop
ctx.clearRect(0, 0, canvas.width, canvas.height); //clears canvas, this doesn't work
alert("done"); //this doesn't work either
}
function fadeloop(f){ //loop that fades
if(f == 'out'){ //make sure it wants to shade out
setTimeout(function() { //timed delay for fading out
fadeImgOut(); //calls function to fade out by .01
if(trans>0.0){ //if faded out break out of loop
fadeloop('out'); //if not restart function
}
},10);
}
}
function fadeImgOut(){ //function that fades out
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img,0,0);
ctx.globalAlpha = trans;
trans = trans - .01;
}
一個按鈕調用nextpic。它會慢慢透明的圖像,然後淡入另一個。現在它將圖像淡出直到trans = .01。不會繼續進行警報(「完成」)或畫布重置。任何想法我一遍又一遍地看着它,不知道該怎麼做。
你在網上得到一個錯誤檢查?如果警報(「完成」)未觸發,我猜測腳本會因爲任何原因而崩潰。 – 2012-02-13 03:00:49
你在哪裏定義canvas.width&canvas.height? – 2012-02-13 03:02:07
另外,你現在在哪裏聲明'trans',看起來每個函數都有一個'trans'的本地聲明,但是我沒有看到任何函數以外的任何聲明,就像你爲ctx所做的那樣。 – 2012-02-13 03:03:12