Replace IconOrigin enum with typedef
This commit is contained in:
@@ -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 IconOrigin from '../style/IconOrigin.js';
|
||||
import ImageState from '../ImageState.js';
|
||||
import LineString from '../geom/LineString.js';
|
||||
import MultiLineString from '../geom/MultiLineString.js';
|
||||
@@ -58,7 +57,7 @@ import {transformGeometryWithOptions} from './Feature.js';
|
||||
* @property {import("../style/Icon.js").IconAnchorUnits} xunits Units of x.
|
||||
* @property {number} y Y coordinate.
|
||||
* @property {import("../style/Icon.js").IconAnchorUnits} yunits Units of Y.
|
||||
* @property {import("../style/IconOrigin.js").default} [origin] Origin.
|
||||
* @property {import("../style/Icon.js").IconOrigin} [origin] Origin.
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -333,7 +332,7 @@ function createStyleDefaults() {
|
||||
|
||||
DEFAULT_IMAGE_STYLE = new Icon({
|
||||
anchor: DEFAULT_IMAGE_STYLE_ANCHOR,
|
||||
anchorOrigin: IconOrigin.BOTTOM_LEFT,
|
||||
anchorOrigin: 'bottom-left',
|
||||
anchorXUnits: DEFAULT_IMAGE_STYLE_ANCHOR_X_UNITS,
|
||||
anchorYUnits: DEFAULT_IMAGE_STYLE_ANCHOR_Y_UNITS,
|
||||
crossOrigin: 'anonymous',
|
||||
@@ -1173,18 +1172,19 @@ function readStyleURL(node) {
|
||||
function readVec2(node) {
|
||||
const xunits = node.getAttribute('xunits');
|
||||
const yunits = node.getAttribute('yunits');
|
||||
/** @type {import('../style/Icon.js').IconOrigin} */
|
||||
let origin;
|
||||
if (xunits !== 'insetPixels') {
|
||||
if (yunits !== 'insetPixels') {
|
||||
origin = IconOrigin.BOTTOM_LEFT;
|
||||
origin = 'bottom-left';
|
||||
} else {
|
||||
origin = IconOrigin.TOP_LEFT;
|
||||
origin = 'top-left';
|
||||
}
|
||||
} else {
|
||||
if (yunits !== 'insetPixels') {
|
||||
origin = IconOrigin.BOTTOM_RIGHT;
|
||||
origin = 'bottom-right';
|
||||
} else {
|
||||
origin = IconOrigin.TOP_RIGHT;
|
||||
origin = 'top-right';
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -1265,7 +1265,8 @@ function iconStyleParser(node, objectStack) {
|
||||
src = DEFAULT_IMAGE_STYLE_SRC;
|
||||
}
|
||||
let anchor, anchorXUnits, anchorYUnits;
|
||||
let anchorOrigin = IconOrigin.BOTTOM_LEFT;
|
||||
/** @type {import('../style/Icon.js').IconOrigin|undefined} */
|
||||
let anchorOrigin = 'bottom-left';
|
||||
const hotSpot = /** @type {Vec2|undefined} */ (object['hotSpot']);
|
||||
if (hotSpot) {
|
||||
anchor = [hotSpot.x, hotSpot.y];
|
||||
@@ -1325,7 +1326,7 @@ function iconStyleParser(node, objectStack) {
|
||||
anchorYUnits: anchorYUnits,
|
||||
crossOrigin: this.crossOrigin_,
|
||||
offset: offset,
|
||||
offsetOrigin: IconOrigin.BOTTOM_LEFT,
|
||||
offsetOrigin: 'bottom-left',
|
||||
rotation: rotation,
|
||||
scale: scale,
|
||||
size: size,
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
};
|
||||
@@ -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 IconOrigin from '../../../../../src/ol/style/IconOrigin.js';
|
||||
import ImageState from '../../../../../src/ol/ImageState.js';
|
||||
import KML, {
|
||||
getDefaultFillStyle,
|
||||
@@ -2429,7 +2428,7 @@ describe('ol.format.KML', function () {
|
||||
if (f.getId() == 1) {
|
||||
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.anchorOrigin_).to.be('bottom-left');
|
||||
expect(imageStyle.anchorXUnits_).to.be('fraction');
|
||||
expect(imageStyle.anchorYUnits_).to.be('fraction');
|
||||
} else {
|
||||
@@ -2438,16 +2437,16 @@ describe('ol.format.KML', function () {
|
||||
expect(imageStyle.anchorXUnits_).to.be('pixels');
|
||||
expect(imageStyle.anchorYUnits_).to.be('pixels');
|
||||
if (f.getId() == 2) {
|
||||
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_LEFT);
|
||||
expect(imageStyle.anchorOrigin_).to.be('bottom-left');
|
||||
}
|
||||
if (f.getId() == 3) {
|
||||
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.BOTTOM_RIGHT);
|
||||
expect(imageStyle.anchorOrigin_).to.be('bottom-right');
|
||||
}
|
||||
if (f.getId() == 4) {
|
||||
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.TOP_LEFT);
|
||||
expect(imageStyle.anchorOrigin_).to.be('top-left');
|
||||
}
|
||||
if (f.getId() == 5) {
|
||||
expect(imageStyle.anchorOrigin_).to.be(IconOrigin.TOP_RIGHT);
|
||||
expect(imageStyle.anchorOrigin_).to.be('top-right');
|
||||
}
|
||||
}
|
||||
expect(imageStyle.getRotation()).to.eql(0);
|
||||
|
||||
Reference in New Issue
Block a user