【问题标题】:INDEX_SIZE_ERR when drawImage on canvasINDEX_SIZE_ERR 在画布上绘制图像时
【发布时间】:2014-02-05 13:10:59
【问题描述】:

我需要在画布上绘制一个 Image 对象,但我在 Firefox 和 IE10 中有一个 INDEX_SIZE_ERR 异常,但在 Chrome 和 Safari 中没有...

根据W3CIf 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


【解决方案1】:

您正在手动设置图像的宽度和高度 (img.width = width; img.height = height;)。我真的不明白你为什么要这样做,但这可能是不必要的。

这些应根据您加载的图像数据自动计算。尝试删除它们以查看数据的实际大小。

【讨论】:

  • 谢谢,您的建议解决了我在 Firefox 上的问题,但在 IE 上却没有。但是,控制台什么也没打印。看起来该函数没有被调用。问题可能不同。
【解决方案2】:

图像widthheight只读 属性,因此它们会导致代码在某些浏览器中中断。

如果您绝对想在加载图像之前设置宽度和高度,您可以这样做:

var img = new Image(width, height);

然后您可以读取图片加载后的img.widthimg.height(或读取img.naturalWidthimg.naturalHeight 以获取原始尺寸)。

虽然没有必要这样做。只需像这样拨打您的drawImage()

ctxCapture.drawImage(img, 0, 0, canvasGallery.width, canvasGallery.height);

这将使用图像的完整尺寸并将其缩放到canvasGallery 的尺寸。

提示:如果您使用此函数加载多个图像,您需要在 onload 处理程序中将 imgthis 交换。

修改代码:

function writePhotoOnCanvas(data, width, height) {

    var canvasGallery = document.getElementById("canvasGallery");
    var ctxCapture = canvasGallery.getContext("2d");

    /// setting width to clear does not work in all browser, to be sure:
    ctxCapture.clearRect(0, 0, canvasGallery.width, canvasGallery.height);

    var img = new Image();
    img.onload = function() {
        //  Draw the picture
        try {
            ctxCapture.drawImage(this, 0, 0,
                                     canvasGallery.width, canvasGallery.height);
        } catch(e) {
            console.error(e.message);
        }
    };

    // Set image content from specified photo
    img.src = data;
}

【讨论】:

  • 感谢您的回答。 clearRect 到底是做什么的?默认情况下,我需要画布背景为黑色。根据我在网上看到的,它只是画了一个白色的矩形。
  • @Maxbester clearRect 将画布清除为已定义矩形的初始状态,即透明黑色像素。要使用黑色清除,只需使用ctx.fillStyle = '#000'; ctx.fillRect(x, y, w, h);
猜你喜欢
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 2013-07-20
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多