write the code for an html5 canvas spanning the full size of the window, that has 5…
- User
write the code for an html5 canvas spanning the full size of the window, that has 5 layers of parallax scrolling backgrounds and a 300px animated image in the center
- Assistant
canvas.width = window.innerWidth; canvas.height = window.innerHeight;
const bgImages = [ 'path/to/your/background-1.png', 'path/to/your/background-2.png', 'path/to/your/background-3.png', 'path/to/your/background-4.png', 'path/to/your/background-5.png' ];
const animatedImg = 'path/to/your/animated-image.png';
const layers = bgImages.map((src) => { const img = new Image(); img.src = src; return img; });
const animatedImage = new Image(300, 300); animatedImage.src = animatedImg;
function drawBackgrounds() { layers.forEach((img, index) => { const speed = 0.1 * (index + 1); const x = (canvas.width - img.width) / 2 - (speed * window.scrollY); ctx.drawImage(img, x, 0, img.width, img.height); }); }