Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Failed to execute 'drawImage'

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

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

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); 

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

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

Here is a JSFiddle to demonstrate, alonside all the code. Player.die() is located on line[242].

like image 489
tanishalfelven Avatar asked Mar 15 '14 22:03

tanishalfelven


2 Answers

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

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

But instead I was get the elements by id from the document page.

Resources:

Explanation on loading images

Explanation on how html loads images

like image 52
tanishalfelven Avatar answered Sep 23 '22 19:09

tanishalfelven


you should wait all elements load,so do like this:

window.onload=function(){        board.drawImage(document.getElementById("player_explode"), this.x, this.y); } 
like image 45
DODD Avatar answered Sep 21 '22 19:09

DODD