Replace IconOrigin enum with typedef

This commit is contained in:
Maximilian Krög
2022-07-17 03:22:12 +02:00
parent 7ec2725c1e
commit 62e335ceda
4 changed files with 36 additions and 66 deletions
+21 -21
View File
@@ -2,7 +2,6 @@
* @module ol/style/Icon
*/
import EventType from '../events/EventType.js';
import IconOrigin from './IconOrigin.js';
import ImageState from '../ImageState.js';
import ImageStyle from './Image.js';
import {asArray} from '../color.js';
@@ -15,10 +14,15 @@ import {getUid} from '../util.js';
* Anchor unit can be either a fraction of the icon size or in pixels.
*/
/**
* @typedef {'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'} IconOrigin
* Icon origin. One of 'bottom-left', 'bottom-right', 'top-left', 'top-right'.
*/
/**
* @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`,
* @property {IconOrigin} [anchorOrigin='top-left'] Origin of the anchor: `bottom-left`, `bottom-right`,
* `top-left` or `top-right`.
* @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
@@ -37,7 +41,7 @@ import {getUid} from '../util.js';
* @property {Array<number>} [offset=[0, 0]] Offset, which, together with the size and the offset origin, define the
* sub-rectangle to use from the original icon image.
* @property {Array<number>} [displacement=[0,0]] Displacement of the icon.
* @property {import("./IconOrigin.js").default} [offsetOrigin='top-left'] Origin of the offset: `bottom-left`, `bottom-right`,
* @property {IconOrigin} [offsetOrigin='top-left'] Origin of the offset: `bottom-left`, `bottom-right`,
* `top-left` or `top-right`.
* @property {number} [opacity=1] Opacity of the icon.
* @property {number|import("../size.js").Size} [scale=1] Scale.
@@ -108,12 +112,10 @@ class Icon extends ImageStyle {
/**
* @private
* @type {import("./IconOrigin.js").default}
* @type {IconOrigin}
*/
this.anchorOrigin_ =
options.anchorOrigin !== undefined
? options.anchorOrigin
: IconOrigin.TOP_LEFT;
options.anchorOrigin !== undefined ? options.anchorOrigin : 'top-left';
/**
* @private
@@ -192,12 +194,10 @@ class Icon extends ImageStyle {
this.offset_ = options.offset !== undefined ? options.offset : [0, 0];
/**
* @private
* @type {import("./IconOrigin.js").default}
* @type {IconOrigin}
*/
this.offsetOrigin_ =
options.offsetOrigin !== undefined
? options.offsetOrigin
: IconOrigin.TOP_LEFT;
options.offsetOrigin !== undefined ? options.offsetOrigin : 'top-left';
/**
* @private
@@ -270,7 +270,7 @@ class Icon extends ImageStyle {
}
}
if (this.anchorOrigin_ != IconOrigin.TOP_LEFT) {
if (this.anchorOrigin_ != 'top-left') {
if (!size) {
return null;
}
@@ -278,14 +278,14 @@ class Icon extends ImageStyle {
anchor = this.anchor_.slice();
}
if (
this.anchorOrigin_ == IconOrigin.TOP_RIGHT ||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT
this.anchorOrigin_ == 'top-right' ||
this.anchorOrigin_ == 'bottom-right'
) {
anchor[0] = -anchor[0] + size[0];
}
if (
this.anchorOrigin_ == IconOrigin.BOTTOM_LEFT ||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT
this.anchorOrigin_ == 'bottom-left' ||
this.anchorOrigin_ == 'bottom-right'
) {
anchor[1] = -anchor[1] + size[1];
}
@@ -369,7 +369,7 @@ class Icon extends ImageStyle {
}
let offset = this.offset_;
if (this.offsetOrigin_ != IconOrigin.TOP_LEFT) {
if (this.offsetOrigin_ != 'top-left') {
const size = this.getSize();
const iconImageSize = this.iconImage_.getSize();
if (!size || !iconImageSize) {
@@ -377,14 +377,14 @@ class Icon extends ImageStyle {
}
offset = offset.slice();
if (
this.offsetOrigin_ == IconOrigin.TOP_RIGHT ||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT
this.offsetOrigin_ == 'top-right' ||
this.offsetOrigin_ == 'bottom-right'
) {
offset[0] = iconImageSize[0] - size[0] - offset[0];
}
if (
this.offsetOrigin_ == IconOrigin.BOTTOM_LEFT ||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT
this.offsetOrigin_ == 'bottom-left' ||
this.offsetOrigin_ == 'bottom-right'
) {
offset[1] = iconImageSize[1] - size[1] - offset[1];
}
-30
View File
@@ -1,30 +0,0 @@
/**
* @module ol/style/IconOrigin
*/
/**
* Icon origin. One of 'bottom-left', 'bottom-right', 'top-left', 'top-right'.
* @enum {string}
*/
export default {
/**
* Origin is at bottom left
* @api
*/
BOTTOM_LEFT: 'bottom-left',
/**
* Origin is at bottom right
* @api
*/
BOTTOM_RIGHT: 'bottom-right',
/**
* Origin is at top left
* @api
*/
TOP_LEFT: 'top-left',
/**
* Origin is at top right
* @api
*/
TOP_RIGHT: 'top-right',
};