JavaScript无法执行'drawImage' [英] JavaScript Failed to execute 'drawImage'

查看:2910
本文介绍了JavaScript无法执行'drawImage'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我创建一个JavaScript Canvas Elements等游戏。我已经能够加载TONS的图片,但在一些选择,JavaScript回复与错误,如

Ok so I'm creating a game with JavaScript Canvas Elements and such. I've been able to load in TONS of Images, but on a select few, JavaScript replies with Errors such as


未捕获TypeError:失败在'CanvasRenderingContext2D'上执行'drawImage':没有找到匹配所提供的签名的函数。

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': No function was found that matched the signature provided.

这使得NO SENSE,因为相同的代码在其他地方工作!?

Which makes NO SENSE at all, because the same code works in other places!?!

这是我的代码中的一个例子:

Here is an example I have in my code:

board.drawImage(document.getElementById("player_explode"), this.x, this.y);

在一个objects方法中, Player.die()

Inside of an objects method, Player.die(), respectively.

有没有人知道为什么会出现这种行为?我非常沮丧...

Does anyone know why this behaviour is coming about? I'm getting very frustrated about it...

这里是 JSFiddle 来验证所有代码。
Player.die()位于行[242]

推荐答案

问题是我加载图片的方式,我应该这样做:

The problem was the way that I was loading my Images, I should've been doing:

var image = new Image();
image.src = "imagesource.jpg";

但是我通过文档中的id获取元素页面。

资源:

加载图片的说明

关于html加载图片的说明

这篇关于JavaScript无法执行'drawImage'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆