-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
39 lines (31 loc) · 784 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module.exports = function loadImages(images, callback){
var list = [];
if (typeof images === 'string') { list = [images]; }
else { list = [].slice.call(images); }
var count = 0;
var loadedImages = {};
function loaded(){
count += 1;
if (count === list.length) { done(null, loadedImages); }
}
function done(err, images) {
if(callback) {
callback(err, images);
}
}
for (var i=0; i<list.length; i++){
var img = new Image();
img.onerror = function(){
done('images not loaded');
}
img.onabort = function(){
done('images not loaded');
}
img.onload = function(){
loaded();
}
img.src = list[i];
img.name = list[i].slice(list[i].lastIndexOf('/')+1);
loadedImages[img.name] = img;
}
};