-
Notifications
You must be signed in to change notification settings - Fork 7
/
tile-image.js
166 lines (139 loc) · 3.69 KB
/
tile-image.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { getTileKey } from "./util.js";
export const LOAD_STATE = {
IDLE: 'idle',
LOADING: 'loading',
LOADED: 'loaded',
ERROR: 'error',
};
export const DECODING_STATE = {
IDLE: 'idle',
DECODING: 'decoding',
DECODED: 'decoded'
};
class TileImage {
constructor(url, x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.key = getTileKey(x, y, z);
this.url = url;
this.hasParent = false; // the Image has an Tile that uses it
this.loadState = LOAD_STATE.IDLE;
this.decodingState = DECODING_STATE.IDLE;
this.styles = undefined;
this._createImage();
}
isLoaded() {
return this.loadState === LOAD_STATE.LOADED;
}
isDecoded() {
return this.decodingState === DECODING_STATE.DECODED;
}
setStyles(styles) {
if (styles) {
this.styles = styles;
for(let name in this.styles) {
this.imageElement.style[name] = this.styles[name];
}
}
}
tileDetached() {
this.hasParent = false;
if (this.decodingState !== DECODING_STATE.DECODING) {
this.decodingState = DECODING_STATE.IDLE;
}
}
tileAttached() {
this.hasParent = true;
}
_createImage() {
this.imageElement = new Image();
this.imageElement.setAttribute('role', 'presentation');
this.imageElement.setAttribute('alt', '');
this.setStyles(this.styles);
}
decode() {
if (this.loadState === LOAD_STATE.LOADED) {
// if image is loaded and decoded, just exit
if (this.decodingState === DECODING_STATE.DECODED) {
this._onDecoded();
return;
}
// if image is decoding, just wait for it
if (this.decodingState === DECODING_STATE.DECODING) {
return;
}
// start decoding
if (this.decodingState === DECODING_STATE.IDLE) {
this._startDecode();
}
} else if (this.loadState === LOAD_STATE.LOADING) {
if (this.decodingState === DECODING_STATE.DECODING) {
// if image is decoding, just wait for it
return;
}
} else {
// this.loadState === LOAD_STATE.IDLE || this.loadState === LOAD_STATE.ERROR
// Image is not loaded yet, or needs a reload
this._startDecode();
}
}
_startDecode() {
if (this.loadState === LOAD_STATE.ERROR) {
this._createImage();
}
if (!this.imageElement.src) {
this.imageElement.src = this.url;
}
this.decodingState = DECODING_STATE.DECODING;
if (this.loadState !== LOAD_STATE.LOADED) {
this.loadState = LOAD_STATE.LOADING;
}
if ('decode' in this.imageElement) {
this.imageElement.decode().then(() => {
this._onDecoded();
}).catch((error) => {
this._onDecodeError();
});
} else {
if (this.loadState === LOAD_STATE.LOADED) {
this._onDecoded();
} else {
this.imageElement.onload = () => {
this._onDecoded();
};
this.imageElement.onerror = (e) => {
this._onDecodeError();
};
}
}
}
_onDecoded() {
// If image has no parent, it won't be added to dom,
// thus it's not guaranteed that it's decoded
this.decodingState = this.hasParent
? DECODING_STATE.DECODED
: DECODING_STATE.IDLE;
this.loadState = LOAD_STATE.LOADED;
if (this.onDecoded) {
this.onDecoded(this);
}
}
_onDecodeError() {
this.decodingState = DECODING_STATE.IDLE;
this.loadState = LOAD_STATE.ERROR;
if (this.onError) {
this.onError(this);
}
}
destroy() {
if (this.imageElement) {
this.imageElement.onload = null;
this.imageElement.onerror = null;
this.imageElement = null;
}
this.onError = null;
this.onDecoded = null;
}
}
export default TileImage;