+ 1
Animating images on html 5 canvas
How do you clear the canvas and draw the image after while it moves. https://code.sololearn.com/W3Oag2193Em3/?ref=app
6 Réponses
+ 2
you may clear the canvas using clearRect(x, y, width, height)
Or just a rectangle with the color you want as base.
The clearRect removes whatever it's under the specified area, leaving it transparent.
Animations are made by looping with a certain delay, and drawing each time with a slight variation.
for example:
setInterval(draw, 33) // each 33 milliseconds
or
function draw() {
// update
// draw stuff
requestAnimationFrame(draw)
}
+ 1
Your code is a bit complicated, I simplified it for you to understand how it works
https://code.sololearn.com/WfFNmwPxIoR8/?ref=app
0
neo.h that doesnt seem to work
0
I think it might be because I forgot to mention that the clearRect function is inside the canvas context.
canvas = document.getElementById("my_canvas");
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 50, 50);
0
neo.h look at my code whenever i clear rect the picture no longer draws
0
thanks neo.h