-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageLoader.js
More file actions
36 lines (32 loc) · 889 Bytes
/
ImageLoader.js
File metadata and controls
36 lines (32 loc) · 889 Bytes
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
// Image preloading
var ImageLoader = (function()
{
var ImageLoader = function(imagePaths)
{
this.imagePaths = imagePaths;
};
ImageLoader.prototype.load = function(callback)
{
this.loadedImages = 0;
this.callback = callback;
this.images = [];
for (var path in this.imagePaths)
{
var img = new Image();
// Bind to ensure correct 'this' in the callback
img.onload = this.onImageLoaded.bind(this);
img.src = this.imagePaths[path];
this.images.push(img);
}
};
// Only callback when all images are loaded
ImageLoader.prototype.onImageLoaded = function ()
{
this.loadedImages++;
if (this.loadedImages == this.images.length)
{
this.callback(this.images);
}
};
return ImageLoader;
})();