maplair
Version:
JavaScript library for mobile-friendly interactive maps
180 lines (155 loc) • 6.89 kB
JavaScript
import {Class} from '../../core/Class.js';
import {setOptions, toNumber} from '../../core/Util.js';
import {Point} from '../../geometry/Point.js';
/** @import {PointExpression} from '../../geometry/Point.js' */
/**
* Options of `Icon`.
* @typedef {object} IconOptions
* @property {string} [iconUrl] **(required)** The URL to the icon image (absolute or
* relative to your script path).
* @property {string} [iconRetinaUrl] The URL to a retina sized version of the icon
* image (absolute or relative to your script path). Used for Retina screen devices.
* @property {PointExpression|number} [iconSize] Size of the icon image in pixels (a
* number for square icons).
* @property {PointExpression} [iconAnchor] The coordinates of the "tip" of the icon
* (relative to its top left corner). The icon will be aligned so that this point is at
* the marker's geographical location. Centered by default if size is specified, also
* can be set in CSS with negative margins.
* @property {PointExpression} [popupAnchor=[0, 0]] The coordinates of the point from
* which popups will "open", relative to the icon anchor.
* @property {PointExpression} [tooltipAnchor=[0, 0]] Where tooltips opening to the side
* attach, relative to the icon anchor: `x` is how far to the right (mirrored for tooltips
* opening to the left) and `y` how far down. Tooltips opening to the top or bottom attach
* to the middle of the icon's top or bottom edge, and `center` ones to the middle of the
* icon at the `y` height.
* @property {string} [shadowUrl] The URL to the icon shadow image. If not specified, no
* shadow image will be created.
* @property {string} [shadowRetinaUrl] The URL to a retina sized version of the shadow
* image. Used for Retina screen devices.
* @property {PointExpression|number} [shadowSize] Size of the shadow image in pixels.
* @property {PointExpression} [shadowAnchor] The coordinates of the "tip" of the shadow
* (relative to its top left corner) (the same as iconAnchor if not specified).
* @property {string} [className] A custom class name to assign to both icon and shadow
* images. Empty by default.
* @property {boolean|string} [crossOrigin=false] Whether the crossOrigin attribute will
* be added to the icon images. If a String is provided, all images will have their
* crossOrigin attribute set to the String provided. This is needed if you want to
* access image pixel data. Refer to
* [CORS Settings](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes)
* for valid String values.
*/
/**
* Represents an icon to provide when creating a marker.
*
* `DefaultIcon` extends `Icon` and is the blue icon Maplair uses for markers by default.
*
* @example
* ```js
* const myIcon = new Icon({
* iconUrl: 'my-icon.png',
* iconRetinaUrl: 'my-icon@2x.png',
* iconSize: [38, 95],
* iconAnchor: [22, 94],
* popupAnchor: [-3, -76],
* shadowUrl: 'my-icon-shadow.png',
* shadowRetinaUrl: 'my-icon-shadow@2x.png',
* shadowSize: [68, 95],
* shadowAnchor: [22, 94]
* });
*
* new Marker([50.505, 30.57], {icon: myIcon}).addTo(map);
* ```
*/
export class Icon extends Class {
static defaultOptions = {
popupAnchor: [0, 0],
tooltipAnchor: [0, 0],
crossOrigin: false
};
/**
* Creates an icon instance with the given options.
* @param {IconOptions} [options]
*/
constructor(options) {
super();
setOptions(this, options);
this.callInitHooks(Icon);
}
/**
* Called internally when the icon has to be shown, returns a `<img>` HTML element
* styled according to the options.
* @param {HTMLElement} [oldIcon] The current element of the icon, to be reused if possible.
* @returns {HTMLElement}
*/
createIcon(oldIcon) {
return this._createIcon('icon', oldIcon);
}
/**
* As `createIcon`, but for the shadow beneath it. Returns `null` without a shadow.
* @param {HTMLElement} [oldIcon] The current element of the shadow, to be reused if possible.
* @returns {HTMLElement|null}
*/
createShadow(oldIcon) {
return this._createIcon('shadow', oldIcon);
}
_createIcon(name, oldIcon) {
const src = this._getIconUrl(name);
if (!src) {
if (name === 'icon') {
throw new Error('iconUrl not set in Icon options (see the docs).');
}
return null;
}
const img = this._createImg(src, oldIcon instanceof HTMLImageElement ? oldIcon : null);
this._setIconStyles(img, name);
if (this.options.crossOrigin || this.options.crossOrigin === '') {
img.crossOrigin = this.options.crossOrigin === true ? '' : this.options.crossOrigin;
} else {
img.crossOrigin = null;
}
return img;
}
// The size and anchor of the icon or shadow in pixels; a side that isn't a number, like
// 'auto' in [100, 'auto'], is left to CSS and comes out as NaN.
_getBox(name) {
const options = this.options;
const sizeOption = options[`${name}Size`];
const [width, height] = (typeof sizeOption === 'number' ? [sizeOption, sizeOption] :
Array.isArray(sizeOption) ? sizeOption : [sizeOption?.x, sizeOption?.y]).map(toNumber);
const anchorOption = name === 'shadow' && options.shadowAnchor || options.iconAnchor;
const anchor = Point.validate(anchorOption) ? Point.from(anchorOption) :
{x: Math.round(width / 2), y: Math.round(height / 2)};
return {width, height, anchor};
}
// Where a tooltip opening in the given direction attaches, relative to the icon anchor.
_getTooltipAnchor(direction) {
const {x, y} = Point.from(this.options.tooltipAnchor);
if (direction === 'left') { return new Point(-x, y); }
if (direction === 'center') { return new Point(0, y); }
if (direction !== 'top' && direction !== 'bottom') { return new Point(x, y); }
const {height, anchor} = this._getBox('icon');
return new Point(0, (direction === 'top' ? -anchor.y : height - anchor.y) || 0);
}
_setIconStyles(img, name) {
const options = this.options;
const {width, height, anchor} = this._getBox(name);
img.className = `maplair-marker-${name} ${options.className || ''}`;
// a marker reuses the element of its previous icon, which may have set any of these
const {style} = img;
style.marginLeft = style.marginTop = style.width = style.height = '';
if (!isNaN(anchor.x)) { style.marginLeft = `${-anchor.x}px`; }
if (!isNaN(anchor.y)) { style.marginTop = `${-anchor.y}px`; }
// the origin of the marker rotation: the anchor, which is the top left corner without one
style.transformOrigin = !isNaN(anchor.x) && !isNaN(anchor.y) ? `${anchor.x}px ${anchor.y}px` : '0 0';
if (!isNaN(width)) { style.width = `${width}px`; }
if (!isNaN(height)) { style.height = `${height}px`; }
}
_createImg(src, el) {
el ??= document.createElement('img');
el.src = src;
return el;
}
_getIconUrl(name) {
return window.devicePixelRatio > 1 && this.options[`${name}RetinaUrl`] || this.options[`${name}Url`];
}
}