-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
45 lines (41 loc) · 959 Bytes
/
map.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
var Map = function (id, name, width, height, layers = []) {
this.id = id;
this.name = name;
this.width = width;
this.height = height;
this.layers = layers;
}
Map.prototype.setLayers = function(layers) {
this.layers = layers;
};
Map.prototype.addLayer = function(layer) {
this.layers.push(layer);
};
Map.prototype.render = function () {
var imgCount = 0;
var loadCount = 0;
for(var src in window.state.images) {
if (typeof window.state.images[src] === 'string') {
imgCount++;
}
}
Object.keys(window.state.images).forEach(function (source) {
if (typeof source === 'string') {
var img = new Image();
img.onload = function() {
window.state.images[source] = img;
loadCount++;
if (loadCount >= imgCount) {
this.layers.forEach(function (layer) {
layer.render();
});
}
}.bind(this);
img.onerror = function()
{
console.log("Image failed!");
};
img.src = source;
}
}.bind(this));
};