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 –