-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.js
More file actions
96 lines (87 loc) · 2.94 KB
/
ImageLoader.js
File metadata and controls
96 lines (87 loc) · 2.94 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* An incredibly simple JavaScript image loader.
*
* Sample usage:
*
* // Loading an array of images
* ImageLoader.load(['image1.png', 'image2.png', 'image3.png'], function(loaded)) {
* // loaded now contains:
* // loaded[0] = Image element for image1.png
* // loaded[1] = Image element for image2.png
* };
*
* // Loading a map of images
* ImageLoader.load({'image1': 'image1.png', 'image2': 'image2.png'}, function(loaded)) {
* // loaded now contains:
* // loaded['image1'] = Image element for image1.png
* // loaded['image2'] = Image element for image2.png
* };
*
* // A root directory for images can be specified with
* ImageLoader.root = 'images';
*
* // Now when ImageLoader.load is called image names will be prefixed with
* // root plus a trailing slash i.e. image1.png becomes images/image1.png
*
* // Trailing slashes will only be added to the prefix if needed
*/
;(function(global) {
// The ImageLoader namespace
var ImageLoader = {
/**
* Optionally can set an image root, all loaded images will be prefixed with this
* image root if it is not null. It doesn't matter if root ends in a trailing slash
* or not.
*/
root: null,
/**
* Load the images specified by name and call the callback with the loaded images.
* @param names
* An array or map of file names. If using an array callback will receive an
* array of loaded images, if using a map callback will receive a map of loaded
* images containing the same keys in the argument map.
* @param callback
* A function to call with the loaded images when loading is completed.
*/
load: function(names, callback) {
// Initialize space to hold the loaded images
var images = names.constructor == Object ? {} : [];
// Determine how many images need to be loaded
var toLoad = names.length || Object.keys(names).length;
// Keep track of how many images have been loaded
var loaded = 0;
// Internal function to call when an image is loaded successfully
var imageLoaded = function() {
// If every image is loaded and the callback isn't null call the callback
if (++loaded == toLoad && callback)
callback(images);
};
// File prefix is initially empty
var prefix = '';
// If root has been set
if (this.root) {
// Ensure root ends with a trailing slash
if (this.root[this.root.length - 1] != '/')
this.root += '/';
// Use root as the prefix
prefix = this.root;
}
// Start loading all of the images
for (var k in names) {
images[k] = new Image();
images[k].src = prefix + names[k];
images[k].onload = imageLoaded;
}
}
};
// Check for AMD
if (typeof global.define === 'function' && global.define.amd) {
// Expose as a module with the name ImageLoader
global.define('ImageLoader', [], function() {
return ImageLoader;
});
} else {
// Expose the ImageLoader namespace to global scope
global.ImageLoader = ImageLoader;
}
})(this);