You cannot operate on images until they are fully loaded, and browsers typically load images asynchronously while the page script executes. If you try to render an image to the canvas before it is fully loaded, no image will be displayed.

var img= new Image(); img.src = "bark.jpg"; Img. onload = function () {drawCanvas(); }Copy the code