简体   繁体   English

未捕获的错误:INDEX_SIZE_ERR

[英]Uncaught Error: INDEX_SIZE_ERR

I am drawing on a canvas with the following line: 我正在使用以下行绘制画布:

ctx.drawImage(compositeImage, 0, 0, image.width, image.height, i, j, scaledCompositeImageWidth, scaledCompositeImageHeight); ctx.drawImage(compositeImage,0,0,image.width,image.height,i,j,scaledCompositeImageWidth,scaledCompositeImageHeight);

This code has executed error free on Safari, Chrome, Firefox (and even IE using google's excanvas library). 此代码在Safari,Chrome,Firefox(甚至是使用谷歌的excanvas库的IE)上执行了错误。 However, a recent update to Chrome now throws the following error: 但是,最近对Chrome的更新现在会引发以下错误:

Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1 未捕获错误:INDEX_SIZE_ERR:DOM异常1

This code often positions part or all of the drawn image OFF the canvas, anyone got any idea what's going on here? 这段代码经常将部分或全部绘制的图像放在画布上,任何人都知道这里发生了什么?

Is compositeImage pointing at a valid (fully loaded) image? compositeImage是否指向有效(完全加载)的图像?

I've seen this exception happen if you try to draw the image before it has loaded. 我已经看到如果您尝试在加载之前绘制图像,则会发生此异常。

Eg 例如

img = new Image();
img.src = '/some/image.png';
ctx.drawImage( img, ..... ); // Throws error

Should be something like 应该是这样的

img = new Image();
img.onload = function() {
  ctx.drawImage( img, .... );
};
img.src = '/some/image.png';

To ensure the image has loaded. 确保图像已加载。

Why? 为什么?

It happens if you draw the image before it is loaded because that is what the html5 2dcontext draft specifies for interpreting the first argument of drawImage() : 如果您在加载图像之前绘制图像会发生这种情况,因为这是html5 2dcontext草稿指定用于解释drawImage()的第一个参数的内容:

If one of the sw or sh arguments is zero, the implementation must raise an INDEX_SIZE_ERR exception. 如果其中一个sw或sh参数为零,则实现必须引发INDEX_SIZE_ERR异常。

sw, sh being source-image width and height sw,sh是源图像的宽度和高度

@jimr's answer is good, just thought it would be relevant to add this here. @ jimr的答案很好,只是认为在这里添加它是相关的。

Another cause for this error is, the dimensions of the source image are too large; 造成此错误的另一个原因是源图像的尺寸太大; they extend beyond the actual image dimensions. 它们超出了实际的图像尺寸。 When this occurs, you'll see this error in IE, but not in Chrome or Firefox. 发生这种情况时,您会在IE中看到此错误,但不会在Chrome或Firefox中看到此错误。

Say you have a 150x150 image: 假设您有一张150x150的图片:

var imageElement = ...;

Then if you try to draw it using larger source dimensions: 然后,如果您尝试使用更大的源尺寸绘制它:

var sourceX = 0;
var sourceY = 0;
var sourceWidth = 200; // Too big!
var sourceHeight = 200; // Too big!
canvasContext.drawImage(imageElement, 
   sourceX, sourceY, sourceWidth, sourceHeight, // Too big! Will throw INDEX_SIZE_ERR
   destX, destY, destWidth, destHeight);

This will throw INDEX_SIZE_ERR on IE. 这将在IE上抛出INDEX_SIZE_ERR。 (The latest IE being IE11 at the time of this writing.) (在撰写本文时,最新的IE浏览器是IE11。)

The fix is simply constraining the source dimensions: 该修复只是限制源维度:

var sourceWidth = Math.min(200, imageElement.naturalWidth);
var sourceHeight = Math.min(200, imageElement.naturalHeight);

malloc4k's answer alluded to this, however, he suggested it was because image.width was zero. 然而,malloc4k的答案提到了这一点,他认为这是因为image.width为零。 I added this new answer because image width being zero is not necessarily the cause of the error on IE. 我添加了这个新的答案,因为图像宽度为零不一定是IE上错误的原因。

Due to my experience, INDEX_SIZE_ERR in IE (even version 10) is likely to happen when you are operating on canvas that has been manipulated with drawImage() with wrong clip size. 根据我的经验,当您在使用带有错误剪辑大小的drawImage()操作的画布上操作时,IE(甚至版本10)中的INDEX_SIZE_ERR可能会发生。

And if you loaded your image dynamically, then it is likely that values of image.width and image.height are ==0 , so you are invoking 如果你动态加载的图像,那么很可能的值image.widthimage.height==0 ,所以要调用

ctx.drawImage(compositeImage, 0, 0, 0, 0, ...

In my case the solution (that worked) was to use image.naturalWidth and image.naturalHeight instead. 在我的情况下,解决方案(有效)是使用image.naturalWidthimage.naturalHeight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM