admin管理员组文章数量:1415664
I'm drawing an Image on the canvas using the drawImage function. This is how Im setting the src property of the image:
var img = new Image(); // Create new Image object
img.src = 'test.php?filename=myfile.jpg'
and then
oCanvas.width = 600; oCanvas.height = 400; oContext.drawImage(img, 0, 0, 600, 400);
The problem is that if the src isn't set then I get the foll error:"uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMCanvasRenderingContext2D.drawImage]" . I know I get this error coz the image hasnt finished loading. I added an alert just before the call to drawImage function to let the image finish loading and it seems to work. But it's a pain in the neck. How do I check if the image has finished loading? BTW I have to set the src property by calling a php file.
I'm drawing an Image on the canvas using the drawImage function. This is how Im setting the src property of the image:
var img = new Image(); // Create new Image object
img.src = 'test.php?filename=myfile.jpg'
and then
oCanvas.width = 600; oCanvas.height = 400; oContext.drawImage(img, 0, 0, 600, 400);
The problem is that if the src isn't set then I get the foll error:"uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIDOMCanvasRenderingContext2D.drawImage]" . I know I get this error coz the image hasnt finished loading. I added an alert just before the call to drawImage function to let the image finish loading and it seems to work. But it's a pain in the neck. How do I check if the image has finished loading? BTW I have to set the src property by calling a php file.
Share Improve this question asked May 27, 2010 at 13:59 smokingunssmokinguns 7092 gold badges10 silver badges24 bronze badges1 Answer
Reset to default 4Define this before you set the image's src
attribute.
// Create new Image object
var img = new Image();
img.onload = function() {
// add it to the canvas
oCanvas.width = 600; oCanvas.height = 400;
oContext.drawImage(img, 0, 0, 600, 400);
};
img.src = 'test.php?filename=myfile.jpg';
(it needs to be defined first because otherwise IE7 will not fire it).
Also, if you wanted to do something with that error, use JavaScript's try/catch
.
try {
oCanvas.width = 600; oCanvas.height = 400;
oContext.drawImage(img, 0, 0, 600, 400);
} catch (exception) {
alert('something went wrong!');
};
本文标签: javascriptImage Preloading in CanvasStack Overflow
版权声明:本文标题:javascript - Image Preloading in Canvas - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745161997a2645477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论