-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.js
111 lines (103 loc) · 3.79 KB
/
resources.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
/* Resources.js
* This is simply an image loading utility. It eases the process of loading
* image files so that they can be used within your game. It also includes
* a simple "caching" layer so it will reuse cached images if you attempt
* to load the same image multiple times.
*/
(function() {
var resourceCache = {};
var loading = [];
var readyCallbacks = [];
/* This is the publicly accessible image loading function. It accepts
* an array of strings pointing to image files or a string for a single
* image. It will then call our private image loading function accordingly.
*/
function load(urlOrArr) {
if(urlOrArr instanceof Array) {
/* If the developer passed in an array of images
* loop through each value and call our image
* loader on that image file
*/
urlOrArr.forEach(function(url) {
_load(url);
});
} else {
/* The developer did not pass an array to this function,
* assume the value is a string and call our image loader
* directly.
*/
_load(urlOrArr);
}
}
/* This is our private image loader function, it is
* called by the public image loader function.
*/
function _load(url) {
if(resourceCache[url]) {
/* If this URL has been previously loaded it will exist within
* our resourceCache array. Just return that image rather
* re-loading the image.
*/
return resourceCache[url];
} else {
/* This URL has not been previously loaded and is not present
* within our cache; we'll need to load this image.
*/
var img = new Image();
img.onload = function() {
/* Once our image has properly loaded, add it to our cache
* so that we can simply return this image if the developer
* attempts to load this file in the future.
*/
resourceCache[url] = img;
/* Once the image is actually loaded and properly cached,
* call all of the onReady() callbacks we have defined.
*/
if(isReady()) {
readyCallbacks.forEach(function(func) { func(); });
}
};
/* Set the initial cache value to false, this will change when
* the image's onload event handler is called. Finally, point
* the image's src attribute to the passed in URL.
*/
resourceCache[url] = false;
img.src = url;
}
}
/* This is used by developers to grab references to images they know
* have been previously loaded. If an image is cached, this functions
* the same as calling load() on that URL.
*/
function get(url) {
return resourceCache[url];
}
/* This function determines if all of the images that have been requested
* for loading have been properly loaded.
*/
function isReady() {
var ready = true;
for(var k in resourceCache) {
if(resourceCache.hasOwnProperty(k) &&
!resourceCache[k]) {
ready = false;
}
}
return ready;
}
/* This function will add a function to the callback stack that is called
* when all requested images are properly loaded.
*/
function onReady(func) {
readyCallbacks.push(func);
}
/* This object defines the publicly accessible functions available to
* developers by creating a global Resources object.
*/
window.Resources = {
load: load,
get: get,
onReady: onReady,
isReady: isReady
};
})();