-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_preloader.js
128 lines (105 loc) · 4.04 KB
/
img_preloader.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* The image preloader module
*/
var imgPreloader = (function() {
// keep track of the number of images being loaded
var nTotalImgNum = 0;
var nLoadedImgNum = 0;
var nLoadProgress = 0;
// callback functions and arguments to be invoked on the image load/error events
var callbackFuncSuccess = null
var callbackFuncSuccessArgs = [];
var callbackFuncFail = null;
var callbackFuncFailArgs = [];
/**
* The helper function to load the image and create a promise for it
* @param string imgSrc: the image source
* @return Promise: a Promise object for the image being loaded
*/
var loadImgHelper = function( imgSrc ) {
var imgPromise = new Promise(function(resolve, reject) {
var imgObj = new Image();
// success when loading the image (asynchronous)
imgObj.onload = function() {
++imgPreloader.nLoadedImgNum;
imgPreloader.nLoadProgress = 100.0 * (imgPreloader.nLoadedImgNum / imgPreloader.nTotalImgNum);
if ( imgPreloader.callbackFuncSuccess && typeof imgPreloader.callbackFuncSuccess === "function" ) {
// callback function
imgPreloader.callbackFuncSuccess.apply(imgPreloader, imgPreloader.callbackFuncSuccessArgs);
}
resolve({
src: imgSrc,
status: "ok"
});
};
// error when loading the image (asynchronous)
imgObj.onerror = function() {
imgPreloader.nLoadProgress = 100.0 * (imgPreloader.nLoadedImgNum / imgPreloader.nTotalImgNum);
if ( imgPreloader.callbackFuncFail && typeof imgPreloader.callbackFuncFail === "function" ) {
// callback function
imgPreloader.callbackFuncFail.apply(imgPreloader, imgPreloader.callbackFuncFailArgs);
}
// use resolve instead of reject on error so it will continue loading the rest of the images
resolve({
src: imgSrc,
status: "error"
});
};
imgObj.src = imgSrc;
});
return imgPromise;
};
/**
* The main function to preload the images
* @param array arrImgSrc: the array containing the promises of images to be loaded
* @param function callbackFuncSuccess: the callback function to invoke when the image loading is successful
* @param array callbackFuncSuccessArgs: the arguments to the callback function upon success
* @param function callbackFuncFail: the callback function to invoke when the image loading fails
* @param array callbackFuncFailArgs: the arguments to the callback function upon failure
* @return Object: an object {src, status} which is the result of Promise.resolve()
*/
var preloadImages = function( arrImgSrc,
callbackFuncSuccess, callbackFuncSuccessArgs,
callbackFuncFail, callbackFuncFailArgs ) {
try {
if ( arrImgSrc.constructor !== Array ) {
throw new Error("The parameter should be an array");
}
imgPreloader.nTotalImgNum = arrImgSrc.length;
// handle the default parameters of the callback if not provided
if ( typeof callbackFuncSuccess === "undefined" || !callbackFuncSuccess ) {
callbackFuncSuccess = null;
}
if ( typeof callbackFuncSuccessArgs === "undefined" || !callbackFuncSuccessArgs ) {
callbackFuncSuccessArgs = [];
}
if ( typeof callbackFuncFail === "undefined" || !callbackFuncFail ) {
callbackFuncFail = null;
}
if ( typeof callbackFuncFailArgs === "undefined" || !callbackFuncFailArgs ) {
callbackFuncFailArgs = [];
}
// set the callback functions and arguments
imgPreloader.callbackFuncSuccess = callbackFuncSuccess
imgPreloader.callbackFuncSuccessArgs = callbackFuncSuccessArgs;
imgPreloader.callbackFuncFail = callbackFuncFail;
imgPreloader.callbackFuncFailArgs = callbackFuncFailArgs;
var arrImgPromises = new Array();
// prepare the promises for loading each image
arrImgSrc.forEach(function( imgSrc ) {
var imgPromise = loadImgHelper(imgSrc);
arrImgPromises.push( imgPromise );
});
// process all the promises in a batch
return Promise.all( arrImgPromises );
} catch (err) {
return Promise.reject(err);
}
};
return {
preloadImages: preloadImages,
nTotalImgNum: nTotalImgNum,
nLoadedImgNum: nLoadedImgNum,
nLoadProgress: nLoadProgress
}
})();