Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  169] [ 7]  / answers: 1 / hits: 16072  / 12 Years ago, tue, may 15, 2012, 12:00:00

Im sure this has got to be something simple that I am overlooking, but I can't seem to get my canvas to display an jpg stored on the server.



<img id=test_img alt=test src=/media/tmp/a4c1117e-c310-4b39-98cc-43e1feb64216.jpg/>
<canvas id=user_photo style=position:relative;></canvas>
<script type=text/javascript>
var image = new Image();
image.src = /media/tmp/a4c1117e-c310-4b39-98cc-43e1feb64216.jpg;
var pic = document.getElementById(user_photo);
pic.getContext('2d').drawImage(image, 0, 0);
</script>


the <img> displays as expected, however the canvas is blank, though it does seem to have the correct dimensions. Any one see what I'm doing wrong?



My tired eyes will appreciate any help.



Thanks


More From » html

 Answers
7

you may want to use the following approach



image.onload = function() {
pic.getContext('2d').drawImage(image, 0,0);
}


so you ensure that the image is loaded when trying to draw it.


[#85578] Monday, May 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinarnoldp

Total Points: 10
Total Questions: 122
Total Answers: 109

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;