-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (33 loc) · 991 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
/**
* Load images on 'urls' asyncronously and then invoke the callback
* @function
*
* @param {String|String[]} urls - Image urls to be loaded
* @param {Function} callback - The callback to be executed once the images are
* loaded
*
* Example:
*
* imageLoad(['foo.gif', 'bar.jpg', 'baz.png'], function(foo, bar baz) {
* // foo, bar, and baz are Image objects, manipulate them here
* });
*/
module.exports = function(urls, callback) {
if (!Array.isArray(urls)) {
urls = [urls];
}
var images = [];
// Insert the image and invoke the callback if all urls were loaded
var tryCallback = function(image, index) {
images[index] = image;
var length = images.filter(function(i) {return i}).length
length === urls.length && callback.apply(this, images);
}
urls.forEach(function(url, index) {
var image = new Image();
image.onload = function() {
tryCallback(image, index);
};
image.src = url;
});
};