variable not working inside function
<p>why this doesnt work?</p>

<pre><code>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';
</code></pre>

<p>but this does?</p>

<pre><code>var img = new Image();
img.onload = function(){
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.drawImage(img,0,0);
};
img.src = 'hero.png';
</code></pre>

<p>how to make that <strong>ctx variable</strong> global so i could use in all functions? btw in all the tutorials everyone is using first method...</p>

<p><strong>Got it working!</strong></p>
