How to draw an existing <img> on canvas
I know I can do this with javascript:
var ctx = getCanvas('testCanvas').getContext('2d'); // get Canvas context var img = new Image(); img.src = 'test.png'; img.onload = function(){ ctx.drawImage(img, 200, 200); // x, y, width, height } But how to draw an existing tag on the canvas:
In html:
<img src='test.png'> Why do I want to do this? I want to optimize image loading with pagepeed
+4
5 answers
You can transfer the src image to your new image
var ctx = getCanvas('testCanvas').getContext('2d'); // get Canvas context var img = new Image(); img.src = document.getElementById('testImage').src; img.onload = function(){ ctx.drawImage(img, 200, 200); // x, y, width, height } add id to image element
<img src='test.png' id="testImage"> +1