0

I am not sure how to put an animation and a shape in Web?

The first shape is okay, but when I add the second, it doesn't play the first.

13th Sep 2022, 11:37 AM
Paxton Programer
3 Respostas
+ 3
Do you have a code to share? Maybe someone can see the problem that way.
13th Sep 2022, 12:14 PM
Ausgrindtube
Ausgrindtube - avatar
0
I do this, window.onload = function() { var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); context.rect(50, 50, 200, 150); context.stroke(); }; And it works, but when I add this, window.onload = function() {     var canvas = document.getElementById("canvas");     var context = canvas.getContext("2d");     var x = 10;     var y = 5;         function draw() {         context.clearRect(0, 0, 600, 400);                 context.beginPath();         context.rect(x, y, 40, 20);         context.fillStyle="brown";         context.fill();         x += 10;         if (x >= 600) {             x = -100;         }     }     setInterval(draw, 50); } The rectangle from the first part disappears
14th Sep 2022, 3:42 PM
Paxton Programer
0
Yep, you clear your entire canvas. You can add your context.rect(50, 50...) part to the second code after the clearRect line and it'll be there.
14th Sep 2022, 4:15 PM
Ausgrindtube
Ausgrindtube - avatar