Why doesn't image show on load, but shows on refresh?

后端 未结 3 900
攒了一身酷
攒了一身酷 2020-12-17 18:17

I\'m playing with the canvas element in HTML5 and I have noticed a peculiar behavior. On initial load, an image I\'m displaying does not show. However, when I refresh the b

3条回答
  •  离开以前
    2020-12-17 18:35

    Images load asynchronously, so only after refresh it loads early enough because it's cached. Normally it isn't loaded yet at the time you call drawImage. Use onload:

    var image = new Image();
    image.src = "Images/smiley.png";
    image.onload = function() {
        context.drawImage(image, 50, 50);
    };
    

提交回复
热议问题