forked from kholland4/platformeng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgloader.js
50 lines (45 loc) · 1.05 KB
/
imgloader.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
var imgs = {};
var imgURLs = [
["coin", "coin.png"],
["portal", "portal.png"],
["brick", "brick.png"],
["block", "block.png"],
["slime", "slime.png"],
["lava", "lava.png"],
["bktile", "tile-base.png"],
["instructions.png", "instructions.png"],
["instructions2.png", "instructions2.png"],
["unknown", "unknown.png"]
];
for(var i = 0; i < imgURLs.length; i++) {
var img = document.createElement("img");
img.src = imgURLs[i][1];
imgs[imgURLs[i][0]] = img;
}
function initImgLoader() {
}
//https://i.canthack.it/detecting-broken-images-js.html
function imgLoaded(img) {
if(!img.complete) {
return false;
}
if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
return false;
}
return true;
}
function image(url) {
var img = document.createElement("img");
img.src = url;
return img;
}
function getImgIndex(img) {
for(var key in imgs) {
if(imgs[key] == img) {
return key;
}
}
}
function getImg(name, defaultVal=imgs.block) { //FIXME more generic default
return imgs[name] || defaultVal;
}