I’m working on a HTML5 canvas game, and I want to load a high resolution image as background. This image is 10MB, so if the image loads slow, then I want to load a smaller image instead.
var myimg = new Image();
myimg.onload = function() {
clearTimeout(t);
alert('high resolution image loaded');
};
mximg.src = 'path/to/highres.png';
var t = setTimeout(function(){
//cancel loading somehow ?
myimg.onload = function() {
alert('low resolution image loaded');
};
myimg.src = 'path/to/low_res.png';
},5000);
should I cancel somehow the loading, or it’s cancelled if I edit the src attribute?
One alternative “solution” would be to load the low-res image first. Only if it loaded quickly enough you start to load the high-res version to replace it.
There are three possible benefits as I see it: