-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImagePreloader.js
More file actions
92 lines (77 loc) · 2.76 KB
/
ImagePreloader.js
File metadata and controls
92 lines (77 loc) · 2.76 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
/**
* Cargo cult of:
* http://www.webreference.com/programming/javascript/gr/column3/index.html
* with some modifications to track original urls, debugging, delayed start.
*/
function ImagePreloader(imageUrls, callback) {
// store the callback
this.callback = callback;
this.imageUrls = imageUrls;
// initialize internal state.
this.loaded = 0;
this.processed = 0;
this.total = imageUrls.length;
this.images = {};
}
ImagePreloader.prototype.start = function() {
// for each imageUrl, call preload()
for ( var i = 0; i < this.imageUrls.length; i++ ) {
this.preload(this.imageUrls[i]);
}
}
/**
* The callback function is stored for later use, then each image URL
* is passed into the preload() method.
*/
ImagePreloader.prototype.preload = function(imageUrl) {
// create new Image object and add to array
var image = new Image;
this.images[imageUrl] = image;
// set up event handlers for the Image object
image.onload = ImagePreloader.prototype.onload;
image.onerror = ImagePreloader.prototype.onerror;
image.onabort = ImagePreloader.prototype.onabort;
// assign pointer back to this.
image._imagePreloader = this;
image._loaded = false;
image._originalUrl = imageUrl;
console.log("loading image: " + imageUrl);
// assign the .src property to load the Image
image.src = imageUrl;
}
/**
* The preload function creates an Image object and assigns functions
* for the three Image events; onload, onerror and onabort. The onload
* event is raised when the image has been loaded into memory, the onerror
* event is raised when an error occurs while loading the image and the
* onabort event is raised if the user cancels the load by clicking the Stop
* button on the browser.
*
* A pointer to the ImagePreloader object is stored in each Image object to
* facilitate the callback mechanism. An optional boolean flag can be added
* here to indicate whether the image loads properly or not.
*
* Finally, the “src” attribute is assigned to start the loading of the image.
*/
ImagePreloader.prototype.onComplete = function() {
this.processed++;
if ( this.processed == this.total ) {
this.callback(this.images, this.loaded);
}
}
ImagePreloader.prototype.onload = function() {
console.log("loaded image: " + this._originalUrl);
this._loaded = true;
this._imagePreloader.loaded++;
this._imagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function() {
console.log("error loading image: " + this._originalUrl);
this._error = true;
this._imagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function() {
console.log("aborted loading image: " + this._originalUrl);
this._aborted = true;
this._imagePreloader.onComplete();
}