我做了一个循环功能,以缓慢淡出图像。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