Skip to content

Instantly share code, notes, and snippets.

@m1erickson
Created June 6, 2014 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m1erickson/1dbeeae071e986ddc45c to your computer and use it in GitHub Desktop.
Save m1erickson/1dbeeae071e986ddc45c to your computer and use it in GitHub Desktop.
// image loader
var imageURLs=[]; // put the paths to your images here
var imagesOK=0;
var imgs=[];
imageURLs.push("");
loadAllImages(start);
function loadAllImages(callback){
for (var i=0; i<imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function(){
imagesOK++;
if (imagesOK>=imageURLs.length ) {
callback();
}
};
img.onerror=function(){alert("image load failed");}
img.crossOrigin="anonymous";
img.src = imageURLs[i];
}
}
function start(){
// the imgs[] array now holds fully loaded images
// the imgs[] are in the same order as imageURLs[]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment