-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.js
189 lines (189 loc) · 6.31 KB
/
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* @module ol/Image
*/
import EventType from './events/EventType.js';
import ImageBase from './ImageBase.js';
import ImageState from './ImageState.js';
import { IMAGE_DECODE } from './has.js';
import { getHeight } from './extent.js';
import { listenOnce, unlistenByKey } from './events.js';
/**
* A function that takes an {@link module:ol/Image~Image} for the image and a
* `{string}` for the src as arguments. It is supposed to make it so the
* underlying image {@link module:ol/Image~Image#getImage} is assigned the
* content specified by the src. If not specified, the default is
*
* function(image, src) {
* image.getImage().src = src;
* }
*
* Providing a custom `imageLoadFunction` can be useful to load images with
* post requests or - in general - through XHR requests, where the src of the
* image element would be set to a data URI when the content is loaded.
*
* @typedef {function(ImageWrapper, string): void} LoadFunction
* @api
*/
var ImageWrapper = /** @class */ (function (_super) {
__extends(ImageWrapper, _super);
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number|undefined} resolution Resolution.
* @param {number} pixelRatio Pixel ratio.
* @param {string} src Image source URI.
* @param {?string} crossOrigin Cross origin.
* @param {LoadFunction} imageLoadFunction Image load function.
*/
function ImageWrapper(extent, resolution, pixelRatio, src, crossOrigin, imageLoadFunction) {
var _this = _super.call(this, extent, resolution, pixelRatio, ImageState.IDLE) || this;
/**
* @private
* @type {string}
*/
_this.src_ = src;
/**
* @private
* @type {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement}
*/
_this.image_ = new Image();
if (crossOrigin !== null) {
_this.image_.crossOrigin = crossOrigin;
}
/**
* @private
* @type {?function():void}
*/
_this.unlisten_ = null;
/**
* @protected
* @type {import("./ImageState.js").default}
*/
_this.state = ImageState.IDLE;
/**
* @private
* @type {LoadFunction}
*/
_this.imageLoadFunction_ = imageLoadFunction;
return _this;
}
/**
* @return {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} Image.
* @api
*/
ImageWrapper.prototype.getImage = function () {
return this.image_;
};
/**
* Tracks loading or read errors.
*
* @private
*/
ImageWrapper.prototype.handleImageError_ = function () {
this.state = ImageState.ERROR;
this.unlistenImage_();
this.changed();
};
/**
* Tracks successful image load.
*
* @private
*/
ImageWrapper.prototype.handleImageLoad_ = function () {
if (this.resolution === undefined) {
this.resolution = getHeight(this.extent) / this.image_.height;
}
this.state = ImageState.LOADED;
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.
* @api
*/
ImageWrapper.prototype.load = function () {
if (this.state == ImageState.IDLE || this.state == ImageState.ERROR) {
this.state = ImageState.LOADING;
this.changed();
this.imageLoadFunction_(this, this.src_);
this.unlisten_ = listenImage(this.image_, this.handleImageLoad_.bind(this), this.handleImageError_.bind(this));
}
};
/**
* @param {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} image Image.
*/
ImageWrapper.prototype.setImage = function (image) {
this.image_ = image;
};
/**
* Discards event handlers which listen for load completion or errors.
*
* @private
*/
ImageWrapper.prototype.unlistenImage_ = function () {
if (this.unlisten_) {
this.unlisten_();
this.unlisten_ = null;
}
};
return ImageWrapper;
}(ImageBase));
/**
* @param {HTMLCanvasElement|HTMLImageElement|HTMLVideoElement} image Image element.
* @param {function():any} loadHandler Load callback function.
* @param {function():any} errorHandler Error callback function.
* @return {function():void} Callback to stop listening.
*/
export function listenImage(image, loadHandler, errorHandler) {
var img = /** @type {HTMLImageElement} */ (image);
if (img.src && IMAGE_DECODE) {
var promise = img.decode();
var listening_1 = true;
var unlisten = function () {
listening_1 = false;
};
promise
.then(function () {
if (listening_1) {
loadHandler();
}
})
.catch(function (error) {
if (listening_1) {
// FIXME: Unconditionally call errorHandler() when this bug is fixed upstream:
// https://bugs.webkit.org/show_bug.cgi?id=198527
if (error.name === 'EncodingError' &&
error.message === 'Invalid image type.') {
loadHandler();
}
else {
errorHandler();
}
}
});
return unlisten;
}
var listenerKeys = [
listenOnce(img, EventType.LOAD, loadHandler),
listenOnce(img, EventType.ERROR, errorHandler),
];
return function unlisten() {
listenerKeys.forEach(unlistenByKey);
};
}
export default ImageWrapper;
//# sourceMappingURL=Image.js.map