Skip to content

Instantly share code, notes, and snippets.

@cybear
Created July 7, 2012 10:26
Show Gist options
  • Save cybear/3065786 to your computer and use it in GitHub Desktop.
Save cybear/3065786 to your computer and use it in GitHub Desktop.
image loader
define(
function(){
"use strict";
return function(urls, success){
var imgs, counter=0;
function isEverythingDone(){
counter++;
console.log('Loaded '+counter+' of '+urls.length+': '+this.src+' '+this.width+'*'+this.height);
if(counter == urls.length) {
success(imgs);
}
}
function urlToImgElement(url){
var el=new Image();
el.src=url;
el.onload=isEverythingDone;
return el;
}
console.log('Attempting to load '+urls.length+' images...');
imgs = urls.map(urlToImgElement);
}
});
@cybear
Copy link
Author

cybear commented Jul 8, 2012

With regards to ES3, I prefer to code for newer and use polyfills for older browsers. I am aware that polyfills are not always 100% identical to the real thing, however for my coding style it usually seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment