-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourcesLoader.js
52 lines (45 loc) · 1021 Bytes
/
ResourcesLoader.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
ResourcesLoader = (function() {
var that = {};
var _callback;
var images = {};
var totalResources = 0;
var loadedResources = 0;
function resourcesLoaded() {
loadedResources += 1;
if(loadedResources == totalResources) {
if(_callback) {
_callback();
}
}
}
that.loadImage = function(name, src) {
totalResources += 1;
var img = new Image();
img.onload = function() {
resourcesLoaded();
}
img.src = src;
images[name] = img;
}
that.loadFont = function(name, src, letters) {
totalResources += 1;
var img = new Image();
img.dataset["letters"] = letters;
img.onload = function() {
img.dataset["charWidth"] = img.width / letters.length
resourcesLoaded();
}
img.src = src;
images[name] = img;
}
that.getImage = function(name) {
return images[name];
}
that.onload = function(callback) {
_callback = callback;
if(totalResources == 0) {
_callback();
}
}
return that
})();