Hit detect everything with a pixel ratio of 1
This commit is contained in:
@@ -352,11 +352,10 @@ class Icon extends ImageStyle {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image element.
|
||||
*/
|
||||
getHitDetectionImage(pixelRatio) {
|
||||
return this.iconImage_.getHitDetectionImage(pixelRatio);
|
||||
getHitDetectionImage() {
|
||||
return this.iconImage_.getHitDetectionImage();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,9 +28,9 @@ class IconImage extends EventTarget {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object<number, HTMLImageElement|HTMLCanvasElement>}
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
*/
|
||||
this.hitDetectionImage_ = {};
|
||||
this.hitDetectionImage_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -162,27 +162,21 @@ class IconImage extends EventTarget {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLImageElement|HTMLCanvasElement} Image element.
|
||||
*/
|
||||
getHitDetectionImage(pixelRatio) {
|
||||
if (!this.hitDetectionImage_[pixelRatio]) {
|
||||
getHitDetectionImage() {
|
||||
if (!this.hitDetectionImage_) {
|
||||
if (this.isTainted_()) {
|
||||
const usedPixelRatio = this.color_ ? pixelRatio : 1;
|
||||
const width = this.size_[0];
|
||||
const height = this.size_[1];
|
||||
const context = createCanvasContext2D(
|
||||
Math.ceil(width * usedPixelRatio),
|
||||
Math.ceil(height * usedPixelRatio)
|
||||
);
|
||||
context.scale(usedPixelRatio, usedPixelRatio);
|
||||
const context = createCanvasContext2D(width, height);
|
||||
context.fillRect(0, 0, width, height);
|
||||
this.hitDetectionImage_[pixelRatio] = context.canvas;
|
||||
this.hitDetectionImage_ = context.canvas;
|
||||
} else {
|
||||
this.hitDetectionImage_[pixelRatio] = this.image_;
|
||||
this.hitDetectionImage_ = this.image_;
|
||||
}
|
||||
}
|
||||
return this.hitDetectionImage_[pixelRatio];
|
||||
return this.hitDetectionImage_;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -154,10 +154,9 @@ class ImageStyle {
|
||||
|
||||
/**
|
||||
* @abstract
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLCanvasElement|HTMLVideoElement|HTMLImageElement} Image element.
|
||||
*/
|
||||
getHitDetectionImage(pixelRatio) {
|
||||
getHitDetectionImage() {
|
||||
return abstract();
|
||||
}
|
||||
|
||||
|
||||
@@ -79,9 +79,9 @@ class RegularShape extends ImageStyle {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Object<number, HTMLCanvasElement>}
|
||||
* @type {HTMLCanvasElement}
|
||||
*/
|
||||
this.hitDetectionCanvas_ = {};
|
||||
this.hitDetectionCanvas_ = null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -204,16 +204,15 @@ class RegularShape extends ImageStyle {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pixelRatio Pixel ratio.
|
||||
* @return {HTMLCanvasElement} Image element.
|
||||
*/
|
||||
getHitDetectionImage(pixelRatio) {
|
||||
if (!this.hitDetectionCanvas_[pixelRatio || 1]) {
|
||||
getHitDetectionImage() {
|
||||
if (!this.hitDetectionCanvas_) {
|
||||
const renderOptions = this.createRenderOptions();
|
||||
|
||||
this.createHitDetectionCanvas_(renderOptions, pixelRatio || 1);
|
||||
this.createHitDetectionCanvas_(renderOptions);
|
||||
}
|
||||
return this.hitDetectionCanvas_[pixelRatio || 1];
|
||||
return this.hitDetectionCanvas_;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +409,7 @@ class RegularShape extends ImageStyle {
|
||||
const displacement = this.getDisplacement();
|
||||
|
||||
this.hitDetectionImageSize_ = [renderOptions.size, renderOptions.size];
|
||||
this.createHitDetectionCanvas_(renderOptions, 1);
|
||||
this.createHitDetectionCanvas_(renderOptions);
|
||||
|
||||
this.anchor_ = [size / 2 - displacement[0], size / 2 + displacement[1]];
|
||||
this.size_ = [size, size];
|
||||
@@ -488,10 +487,9 @@ class RegularShape extends ImageStyle {
|
||||
/**
|
||||
* @private
|
||||
* @param {RenderOptions} renderOptions Render options.
|
||||
* @param {number} pixelRatio The pixel ratio.
|
||||
*/
|
||||
createHitDetectionCanvas_(renderOptions, pixelRatio) {
|
||||
this.hitDetectionCanvas_[pixelRatio] = this.getImage(pixelRatio);
|
||||
createHitDetectionCanvas_(renderOptions) {
|
||||
this.hitDetectionCanvas_ = this.getImage(1);
|
||||
if (this.fill_) {
|
||||
let color = this.fill_.getColor();
|
||||
|
||||
@@ -509,12 +507,12 @@ class RegularShape extends ImageStyle {
|
||||
// if a transparent fill style is set, create an extra hit-detection image
|
||||
// with a default fill style
|
||||
const context = createCanvasContext2D(
|
||||
renderOptions.size * pixelRatio,
|
||||
renderOptions.size * pixelRatio
|
||||
renderOptions.size,
|
||||
renderOptions.size
|
||||
);
|
||||
this.hitDetectionCanvas_[pixelRatio] = context.canvas;
|
||||
this.hitDetectionCanvas_ = context.canvas;
|
||||
|
||||
this.drawHitDetectionCanvas_(renderOptions, context, 0, 0, pixelRatio);
|
||||
this.drawHitDetectionCanvas_(renderOptions, context, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -525,13 +523,9 @@ class RegularShape extends ImageStyle {
|
||||
* @param {CanvasRenderingContext2D} context The context.
|
||||
* @param {number} x The origin for the symbol (x).
|
||||
* @param {number} y The origin for the symbol (y).
|
||||
* @param {number} pixelRatio The pixel ratio.
|
||||
*/
|
||||
drawHitDetectionCanvas_(renderOptions, context, x, y, pixelRatio) {
|
||||
// reset transform
|
||||
context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
|
||||
|
||||
// then move to (x, y)
|
||||
drawHitDetectionCanvas_(renderOptions, context, x, y) {
|
||||
// move to (x, y)
|
||||
context.translate(x, y);
|
||||
|
||||
context.beginPath();
|
||||
|
||||
Reference in New Issue
Block a user