【发布时间】:2014-02-05 13:10:59
【问题描述】:
我需要在画布上绘制一个 Image 对象,但我在 Firefox 和 IE10 中有一个 INDEX_SIZE_ERR 异常,但在 Chrome 和 Safari 中没有...
根据W3C:If one of the sw or sh arguments is zero, the implementation must raise an INDEX_SIZE_ERR exception.。
这是导致问题的代码:
function writePhotoOnCanvas(data, width, height) {
// Get the canvas
var canvasGallery = document.getElementById("canvasGallery");
// Clear the canvas
canvasGallery.width = canvasGallery.width;
// Get its context
var ctxCapture = canvasGallery.getContext("2d");
// Create an image in order to draw in the canvas
var img = new Image();
// Set image width
img.width = width;
// Set image height
img.height = height;
// To do when the image is loaded
img.onload = function() {
console.log("img.width="+img.width+", img.height="+img.height);
console.log("width="+width+", height="+height+", canvasGallery.width="+canvasGallery.width+", canvasGallery.height="+canvasGallery.height);
// Draw the picture
try {
ctxCapture.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvasGallery.width, canvasGallery.height);
} catch(e) {
console.error(e.message);
}
};
// Set image content from specified photo
img.src = data;
}
控制台显示:
img.width=640, img.height=480
width=640, height=480, canvasGallery.width=589, canvasGallery.height=440
Index or size is negative or greater than the allowed amount
问题的根源是什么?
谢谢
【问题讨论】:
-
我也遇到了同样的错误,你可以试试下面的。尝试在您发现错误的地方进行递归循环。像 console.error(e.message); setTimeout(函数(){ writePhotoOnCanvas(数据,宽度,高度)},10);返回;
标签: javascript exception canvas html5-canvas drawimage