admin管理员组文章数量:1326282
I am finding that a certain code fails in Internet Explorer and Edge, but seems to work flawlessly in Chrome and Firefox:
var image = document.getElementById("myImage");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.drawImage(image, 1, 0, 499, 278, 0, 0, 749, 417);
img {
width: 300px;
height: 200px;
}
<img id="myImage" src=".png" />
<canvas id="myCanvas" width="600" height="400"></canvas>
I am finding that a certain code fails in Internet Explorer and Edge, but seems to work flawlessly in Chrome and Firefox:
var image = document.getElementById("myImage");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.drawImage(image, 1, 0, 499, 278, 0, 0, 749, 417);
img {
width: 300px;
height: 200px;
}
<img id="myImage" src="https://image.ibb.co/bsANfm/emptyphoto.png" />
<canvas id="myCanvas" width="600" height="400"></canvas>
For some reason, the CanvasRenderingContext2D.drawImage() call fails in IE 11 and Edge 40.15063.674.0, where I'm getting an IndexSizeError exception. According to MDN, I should only be getting this error if the size of either the canvas or the source rectangle is 0, which is not the case here.
Could anyone shed some light into whether I'm doing something wrong here, or if there's some workaround available?
Share Improve this question edited Oct 26, 2017 at 16:44 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Oct 24, 2017 at 15:13 unpollitounpollito 1,0192 gold badges13 silver badges32 bronze badges2 Answers
Reset to default 6I've found the culprit. Essentially, I'd been asking the browser to paint a section of the image from y=0 to y=278, but the image is only 275px high. IE and Edge seem not to like this and decide to throw an error. If I change the 278 in the snippet above to 275, it works:
var image = document.getElementById("myImage");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.drawImage(image, 1, 0, 499, 275, 0, 0, 749, 417);
img {
width: 300px;
height: 200px;
}
<img id="myImage" src="https://image.ibb.co/bsANfm/emptyphoto.png" />
<canvas id="myCanvas" width="600" height="400"></canvas>
I've just hit the same issue, but with a slightly different cause. I'm using DrawImage with a transform to change the shape of it. Source image is 1240x1754.
canvas_context.transform( -0.035, 0.07, 0, 1, 600 * 1.035, 30 + 600 * 0.07 );
canvas_context.drawImage( source_image, 0, 0, 1240, 1754, 0, 0, 600, 849 );
This doesn't work in IE, it once again fails with the IndexSizeError exception.
It appears that the problem is caused by the browser going slightly over the edge of the source image as it tries to transform the image, even though the source area specified is the dimensions of the source image. Coming in 1 pixel from the edge in all directions changes the last line to:
canvas_context.drawImage( source_image, 1, 1, 1238, 1752, 0, 0, 600, 849 );
which solves the problem.
本文标签: javascriptIndexSizeError on drawImage on IE and EdgeStack Overflow
版权声明:本文标题:javascript - IndexSizeError on drawImage on IE and Edge - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742211130a2433732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论