I want to place an image in a HTML5 canvas. I found some sample code which worked, but the image was loaded 2 times when I actually only need 1. I can hide one of the images with JavaScript or CSS, but I'm looking for a way where the image only needs to load once.
<!DOCTYPE html>
<html>
<body>
<img id="img" src="img.png" width="150px" height="150px">
<canvas id="canvas" width="200px" height="200px"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("img");
ctx.drawImage(img, 0, 0);
</script>
</body>
</html>