Skip to content
Advertisement

Canvas drawImage() with image doesn’t draw anything

I want to change all images src of document to dataURL.

I am trying to draw all image in canvas through for of loop but it doesn’t work. Help me!

JavaScript

Advertisement

Answer

Your code isn’t waiting for the images to load. Add your canvas drawing code to the onload function of each image to execute it only once the image data arrives.

JavaScript
JavaScript

As an aside, height="300px"width="500px" needs spaces between properties and doesn’t need px after each value.

Use const item rather than items to avoid creating a global in your loop.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement