1

When I load the page for the first time, nothing shows up but when I reload the page the image shows up. How to load the image on first page load? I referred to a few answers on SO, and found that I could use onload but the thing is that I am calling head.draw() from another function at a certain time.

let head_ = new Image();
head_.src = "img/head.png";

const head = {
    x : 12,
    y : 12,
    w : 36,
    h : 36,
    draw : function(){
        ctx.drawImage(head_, 0, 0, head_.width, head_.height, this.x, this.y, this.w, this.h);
    }
}

function draw(){
    // SOME CODE
    head.draw();
    // SOME CODE
}

function loop(){
    draw();
    requestAnimationFrame(loop);
}

And also all the other images are loading fine even on the first load. This concerns me more!!!

Is there any way to forcefully load the images?

FunnelScr
  • 161
  • 1
  • 12

0 Answers0