-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ImageTile.js
197 lines (181 loc) · 4.81 KB
/
ImageTile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @module ol/ImageTile
*/
import {listenImage} from './Image.js';
import Tile from './Tile.js';
import TileState from './TileState.js';
import {createCanvasContext2D} from './dom.js';
class ImageTile extends Tile {
/**
* @param {import("./tilecoord.js").TileCoord} tileCoord Tile coordinate.
* @param {import("./TileState.js").default} state State.
* @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin.
* @param {import("./Tile.js").LoadFunction} tileLoadFunction Tile load function.
* @param {import("./Tile.js").Options} [options] Tile options.
*/
constructor(tileCoord, state, src, crossOrigin, tileLoadFunction, options) {
super(tileCoord, state, options);
/**
* @private
* @type {?string}
*/
this.crossOrigin_ = crossOrigin;
/**
* Image URI
*
* @private
* @type {string}
*/
this.src_ = src;
this.key = src;
/**
* @private
* @type {HTMLImageElement|HTMLCanvasElement}
*/
this.image_ = new Image();
if (crossOrigin !== null) {
this.image_.crossOrigin = crossOrigin;
}
/**
* @private
* @type {?function():void}
*/
this.unlisten_ = null;
/**
* @private
* @type {import("./Tile.js").LoadFunction}
*/
this.tileLoadFunction_ = tileLoadFunction;
}
/**
* Get the HTML image element for this tile (may be a Canvas, Image, or Video).
* @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image.
* @api
*/
getImage() {
return this.image_;
}
/**
* Sets an HTML image element for this tile (may be a Canvas or preloaded Image).
* @param {HTMLCanvasElement|HTMLImageElement} element Element.
*/
setImage(element) {
this.image_ = element;
this.state = TileState.LOADED;
this.unlistenImage_();
this.changed();
}
/**
* Tracks loading or read errors.
*
* @private
*/
handleImageError_() {
this.state = TileState.ERROR;
this.unlistenImage_();
this.image_ = getBlankImage();
this.changed();
}
/**
* Tracks successful image load.
*
* @private
*/
handleImageLoad_() {
const image = /** @type {HTMLImageElement} */ (this.image_);
if (image.naturalWidth && image.naturalHeight) {
this.state = TileState.LOADED;
} else {
this.state = TileState.EMPTY;
}
this.unlistenImage_();
this.changed();
}
/**
* Load the image or retry if loading previously failed.
* Loading is taken care of by the tile queue, and calling this method is
* only needed for preloading or for reloading in case of an error.
*
* To retry loading tiles on failed requests, use a custom `tileLoadFunction`
* that checks for error status codes and reloads only when the status code is
* 408, 429, 500, 502, 503 and 504, and only when not too many retries have been
* made already:
*
* ```js
* const retryCodes = [408, 429, 500, 502, 503, 504];
* const retries = {};
* source.setTileLoadFunction((tile, src) => {
* const image = tile.getImage();
* fetch(src)
* .then((response) => {
* if (retryCodes.includes(response.status)) {
* retries[src] = (retries[src] || 0) + 1;
* if (retries[src] <= 3) {
* setTimeout(() => tile.load(), retries[src] * 1000);
* }
* return Promise.reject();
* }
* return response.blob();
* })
* .then((blob) => {
* const imageUrl = URL.createObjectURL(blob);
* image.src = imageUrl;
* setTimeout(() => URL.revokeObjectURL(imageUrl), 5000);
* })
* .catch(() => tile.setState(3)); // error
* });
* ```
* @api
* @override
*/
load() {
if (this.state == TileState.ERROR) {
this.state = TileState.IDLE;
this.image_ = new Image();
if (this.crossOrigin_ !== null) {
this.image_.crossOrigin = this.crossOrigin_;
}
}
if (this.state == TileState.IDLE) {
this.state = TileState.LOADING;
this.changed();
this.tileLoadFunction_(this, this.src_);
this.unlisten_ = listenImage(
this.image_,
this.handleImageLoad_.bind(this),
this.handleImageError_.bind(this),
);
}
}
/**
* Discards event handlers which listen for load completion or errors.
*
* @private
*/
unlistenImage_() {
if (this.unlisten_) {
this.unlisten_();
this.unlisten_ = null;
}
}
/**
* @override
*/
disposeInternal() {
this.unlistenImage_();
this.image_ = null;
super.disposeInternal();
}
}
/**
* Get a 1-pixel blank image.
* @return {HTMLCanvasElement} Blank image.
*/
function getBlankImage() {
const ctx = createCanvasContext2D(1, 1);
ctx.fillStyle = 'rgba(0,0,0,0)';
ctx.fillRect(0, 0, 1, 1);
return ctx.canvas;
}
export default ImageTile;