canvas教程

Clearing canvas

字号+ 作者:H5之家 来源:H5之家 2017-02-22 16:11 我要评论( )

javascript,canvas, Clearing canvas

I'm currently trying to create a canvas with raindrops falling, and my only problem is that the drops aren't clearing after they spawn on the screen. So basically, the drops continue to fill up the canvas until all of it is blue! I need to clear the canvas after the drops have fallen.

Here are my draw and setup functions:

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); } } }

I'm pretty sure I'll need to clear the canvas in my draw function, but I'm not sure where exactly or how. Let me know if you need the full code or if you have any questions. Thanks.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 动漫之家漫画网

    动漫之家漫画网

    2017-02-22 18:00

  • canvas绘制的直线动画

    canvas绘制的直线动画

    2017-02-21 18:00

  • Javascript与HTML5的canvas实现图片旋转效果

    Javascript与HTML5的canvas实现图片旋转效果

    2017-02-21 15:01

  • HTML5 canvas实现流体力学

    HTML5 canvas实现流体力学

    2017-02-21 14:04

网友点评