0
我目前正在尝试创建一个带有雨滴下落的画布,而我唯一的问题是在画面上产生的液滴不会清除。所以基本上,这些水滴继续填满画布,直到它全部变成蓝色!滴落后我需要清除画布。清除画布
这里是我的平局和设置功能:
function draw() {
drawBackground();
for (var i=0; i< nOfDrops; i++)
{
ctx.drawImage (rainDrops[i].image, rainDrops[i].x, rainDrops[i].y); //The rain drop
rainDrops[i].y += rainDrops[i].speed; //Set the falling speed
if (rainDrops[i].y > 450) { //Repeat the raindrop when it falls out of view
rainDrops[i].y = -25 //Account for the image size
rainDrops[i].x = Math.random() * 600; //Make it appear randomly along the width
}
}
}
function setup() {
var canvas = document.getElementById('canvasRain');
if (canvas.getContext) {
ctx = canvas.getContext('2d');
imgBg = new Image();
imgBg.src = "";
setInterval(draw, 36);
for (var i = 0; i < nOfDrops; i++) {
var rainDr = new Object();
rainDr["image"] = new Image();
rainDr.image.src = "rain.png";
rainDr["x"] = Math.random() * 600;
rainDr["y"] = Math.random() * 5;
rainDr["speed"] = 3 + Math.random() * 5;
rainDrops.push(rainDr);
}
}
}
我敢肯定,我需要清除画布在我的绘制函数,但我不知道确切位置在哪里或如何。如果您需要完整的代码或者您有任何问题,请告诉我。谢谢。
可能重复[如何清除画布重绘](http://stackoverflow.com/questions/2142535/how-to-clear-the-canvas-for-redrawing) –
我找不到它MDN现在正在寻找它,但我认为'canvas.width = canvas.width;'也会重置画布。 –
是的。看到这里:http://simonsarris.com/blog/346-how-you-clear-your-canvas-matters –