Answer the question
In order to leave comments, you need to log in
What is the CORRECT way to insert images into canvas?
const img = new Image();
img.src = 'path/img.png';
canvas.drawImage(img, x, y);
? window.onload
? img.onload
? img.onload
either the character is superimposed on top of the background (which is what I need), or the background is superimposed on top of the character ... window.onload
and trying to animate, I get an error about excessive recursion ...Answer the question
In order to leave comments, you need to log in
An image is represented by a file stored on the server and containing information about pixels in the form of a two-dimensional array of numbers. The easiest way to get this data in your program is to load it with an Image object and then pass it to the drawImage method to draw it on the canvas. If you do not wait until the data is fully loaded, then there will simply be nothing to draw. So your goal is to fully load all the images and draw them in the correct order. The easiest way to achieve this is with a Promise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question