# Code coverage report for src/layer/ImageOverlay.js

## Statements: 4.55% (2 / 44) Branches: 0% (0 / 16) Functions: 0% (0 / 13) Lines: 4.55% (2 / 44)

All files » src/layer/ » ImageOverlay.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133

    1

    1

     /*
     * L.ImageOverlay is used to overlay images over the map (to specific geographical bounds).
     */

    L.ImageOverlay = L.Class.extend({
    	includes: L.Mixin.Events,

    	options: {
    		opacity: 1
    	},

    	initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
    		this._url = url;
    		this._bounds = L.latLngBounds(bounds);

    		L.setOptions(this, options);
    	},

    	onAdd: function (map) {
    		this._map = map;

    		if (!this._image) {
    			this._initImage();
    		}

    		map._panes.overlayPane.appendChild(this._image);

    		map.on('viewreset', this._reset, this);

    		if (map.options.zoomAnimation && L.Browser.any3d) {
    			map.on('zoomanim', this._animateZoom, this);
    		}

    		this._reset();
    	},

    	onRemove: function (map) {
    		map.getPanes().overlayPane.removeChild(this._image);

    		map.off('viewreset', this._reset, this);

    		if (map.options.zoomAnimation) {
    			map.off('zoomanim', this._animateZoom, this);
    		}
    	},

    	addTo: function (map) {
    		map.addLayer(this);
    		return this;
    	},

    	setOpacity: function (opacity) {
    		this.options.opacity = opacity;
    		this._updateOpacity();
    		return this;
    	},

    	// TODO remove bringToFront/bringToBack duplication from TileLayer/Path
    	bringToFront: function () {
    		if (this._image) {
    			this._map._panes.overlayPane.appendChild(this._image);
    		}
    		return this;
    	},

    	bringToBack: function () {
    		var pane = this._map._panes.overlayPane;
    		if (this._image) {
    			pane.insertBefore(this._image, pane.firstChild);
    		}
    		return this;
    	},

    	_initImage: function () {
    		this._image = L.DomUtil.create('img', 'leaflet-image-layer');

    		if (this._map.options.zoomAnimation && L.Browser.any3d) {
    			L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
    		} else {
    			L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
    		}

    		this._updateOpacity();

    		//TODO createImage util method to remove duplication
    		L.extend(this._image, {
    			galleryimg: 'no',
    			onselectstart: L.Util.falseFn,
    			onmousemove: L.Util.falseFn,
    			onload: L.bind(this._onImageLoad, this),
    			src: this._url
    		});
    	},

    	_animateZoom: function (e) {
    		var map = this._map,
    		    image = this._image,
    		    scale = map.getZoomScale(e.zoom),
    		    nw = this._bounds.getNorthWest(),
    		    se = this._bounds.getSouthEast(),

    		    topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
    		    size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
    		    origin = topLeft._add(size._multiplyBy((1 / 2) * (1 - 1 / scale)));

    		image.style[L.DomUtil.TRANSFORM] =
    		        L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
    	},

    	_reset: function () {
    		var image   = this._image,
    		    topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
    		    size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);

    		L.DomUtil.setPosition(image, topLeft);

    		image.style.width  = size.x + 'px';
    		image.style.height = size.y + 'px';
    	},

    	_onImageLoad: function () {
    		this.fire('load');
    	},

    	_updateOpacity: function () {
    		L.DomUtil.setOpacity(this._image, this.options.opacity);
    	}
    });

    L.imageOverlay = function (url, bounds, options) {
    	return new L.ImageOverlay(url, bounds, options);
    };