当image.src不可用时处理canvas.drawImage错误

时间:2013-04-09 05:50:10

标签: html5 html5-canvas

当Image的src url不可用时,在canvas上下文中处理调用drawImage的有效方法是什么?例如:

myImage.src = "ImageDoesNotExist";
canvasCtx.drawImage(myImage,0,0); // 0x80004005 - JavaScript runtime error: Unspecified error.

谢谢!

1 个答案:

答案 0 :(得分:2)

除了image.onload之外,这些场合还有image.onerror

var img = new Image();
img.onload = function(){ 
    context.drawImage(img,0,0);
}; 
img.onerror = function(){ 
    alert("Oops, bad stuff happened while loading this image.");
};
img.src = "ImageDoesNotExist";