@muriel.schmidt
To draw behind an image in a canvas, you can use the following steps:
Here's a simple example code snippet to illustrate how to draw behind an image in canvas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); const img = new Image(); img.src = 'image.jpg'; img.onload = function() { ctx.drawImage(img, 0, 0, canvas.width, canvas.height); // Draw behind the image ctx.globalCompositeOperation = 'destination-over'; // Draw a rectangle behind the image ctx.fillStyle = 'blue'; ctx.fillRect(50, 50, 100, 100); // Reset globalCompositeOperation to default value ctx.globalCompositeOperation = 'source-over'; }; |
In this example, the image is loaded onto the canvas and then a blue rectangle is drawn behind the image using the 'destination-over'
value for globalCompositeOperation
. This allows the rectangle to appear behind the image on the canvas.