Sir
Sir

Reputation: 8280

DrawImage will not load on first frame

I have a simple draw image for my canvas but it won't display on the first frame.

It's driving me mad i dunno why it won't do it!!

This is my script:

img = new Image();
img.src = 'images/0.png'; //preLoad the image


window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
          window.webkitRequestAnimationFrame || 
          window.mozRequestAnimationFrame    || 
          window.oRequestAnimationFrame      || 
          window.msRequestAnimationFrame     || 
          function( callback,  element){
            window.setTimeout(callback, 1000 / 60);
          };
})(); //frames per second


function gameUpdate(){ 
    previous = 0;
    requestAnimFrame( gameUpdate ); 
    draw(); 
}

Then the draw() function has this:

//doesn't display on first fame
canvas['canvas1'].ctx.drawImage(img, 100, 150); 

//displays on first frame           
canvas['canvas1'].ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
canvas['canvas1'].ctx.fillRect (30, 30, 55, 50);

FillRect works fine but not the drawImage for the first frame, any ideas why this might be ??

Upvotes: 0

Views: 387

Answers (1)

lostsource
lostsource

Reputation: 21830

I think you should start the animation loop by calling requestAnimFrame outside of your gameUpdate function

requestAnimFrame(gameUpdate)

You might also need to make sure the image is actually loaded

var img = new Image();
img.onload = function() {
    // do the draw image here
}
img.src = 'images/0.png'; //preLoad the image

Upvotes: 2

Related Questions