/    Sign up×
Community /Pin to ProfileBookmark

Problem preloading images

I am trying to preload a list of images (actually only 2 so far) but am getting the following error:

[COLOR=”#FF0000″][B][I]Uncaught TypeError: Failed to execute ‘drawImage’ on ‘CanvasRenderingContext2D’: No function was found that matched the signature provided. [/I][/B][/COLOR]

Here it is my HTML code:

[code=php]<!DOCTYPE HTML>
<html>
<head>

<META NAME=”ROBOTS” CONTENT=”NOINDEX, NOFOLLOW”>
</head>

<!– body onLoad=”initLobby()” style=”margin:0px;border:0px;padding:0px;width:320px;height:340px;” –>

<body onLoad=”PreloadImages();” style=”margin:0px;border:0px;padding:0px;width:320px;height:340px;”>
<canvas id=”the-multiplication-game” width=”320″ height=”340″>

</canvas>

<script type=’text/javascript’ src=’https://cdn.firebase.com/v0/firebase.js’></script>
<script src=”js/util.js”></script>
<script src=”js/lobby.js”></script>
<script src=”js/begin.js”></script>
<script src=”js/userclicked.js”></script>

<!– <script src=”create-rooms.js”></script>
<script src=”themultiplicationgame.js”></script> –>

</body>
</html>[/code]

And here it is my JS code:

[code=php]var canvas = document.getElementById(‘the-multiplication-game’);
var context = canvas.getContext(‘2d’);

var myimages = new Array();
var LoadedImages = 0;

var image_list = new Array();
image_list[0] = ‘bg.jpg’;
image_list[1] = ‘game_name.png’;

function PreloadImages()
{
if (document.images)
{
for (var i=0;i<=image_list.length-1;i++)
{
myimages[i] = new Image;
myimages[i].src = “images/”+image_list[i];
myimages[i].onload = function()
{
LoadedImages++;
if (LoadedImages == image_list.length)
for (var j=0;j<=myimages.length-1;j++)
context.drawImage(myimages[j].src,0,0);
}
}
}
}
[/code]

Any idea about what I am doing wrong?

PS: The error is happening on line [I]context.drawImage(myimages[j].src,0,0); [/I]

Thanks!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowMay 11.2014 — You shouldn't be passing .src, you should just be passing the image element.

context.drawImage(myimages[j],0,0);

That's your bug...

Some advice, don't waste time saying "new Array" when [] will do the same job... don't waste time saying "var" on every new var when you can pass a comma delimited list. Don't introduce an extra subtraction to your loop check when you could just do "<" instead of "<=".

var
canvas = document.getElementById('the-multiplication-game'),
context = canvas.getContext('2d'),
myimages = [];
LoadedImages = 0,
image_list = ['bg.jpg', 'game_name.png'];

function PreloadImages() {
if (document.images) {
for (var i = 0; i &lt; image_list.length; i++) {
myimages[i] = new Image;
myimages[i].src = 'images/' + image_list[i];
myimages[i].onload = function() {
if (++LoadedImages == image_list.length)
for (var j = 0; j &lt; myimages.length; j++)
context.drawImage(myimages[j], 0, 0);
}
}
}
}


Should do the job...

Also, some other advice: don't declare width and height on BODY, it's unreliable cross browser (IE will still screw it up and random intervals), don't declare STYLE in the markup (Even in testing, just makes for sloppy coding) and as a Scripting only event you should probably hook 'onload' instead of using the attribute, just in case some other script (since you seem to be loading a slew of them) interferes with that.

I probably would also generate the canvas from the scripting as it's a scripting only element and as such has no business in the markup as a tag -- but that's more my dislike for how STUPID HTML 5's implementation is.
Copy linkTweet thisAlerts:
@deathshadowMay 12.2014 — Oops, myImages = [] should have a comma after it, not a semicolon.
Copy linkTweet thisAlerts:
@SteamPunk_1966authorMay 12.2014 — Hey deathshadow, what an excellent response!

Thank you for all the valuable tips. I will be more careful with such details from now on.

Again, thank you very very much!

?
×

Success!

Help @SteamPunk_1966 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...