Replace IconAnchorUnits enum with typedef

This commit is contained in:
Maximilian Krög
2022-07-17 03:11:41 +02:00
parent 48e57f4d8c
commit 7ec2725c1e
5 changed files with 33 additions and 56 deletions

View File

@@ -5,7 +5,6 @@ import Feature from '../Feature.js';
import Fill from '../style/Fill.js';
import GeometryCollection from '../geom/GeometryCollection.js';
import Icon from '../style/Icon.js';
import IconAnchorUnits from '../style/IconAnchorUnits.js';
import IconOrigin from '../style/IconOrigin.js';
import ImageState from '../ImageState.js';
import LineString from '../geom/LineString.js';
@@ -56,9 +55,9 @@ import {transformGeometryWithOptions} from './Feature.js';
/**
* @typedef {Object} Vec2
* @property {number} x X coordinate.
* @property {import("../style/IconAnchorUnits").default} xunits Units of x.
* @property {import("../style/Icon.js").IconAnchorUnits} xunits Units of x.
* @property {number} y Y coordinate.
* @property {import("../style/IconAnchorUnits").default} yunits Units of Y.
* @property {import("../style/Icon.js").IconAnchorUnits} yunits Units of Y.
* @property {import("../style/IconOrigin.js").default} [origin] Origin.
*/
@@ -95,12 +94,12 @@ const SCHEMA_LOCATION =
'https://developers.google.com/kml/schema/kml22gx.xsd';
/**
* @type {Object<string, import("../style/IconAnchorUnits").default>}
* @type {Object<string, import("../style/Icon.js").IconAnchorUnits>}
*/
const ICON_ANCHOR_UNITS_MAP = {
'fraction': IconAnchorUnits.FRACTION,
'pixels': IconAnchorUnits.PIXELS,
'insetPixels': IconAnchorUnits.PIXELS,
'fraction': 'fraction',
'pixels': 'pixels',
'insetPixels': 'pixels',
};
/**
@@ -211,12 +210,12 @@ export function getDefaultFillStyle() {
let DEFAULT_IMAGE_STYLE_ANCHOR;
/**
* @type {import("../style/IconAnchorUnits").default}
* @type {import("../style/Icon.js").IconAnchorUnits}
*/
let DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS;
/**
* @type {import("../style/IconAnchorUnits").default}
* @type {import("../style/Icon.js").IconAnchorUnits}
*/
let DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS;
@@ -323,9 +322,9 @@ function createStyleDefaults() {
DEFAULT_IMAGE_STYLE_ANCHOR = [20, 2];
DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS = IconAnchorUnits.PIXELS;
DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS = 'pixels';
DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS = IconAnchorUnits.PIXELS;
DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS = 'pixels';
DEFAULT_IMAGE_STYLE_SIZE = [64, 64];
@@ -2627,9 +2626,9 @@ function writeIconStyle(node, style, objectStack) {
if (anchor && (anchor[0] !== size[0] / 2 || anchor[1] !== size[1] / 2)) {
const /** @type {Vec2} */ hotSpot = {
x: anchor[0],
xunits: IconAnchorUnits.PIXELS,
xunits: 'pixels',
y: size[1] - anchor[1],
yunits: IconAnchorUnits.PIXELS,
yunits: 'pixels',
};
properties['hotSpot'] = hotSpot;
}

View File

@@ -3,7 +3,6 @@
*/
import CanvasImmediateRenderer from './Immediate.js';
import IconAnchorUnits from '../../style/IconAnchorUnits.js';
import {Icon} from '../../style.js';
import {clamp} from '../../math.js';
import {createCanvasContext2D} from '../../dom.js';
@@ -104,8 +103,8 @@ export function createHitDetectionImageData(
img: img,
imgSize: imgSize,
anchor: image.getAnchor(),
anchorXUnits: IconAnchorUnits.PIXELS,
anchorYUnits: IconAnchorUnits.PIXELS,
anchorXUnits: 'pixels',
anchorYUnits: 'pixels',
offset: image.getOrigin(),
opacity: 1,
size: image.getSize(),

View File

@@ -2,7 +2,6 @@
* @module ol/style/Icon
*/
import EventType from '../events/EventType.js';
import IconAnchorUnits from './IconAnchorUnits.js';
import IconOrigin from './IconOrigin.js';
import ImageState from '../ImageState.js';
import ImageStyle from './Image.js';
@@ -11,15 +10,20 @@ import {assert} from '../asserts.js';
import {get as getIconImage} from './IconImage.js';
import {getUid} from '../util.js';
/**
* @typedef {'fraction' | 'pixels'} IconAnchorUnits
* Anchor unit can be either a fraction of the icon size or in pixels.
*/
/**
* @typedef {Object} Options
* @property {Array<number>} [anchor=[0.5, 0.5]] Anchor. Default value is the icon center.
* @property {import("./IconOrigin.js").default} [anchorOrigin='top-left'] Origin of the anchor: `bottom-left`, `bottom-right`,
* `top-left` or `top-right`.
* @property {import("./IconAnchorUnits.js").default} [anchorXUnits='fraction'] Units in which the anchor x value is
* @property {IconAnchorUnits} [anchorXUnits='fraction'] Units in which the anchor x value is
* specified. A value of `'fraction'` indicates the x value is a fraction of the icon. A value of `'pixels'` indicates
* the x value in pixels.
* @property {import("./IconAnchorUnits.js").default} [anchorYUnits='fraction'] Units in which the anchor y value is
* @property {IconAnchorUnits} [anchorYUnits='fraction'] Units in which the anchor y value is
* specified. A value of `'fraction'` indicates the y value is a fraction of the icon. A value of `'pixels'` indicates
* the y value in pixels.
* @property {import("../color.js").Color|string} [color] Color to tint the icon. If not specified,
@@ -113,21 +117,17 @@ class Icon extends ImageStyle {
/**
* @private
* @type {import("./IconAnchorUnits.js").default}
* @type {IconAnchorUnits}
*/
this.anchorXUnits_ =
options.anchorXUnits !== undefined
? options.anchorXUnits
: IconAnchorUnits.FRACTION;
options.anchorXUnits !== undefined ? options.anchorXUnits : 'fraction';
/**
* @private
* @type {import("./IconAnchorUnits.js").default}
* @type {IconAnchorUnits}
*/
this.anchorYUnits_ =
options.anchorYUnits !== undefined
? options.anchorYUnits
: IconAnchorUnits.FRACTION;
options.anchorYUnits !== undefined ? options.anchorYUnits : 'fraction';
/**
* @private
@@ -255,17 +255,17 @@ class Icon extends ImageStyle {
anchor = this.anchor_;
const size = this.getSize();
if (
this.anchorXUnits_ == IconAnchorUnits.FRACTION ||
this.anchorYUnits_ == IconAnchorUnits.FRACTION
this.anchorXUnits_ == 'fraction' ||
this.anchorYUnits_ == 'fraction'
) {
if (!size) {
return null;
}
anchor = this.anchor_.slice();
if (this.anchorXUnits_ == IconAnchorUnits.FRACTION) {
if (this.anchorXUnits_ == 'fraction') {
anchor[0] *= size[0];
}
if (this.anchorYUnits_ == IconAnchorUnits.FRACTION) {
if (this.anchorYUnits_ == 'fraction') {
anchor[1] *= size[1];
}
}

View File

@@ -1,20 +0,0 @@
/**
* @module ol/style/IconAnchorUnits
*/
/**
* Icon anchor units. One of 'fraction', 'pixels'.
* @enum {string}
*/
export default {
/**
* Anchor is a fraction
* @api
*/
FRACTION: 'fraction',
/**
* Anchor is in pixels
* @api
*/
PIXELS: 'pixels',
};

View File

@@ -4,7 +4,6 @@ import Fill from '../../../../../src/ol/style/Fill.js';
import GeoJSON from '../../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import Icon from '../../../../../src/ol/style/Icon.js';
import IconAnchorUnits from '../../../../../src/ol/style/IconAnchorUnits.js';
import IconOrigin from '../../../../../src/ol/style/IconOrigin.js';
import ImageState from '../../../../../src/ol/ImageState.js';
import KML, {
@@ -2431,13 +2430,13 @@ describe('ol.format.KML', function () {
expect(imageStyle.anchor_[0]).to.be(0.5);
expect(imageStyle.anchor_[1]).to.be(0.5);
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_LEFT);
expect(imageStyle.anchorXUnits_).to.be(IconAnchorUnits.FRACTION);
expect(imageStyle.anchorYUnits_).to.be(IconAnchorUnits.FRACTION);
expect(imageStyle.anchorXUnits_).to.be('fraction');
expect(imageStyle.anchorYUnits_).to.be('fraction');
} else {
expect(imageStyle.anchor_[0]).to.be(5);
expect(imageStyle.anchor_[1]).to.be(5);
expect(imageStyle.anchorXUnits_).to.be(IconAnchorUnits.PIXELS);
expect(imageStyle.anchorYUnits_).to.be(IconAnchorUnits.PIXELS);
expect(imageStyle.anchorXUnits_).to.be('pixels');
expect(imageStyle.anchorYUnits_).to.be('pixels');
if (f.getId() == 2) {
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_LEFT);
}