admin管理员组文章数量:1391989
I am trying to load a picture into HTML5 Canvas. when i use a URL to load the image everything works fine, but if i put the image on the local drive and point to it, nothing happens.
note: when i use a regular tag, everything works fine and the image is loaded.
here is the code:
var canvas = document.getElementById("rightSide"); var context = canvas.getContext("2d"); var imageObj = new Image(); imageObj.src = "cloud.gif"; context.drawImage(imageObj, 650, 55, 93, 104);
< canvas id="rightSide" width="800px" height="800">
thanks.
I am trying to load a picture into HTML5 Canvas. when i use a URL to load the image everything works fine, but if i put the image on the local drive and point to it, nothing happens.
note: when i use a regular tag, everything works fine and the image is loaded.
here is the code:
var canvas = document.getElementById("rightSide"); var context = canvas.getContext("2d"); var imageObj = new Image(); imageObj.src = "cloud.gif"; context.drawImage(imageObj, 650, 55, 93, 104);
< canvas id="rightSide" width="800px" height="800">
thanks.
Share asked May 21, 2012 at 11:23 GleebGleeb 11.3k28 gold badges96 silver badges139 bronze badges 1- When is the code being executed? In a script tag, or onload, or what? – Phil H Commented May 21, 2012 at 11:34
2 Answers
Reset to default 9Try something like this.
<canvas id="canvas"></canvas>
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = new Image();
img.onload = function(){
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
img.src = 'image.jpg';
A local file being loaded into canvas is treated as being from a different source and therefore "tainted". This is why it's not working for your local file, but does for a URL.
本文标签: javascripthtml5 image is not loaded into canvasStack Overflow
版权声明:本文标题:javascript - html5 image is not loaded into canvas - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650410a2617649.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论