admin管理员组文章数量:1415111
The idea of my code is create a hidden div which loads the image. When it's load event is fired draw it in the canvas. When I run the code I get this error 0x80040111 (NS_ERROR_NOT_AVAILABLE), yet I am waiting for the load event. Here is my code.
HTML
<div id="old-counties-image-wrapper" style="display: none;">
<img border="0" height="390" id="interreg-iiia-old-counties-map" src="/f/MISCELLANEOUS/old-map.jpg" /></div>
<p>
<canvas id="old-counties-image-canvas"></canvas></p>
and javascript
$('#interreg-iiia-old-counties-map').load(function() {
var canvas=document.getElementById('old-counties-image-canvas');
if (canvas.getContext) {
var ctx=canvas.getContext('2d');
var img=$('#interreg-iiia-old-counties-map');
ctx.drawImage(img, 0, 0);
}
//else {
// $('#old-counties-image-wrapper').show();
//}
});
The else part is mented out for now but is there for browsers that don't support canvas.
The idea of my code is create a hidden div which loads the image. When it's load event is fired draw it in the canvas. When I run the code I get this error 0x80040111 (NS_ERROR_NOT_AVAILABLE), yet I am waiting for the load event. Here is my code.
HTML
<div id="old-counties-image-wrapper" style="display: none;">
<img border="0" height="390" id="interreg-iiia-old-counties-map" src="/f/MISCELLANEOUS/old-map.jpg" /></div>
<p>
<canvas id="old-counties-image-canvas"></canvas></p>
and javascript
$('#interreg-iiia-old-counties-map').load(function() {
var canvas=document.getElementById('old-counties-image-canvas');
if (canvas.getContext) {
var ctx=canvas.getContext('2d');
var img=$('#interreg-iiia-old-counties-map');
ctx.drawImage(img, 0, 0);
}
//else {
// $('#old-counties-image-wrapper').show();
//}
});
The else part is mented out for now but is there for browsers that don't support canvas.
Share Improve this question edited Jan 20, 2011 at 1:52 Yi Jiang 50.2k16 gold badges139 silver badges136 bronze badges asked Jan 19, 2011 at 10:43 BelindaBelinda 1,2312 gold badges14 silver badges25 bronze badges2 Answers
Reset to default 4Because $('#interreg-iiia-old-counties-map')
returns a jQuery object, while the drawImage
method takes an Image
object - the jQuery ($
) function returns a jQuery object that wraps the original element to provide the usual jQuery functions you see.
You can get the underlying Image
object by using the get
method, but in this case it would be easier to just use this
, which in the context of the callback function supplied to the load
function, is the original $('#interreg-iiia-old-counties-map')
DOM element. In other words,
ctx.drawImage(this, 0, 0);
should work fine here. You also don't have to use a hidden <img>
element - with new Image
you can retrieve the image similar to what you're doing here:
var img = new Image(),
canvas = document.getElementById('old-counties-image-canvas');
img.src = '/f/MISCELLANEOUS/old-map.jpg';
img.onload = function(){
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
}
};
And, now for my own take on a solution:
var imgId = $("#myDiv img").attr("id");
var imgObj = document.getElementById(imgId);
var canvasContext = $("#imgCanvas")[0].getContext('2d');
canvasContext.drawImage(imgObj, 0, 0);
It's ugly, but I don't think it's much uglier than the solutions presented above. There may be performance advantages to other solutions as well, though this one seems to avoid needing to load the image file from the server's file system, which should be worth something. I tested it in Chrome, Firefox, and IE10.
本文标签: javascriptHTMLCanvasContextdrawImage() failing when passed an image found using jQueryStack Overflow
版权声明:本文标题:javascript - HTMLCanvasContext.drawImage() failing when passed an image found using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200856a2647361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论