Manual class transform
This commit is contained in:
+197
-205
@@ -19,74 +19,227 @@ import {shared as iconImageCache} from '../style/IconImageCache.js';
|
||||
* @param {module:ol/color~Color} color Color.
|
||||
* @extends {module:ol/events/EventTarget}
|
||||
*/
|
||||
const IconImage = function(image, src, size, crossOrigin, imageState, color) {
|
||||
class IconImage {
|
||||
constructor(image, src, size, crossOrigin, imageState, color) {
|
||||
|
||||
EventTarget.call(this);
|
||||
EventTarget.call(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
*/
|
||||
this.hitDetectionImage_ = null;
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
*/
|
||||
this.hitDetectionImage_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
*/
|
||||
this.image_ = !image ? new Image() : image;
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
*/
|
||||
this.image_ = !image ? new Image() : image;
|
||||
|
||||
if (crossOrigin !== null) {
|
||||
this.image_.crossOrigin = crossOrigin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLCanvasElement}
|
||||
*/
|
||||
this.canvas_ = color ?
|
||||
/** @type {HTMLCanvasElement} */ (document.createElement('CANVAS')) :
|
||||
null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/color~Color}
|
||||
*/
|
||||
this.color_ = color;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<module:ol/events~EventsKey>}
|
||||
*/
|
||||
this.imageListenerKeys_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/ImageState}
|
||||
*/
|
||||
this.imageState_ = imageState;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/size~Size}
|
||||
*/
|
||||
this.size_ = size;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
this.src_ = src;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.tainting_ = false;
|
||||
if (this.imageState_ == ImageState.LOADED) {
|
||||
this.determineTainting_();
|
||||
}
|
||||
|
||||
if (crossOrigin !== null) {
|
||||
this.image_.crossOrigin = crossOrigin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {HTMLCanvasElement}
|
||||
*/
|
||||
this.canvas_ = color ?
|
||||
/** @type {HTMLCanvasElement} */ (document.createElement('CANVAS')) :
|
||||
null;
|
||||
determineTainting_() {
|
||||
const context = createCanvasContext2D(1, 1);
|
||||
try {
|
||||
context.drawImage(this.image_, 0, 0);
|
||||
context.getImageData(0, 0, 1, 1);
|
||||
} catch (e) {
|
||||
this.tainting_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/color~Color}
|
||||
*/
|
||||
this.color_ = color;
|
||||
dispatchChangeEvent_() {
|
||||
this.dispatchEvent(EventType.CHANGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<module:ol/events~EventsKey>}
|
||||
*/
|
||||
this.imageListenerKeys_ = null;
|
||||
handleImageError_() {
|
||||
this.imageState_ = ImageState.ERROR;
|
||||
this.unlistenImage_();
|
||||
this.dispatchChangeEvent_();
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/ImageState}
|
||||
*/
|
||||
this.imageState_ = imageState;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {module:ol/size~Size}
|
||||
*/
|
||||
this.size_ = size;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
this.src_ = src;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.tainting_ = false;
|
||||
if (this.imageState_ == ImageState.LOADED) {
|
||||
handleImageLoad_() {
|
||||
this.imageState_ = ImageState.LOADED;
|
||||
if (this.size_) {
|
||||
this.image_.width = this.size_[0];
|
||||
this.image_.height = this.size_[1];
|
||||
}
|
||||
this.size_ = [this.image_.width, this.image_.height];
|
||||
this.unlistenImage_();
|
||||
this.determineTainting_();
|
||||
this.replaceColor_();
|
||||
this.dispatchChangeEvent_();
|
||||
}
|
||||
|
||||
};
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image or Canvas element.
|
||||
*/
|
||||
getImage(pixelRatio) {
|
||||
return this.canvas_ ? this.canvas_ : this.image_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {module:ol/ImageState} Image state.
|
||||
*/
|
||||
getImageState() {
|
||||
return this.imageState_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image element.
|
||||
*/
|
||||
getHitDetectionImage(pixelRatio) {
|
||||
if (!this.hitDetectionImage_) {
|
||||
if (this.tainting_) {
|
||||
const width = this.size_[0];
|
||||
const height = this.size_[1];
|
||||
const context = createCanvasContext2D(width, height);
|
||||
context.fillRect(0, 0, width, height);
|
||||
this.hitDetectionImage_ = context.canvas;
|
||||
} else {
|
||||
this.hitDetectionImage_ = this.image_;
|
||||
}
|
||||
}
|
||||
return this.hitDetectionImage_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {module:ol/size~Size} Image size.
|
||||
*/
|
||||
getSize() {
|
||||
return this.size_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string|undefined} Image src.
|
||||
*/
|
||||
getSrc() {
|
||||
return this.src_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load not yet loaded URI.
|
||||
*/
|
||||
load() {
|
||||
if (this.imageState_ == ImageState.IDLE) {
|
||||
this.imageState_ = ImageState.LOADING;
|
||||
this.imageListenerKeys_ = [
|
||||
listenOnce(this.image_, EventType.ERROR,
|
||||
this.handleImageError_, this),
|
||||
listenOnce(this.image_, EventType.LOAD,
|
||||
this.handleImageLoad_, this)
|
||||
];
|
||||
try {
|
||||
this.image_.src = this.src_;
|
||||
} catch (e) {
|
||||
this.handleImageError_();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
replaceColor_() {
|
||||
if (this.tainting_ || this.color_ === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.canvas_.width = this.image_.width;
|
||||
this.canvas_.height = this.image_.height;
|
||||
|
||||
const ctx = this.canvas_.getContext('2d');
|
||||
ctx.drawImage(this.image_, 0, 0);
|
||||
|
||||
const imgData = ctx.getImageData(0, 0, this.image_.width, this.image_.height);
|
||||
const data = imgData.data;
|
||||
const r = this.color_[0] / 255.0;
|
||||
const g = this.color_[1] / 255.0;
|
||||
const b = this.color_[2] / 255.0;
|
||||
|
||||
for (let i = 0, ii = data.length; i < ii; i += 4) {
|
||||
data[i] *= r;
|
||||
data[i + 1] *= g;
|
||||
data[i + 2] *= b;
|
||||
}
|
||||
ctx.putImageData(imgData, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards event handlers which listen for load completion or errors.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
unlistenImage_() {
|
||||
this.imageListenerKeys_.forEach(unlistenByKey);
|
||||
this.imageListenerKeys_ = null;
|
||||
}
|
||||
}
|
||||
|
||||
inherits(IconImage, EventTarget);
|
||||
|
||||
@@ -110,165 +263,4 @@ export function get(image, src, size, crossOrigin, imageState, color) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.determineTainting_ = function() {
|
||||
const context = createCanvasContext2D(1, 1);
|
||||
try {
|
||||
context.drawImage(this.image_, 0, 0);
|
||||
context.getImageData(0, 0, 1, 1);
|
||||
} catch (e) {
|
||||
this.tainting_ = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.dispatchChangeEvent_ = function() {
|
||||
this.dispatchEvent(EventType.CHANGE);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.handleImageError_ = function() {
|
||||
this.imageState_ = ImageState.ERROR;
|
||||
this.unlistenImage_();
|
||||
this.dispatchChangeEvent_();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.handleImageLoad_ = function() {
|
||||
this.imageState_ = ImageState.LOADED;
|
||||
if (this.size_) {
|
||||
this.image_.width = this.size_[0];
|
||||
this.image_.height = this.size_[1];
|
||||
}
|
||||
this.size_ = [this.image_.width, this.image_.height];
|
||||
this.unlistenImage_();
|
||||
this.determineTainting_();
|
||||
this.replaceColor_();
|
||||
this.dispatchChangeEvent_();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image or Canvas element.
|
||||
*/
|
||||
IconImage.prototype.getImage = function(pixelRatio) {
|
||||
return this.canvas_ ? this.canvas_ : this.image_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {module:ol/ImageState} Image state.
|
||||
*/
|
||||
IconImage.prototype.getImageState = function() {
|
||||
return this.imageState_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image element.
|
||||
*/
|
||||
IconImage.prototype.getHitDetectionImage = function(pixelRatio) {
|
||||
if (!this.hitDetectionImage_) {
|
||||
if (this.tainting_) {
|
||||
const width = this.size_[0];
|
||||
const height = this.size_[1];
|
||||
const context = createCanvasContext2D(width, height);
|
||||
context.fillRect(0, 0, width, height);
|
||||
this.hitDetectionImage_ = context.canvas;
|
||||
} else {
|
||||
this.hitDetectionImage_ = this.image_;
|
||||
}
|
||||
}
|
||||
return this.hitDetectionImage_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {module:ol/size~Size} Image size.
|
||||
*/
|
||||
IconImage.prototype.getSize = function() {
|
||||
return this.size_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {string|undefined} Image src.
|
||||
*/
|
||||
IconImage.prototype.getSrc = function() {
|
||||
return this.src_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Load not yet loaded URI.
|
||||
*/
|
||||
IconImage.prototype.load = function() {
|
||||
if (this.imageState_ == ImageState.IDLE) {
|
||||
this.imageState_ = ImageState.LOADING;
|
||||
this.imageListenerKeys_ = [
|
||||
listenOnce(this.image_, EventType.ERROR,
|
||||
this.handleImageError_, this),
|
||||
listenOnce(this.image_, EventType.LOAD,
|
||||
this.handleImageLoad_, this)
|
||||
];
|
||||
try {
|
||||
this.image_.src = this.src_;
|
||||
} catch (e) {
|
||||
this.handleImageError_();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.replaceColor_ = function() {
|
||||
if (this.tainting_ || this.color_ === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.canvas_.width = this.image_.width;
|
||||
this.canvas_.height = this.image_.height;
|
||||
|
||||
const ctx = this.canvas_.getContext('2d');
|
||||
ctx.drawImage(this.image_, 0, 0);
|
||||
|
||||
const imgData = ctx.getImageData(0, 0, this.image_.width, this.image_.height);
|
||||
const data = imgData.data;
|
||||
const r = this.color_[0] / 255.0;
|
||||
const g = this.color_[1] / 255.0;
|
||||
const b = this.color_[2] / 255.0;
|
||||
|
||||
for (let i = 0, ii = data.length; i < ii; i += 4) {
|
||||
data[i] *= r;
|
||||
data[i + 1] *= g;
|
||||
data[i + 2] *= b;
|
||||
}
|
||||
ctx.putImageData(imgData, 0, 0);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Discards event handlers which listen for load completion or errors.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
IconImage.prototype.unlistenImage_ = function() {
|
||||
this.imageListenerKeys_.forEach(unlistenByKey);
|
||||
this.imageListenerKeys_ = null;
|
||||
};
|
||||
export default IconImage;
|
||||
|
||||
Reference in New Issue
Block a user