I suspect the reason is synchronization: if your code is in the script element above where your element with the id "canvas" defined, your first block of code will not find it in the call to document.getElementById("canvas") because it is not yet exist. While waiting for the image to load, you check it later when it exists.
If I'm right, the solution is to move the script block to the end of your body element just before your closing </body> (or somewhere after the canvas tag, really).
For example, instead of:
<script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.onload = function(){ ctx.drawImage(img,0,0); }; img.src = 'hero.png'; </script> <canvas id="canvas"></canvas> </body>
do the following:
<canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var img = new Image(); img.onload = function(){ ctx.drawImage(img,0,0); }; img.src = 'hero.png'; </script> </body>
Inserting your scripts at the bottom of the file is anyway a good idea anymore: YUI Best Practices to speed up your site
source share