我正在尝试将图片加载到 HTML5 Canvas 中。 当我使用 URL 加载图像时,一切正常,但如果我将图像放在本地驱动器上并指向它,则没有任何反应。
注意:当我使用常规标签时,一切正常并且图像已加载。
代码如下:
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">
谢谢。
最佳答案
尝试这样的事情。
<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';
关于javascript - html5 图像未加载到 Canvas 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10684290/