admin管理员组文章数量:1388844
I am new to HTML5 and the canvas element and in the last few weeks I have been trying to understand it better. Anyways I am getting a Uncaught TypeError: Type error
when I am trying to change the red square from this code / to this image .png. But when I try to I just always get that Uncaught TypeError: Type error
. Here is the code I have currently /.
If you need me to explain something better just ask.
I am new to HTML5 and the canvas element and in the last few weeks I have been trying to understand it better. Anyways I am getting a Uncaught TypeError: Type error
when I am trying to change the red square from this code http://jsfiddle/kmHZt/10/ to this image https://i.sstatic/dmLmn.png. But when I try to I just always get that Uncaught TypeError: Type error
. Here is the code I have currently http://jsfiddle/cCDFs/.
If you need me to explain something better just ask.
Share Improve this question edited Mar 1, 2013 at 20:11 Andreas 21.9k7 gold badges51 silver badges58 bronze badges asked Mar 1, 2013 at 20:07 AnzwurAnzwur 6131 gold badge6 silver badges10 bronze badges2 Answers
Reset to default 5You're trying to pass a URL into drawImage
, which you can't do - you need to load an image element and pass it instead, after the image has finished loading.
This article has examples of what I'm describing.
The code would end up something like this:
var img = new Image();
img.onload = function() {
context.drawImage(img, 2, 2);
}
img.src = 'http://i.imgur./k73egsW.png';
In fact, your code does it already when it loads and draws the tiles.
The first argument you need to pass to drawImage is an Image or a Canvas object, not a string;
I didn't quite get what you try to achieve with your code, but if you replace your draw() call to your function by code like this:
var otherImageObj = new Image();
otherImageObj.onload = function() {
context.clearRect(0,0,canvas.width, canvas.height);
for (y = 0; y <= vHeight; y++) {
for (x = 0; x <= vWidth; x++) {
theX = x * 32;
theY = y * 32;
context.drawImage(tiles[board[y+vY][x+vX]], theX, theY, 32, 32);
}
}
context.drawImage(otherImageObj, 2, 2);
};
otherImageObj.src = "http://i.imgur./k73egsW.png";
Your code actually works, albeit, getting a "403 (Forbidden)" error for your image.
本文标签: javascriptUncaught TypeError Type error with drawImageStack Overflow
版权声明:本文标题:javascript - Uncaught TypeError: Type error with drawImage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744598157a2614902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论