How do I use draw image in Javascript
woodyloody (7)

I have been wondering how to add images to a canvas and found the drw image function. I was wondering why it would not work when i have tried using it in my code and i was wondering if someone could help me.

You are viewing a single comment. View All
heyitsmarcus (287)

Hey @woodyloody,

The reason you're not seeing the image is that you have to link the JavaScript file to your HTML page. You need to put a script tag in the bottom of your body with a src attribute to the script.js file like this:


<p>Image to use:</p>

<img id="scream" width="220" height="277"
src="s.jpeg" alt="The Scream">


<canvas id="myCanvas" width="240" height="297"
style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
<script src="script.js"></script>

And if you always leave the script at the bottom of the page, you don't need to use window.onload to start your code.

If this solves your problem, please consider upvoting and marking my answer as the accepted answer to close this question. But, I'll continue answering any questions you may have.