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

后端 未结 3 902
攒了一身酷
攒了一身酷 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:44

    Actually, even just using image.onload = function() {}, you can still run into problems. Do use this technique (that's not at all what I'm saying), but move it to the bottom of your page.

    As an example, I have a social networking site that uses canvas to show the profile photo (URI stored to the DB), and print it to canvas, then overlay a logo.

    Hi! My name is {profile_name}

    Add {profile_name} as a friend? DEBUG: Canvas is not supported. Click to visit {profile_name} Gallery

    ...

    
    
    
    

    Ideally, this would go all the way at the bottom before the end tag, but I put it up higher because of my template system. Apparently, this gives the image time to load after the canvas element has been drawn to the screen and is ready to receive input.

    I can't rely on setting the background of the canvas, and I have no desire to contend with refreshes. For whatever reason, just including the script with img.onload = function() {} was not enough. Move it lower, and save yourself the headaches.

    提交回复
    热议问题