cdf
Version:
A library for creating oldschool demo-like animations with JavaScript
37 lines (32 loc) • 1.1 kB
JavaScript
(function(global){
var lib = global.cdf || {};
var ImageLoader = lib.ImageLoader = function () {
var args=[];
if(arguments[0] && arguments[0].forEach)args = arguments[0];
else if(arguments.length)args = [].slice.call(arguments);
this.paths = [];
this.images = {};
this.finished = [];
for(var i = 0; i< args.length; i++){
this.add(args[i]);
}
};
ImageLoader.prototype = {
add:function (url) {
var instance = this;
if(this.paths.indexOf(url)>=0)return;
this.paths.push(url);
var img = this.images[url] = new lib.Image(url);
img.on('load', function(){
instance.finished.push(url);
instance.trigger('progress',[img,instance.paths.length, instance.finished.length], instance);
if(instance.paths.length === instance.finished.length){
instance.trigger('finished',[instance.images], instance);
}
});
return this;
}
};
lib.utils.eventer(ImageLoader.prototype);
if(typeof module!=='undefined'&& module.exports){module.exports=lib;}else{global.cdf=lib;}
})(window);