Source: bemap-model/bemap-icon.js

/**
 * BeNomad BeMap JavaScript API - Icon class
 */

/**
 * @classdesc
 * Base class for icon.
 * @public
 * @constructor
 * @param {object} options See below the available values.
 * @param {String} options.src Source of the image. It can be an URL or an folder path.
 *   Defaults to {@link bemap.Icon.DEFAULT_SRC}, an inline SVG pin — no network
 *   request, so it also works offline and on a closed network.
 * @param {double} options.anchorX .
 * @param {double} options.anchorY .
 * @param {String} options.anchorXUnits Units of the anchorX.
 * @param {String} options.anchorYUnits Units of the anchorY.
 * @param {float} options.opacity a number between 0 and 1 to define the opacity of the icon. 1 to full opacity and 0 to full transparency.
 */
bemap.Icon = function(options) {
    var opts = options || {};

    /**
     * @type {object}
     * @protected
     */
    this.native = null;

    /**
     * @type {String}
     * @protected
     */
    this.src = opts.src ? opts.src : bemap.Icon.DEFAULT_SRC;

    /**
     * @type {double}
     * @protected
     */
    this.width = opts.width ? opts.width : null;

    /**
     * @type {double}
     * @protected
     */
    this.height = opts.height ? opts.height : null;

    /**
     * @type {double}
     * @protected
     */
    this.anchorX = opts.anchorX ? opts.anchorX : 0.5;

    /**
     * @type {double}
     * @protected
     */
    this.anchorY = opts.anchorY ? opts.anchorY : 1;

    /**
     * @type {String}
     * @protected
     */
    this.anchorXUnits = opts.anchorXUnits ? opts.anchorXUnits : 'fraction';

    /**
     * {String}
     * @protected
     */
    this.anchorYUnits = opts.anchorYUnits ? opts.anchorYUnits : 'fraction';

    /**
     * @type {float}
     * @protected
     */
    this.opacity = opts.opacity ? opts.opacity : 0;

    /**
     * @type {float}
     * @protected
     */
    this.scale = opts.scale ? opts.scale : 1;
};

/**
 * Default marker image, used when no `src` is given.
 *
 * An inline SVG `data:` URI rather than a URL. The previous default pointed at
 * `openlayers.org/en/v3.18.2/examples/data/icon.png` — a third-party host, over
 * plain HTTP, in the example data of a long-abandoned OpenLayers release. On an
 * HTTPS page Chrome upgrades that request and the upgrade fails too, so the
 * marker never appeared and nothing was thrown (BEMAP-1908).
 *
 * A `data:` URI is used deliberately: it needs no network, so it works offline
 * and on a customer's closed network, it sends nothing to a third party, and it
 * adds no file to ship or host. The shape is a 25x41 pin whose tip sits at the
 * bottom centre, matching the `anchorX: 0.5` / `anchorY: 1` defaults above.
 *
 * Spaces and `#` are percent-encoded so the URI stays valid in a CSS context
 * (Leaflet) as well as in an `Image.src` (OpenLayers) and `loadImage`
 * (MapLibre).
 *
 * @public
 * @const
 * @type {String}
 */
bemap.Icon.DEFAULT_SRC =
    "data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='25'%20height='41'" +
    "%20viewBox='0%200%2025%2041'%3E%3Cpath%20d='M12.5,0C5.6,0,0,5.6,0,12.5c0,9.4,12.5,28.5," +
    "12.5,28.5S25,21.9,25,12.5C25,5.6,19.4,0,12.5,0z'%20fill='%232A6EBB'/%3E%3Ccircle%20" +
    "cx='12.5'%20cy='12.5'%20r='4.5'%20fill='%23FFFFFF'/%3E%3C/svg%3E";

/**
 * Return the Source of the image.
 * @public
 * @return {String} Return the source of the image.
 */
bemap.Icon.prototype.getSrc = function() {
    return this.src;
};

/**
 * Set the Source of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setSrc = function(src) {
    this.src = src;
    return this;
};

/**
 * Return the width of the image.
 * @public
 * @return {double} Return the width of the image.
 */
bemap.Icon.prototype.getWidth = function() {
    return this.width;
};

/**
 * Set the width of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setWidth = function(width) {
    this.width = width;
    return this;
};

/**
 * Return the height of the image.
 * @public
 * @return {double} Return the height of the image.
 */
bemap.Icon.prototype.getHeight = function() {
    return this.height;
};

/**
 * Set the height of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setHeight = function(height) {
    this.height = height;
    return this;
};

/**
 * Return the horizontale anchor of the image.
 * @public
 * @return {double} Return the horizontale anchor of the image.
 */
bemap.Icon.prototype.getAnchorX = function() {
    return this.anchorX;
};

/**
 * Set the horizontale anchor of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setAnchorX = function(anchorX) {
    this.anchorX = anchorX;
    return this;
};

/**
 * Return the verticale anchor of the image.
 * @public
 * @return {double} Return the verticale anchor of the image.
 */
bemap.Icon.prototype.getAnchorY = function() {
    return this.anchorY;
};

/**
 * Set the verticale anchor of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setAnchorY = function(anchorY) {
    this.anchorY = anchorY;
    return this;
};

/**
 * Return the units horizontale anchor of the image.
 * @public
 * @return {String} Return the units horizontale anchor of the image.
 */
bemap.Icon.prototype.getAnchorXUnits = function() {
    return this.anchorXUnits;
};

/**
 * Set the horizontale anchor units the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setAnchorXUnits = function(anchorXUnits) {
    this.anchorXUnits = anchorXUnits;
    return this;
};

/**
 * Return the units verticale anchor of the image.
 * @public
 * @return {String} Return the units verticale anchor of the image.
 */
bemap.Icon.prototype.getAnchorYUnits = function() {
    return this.anchorYUnits;
};

/**
 * Set the vertical anchor units the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setAnchorYUnits = function(anchorYUnits) {
    this.anchorYUnits = anchorYUnits;
    return this;
};

/**
 * Return the opacity the image.
 * @public
 * @return {float} Return the opacity of the image.
 */
bemap.Icon.prototype.getOpacity = function() {
    return this.opacity;
};

/**
 * Set the opacity of the image.
 * @public
 * @return {bemap.Icon} Return this.
 */
bemap.Icon.prototype.setOpacity = function(opacity) {
    this.opacity = opacity;
    return this;
};