The code snippet is responsible for animating a square on a canvas element. It starts by drawing a square on the canvas and then repeatedly moves the square to the right until it reaches the end of the canvas.
The code snippet requires a canvas element with the id "canvas" to be present in the HTML document.
The code initializes the canvas and sets the stroke style and line width. It starts a new path and moves the pen to the specified coordinates. The path is rendered on the canvas. A blue square is drawn on the canvas at the specified coordinates. The code then loops through the canvas width, clearing the canvas and drawing a square at each iteration. A variable x is initialized to 0 and a setInterval function is used to repeatedly move the square to the right by updating the x value. If the square reaches the end of the canvas, the interval is cleared. Outputs The code snippet animates a square moving from left to right on a canvas.
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'black';
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(100, 50);
ctx.stroke();
ctx.fillStyle = 'blue';
ctx.fillRect(100, 100, 30, 30);
const size = 30;
ctx.fillStyle = 'blue';
for (let x = 0; x < canvas.width; x += size) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x, 50, size, size);
}
let x = 0;
const id = setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillRect(x, 50, size, size);
x += size;
if (x >= canvas.width) {
clearInterval(id);
}
}, 100);
In this example, a canvas element with the id "canvas" is created in the HTML. The JavaScript code initializes the canvas, sets the stroke style and line width, and draws a square on the canvas. It then loops through the canvas width, clearing the canvas and drawing a square at each iteration. Finally, it animates the square moving from left to right using a setInterval function.