diff --git a/src/DistortableImageOverlay.js b/src/DistortableImageOverlay.js index ed02b10cb..0c3dd1518 100644 --- a/src/DistortableImageOverlay.js +++ b/src/DistortableImageOverlay.js @@ -39,35 +39,7 @@ L.DistortableImageOverlay = L.ImageOverlay.extend({ } // Have to wait for the image to load because need to access its w/h - L.DomEvent.on(this.getElement(), 'load', () => { - this.getPane().appendChild(this.getElement()); - this._initImageDimensions(); - - if (this.options.rotation) { - const units = this.options.rotation.deg >= 0 ? 'deg' : 'rad'; - this.setAngle(this.options.rotation[units], units); - } else { - this.rotation = {deg: 0, rad: 0}; - this._reset(); - } - - /* Initialize default corners if not already set */ - if (!this._corners) { - if (map.options.zoomAnimation && L.Browser.any3d) { - map.on('zoomanim', this._animateZoom, this); - } - } - - /** if there is a featureGroup, only its editable option matters */ - const eventParents = this._eventParents; - if (eventParents) { - this.eP = eventParents[Object.keys(eventParents)[0]]; - if (this.eP.editable) { this.editing.enable(); } - } else { - if (this.editable) { this.editing.enable(); } - this.eP = null; - } - }); + L.DomEvent.on(this.getElement(), 'load', this._initAfterLoading, this); L.DomEvent.on(this.getElement(), 'click', this.select, this); L.DomEvent.on(map, { @@ -91,6 +63,7 @@ L.DistortableImageOverlay = L.ImageOverlay.extend({ }, onRemove(map) { + L.DomEvent.off(this.getElement(), 'load', this._initAfterLoading, this); L.DomEvent.off(this.getElement(), 'click', this.select, this); L.DomEvent.off(map, { singleclickon: this._singleClickListeners, @@ -108,6 +81,36 @@ L.DistortableImageOverlay = L.ImageOverlay.extend({ L.DomEvent.off(this.getElement(), 'mousemove', this.deactivateTooltip, this); }, + _initAfterLoading() { + this.getPane().appendChild(this.getElement()); + this._initImageDimensions(); + + if (this.options.rotation) { + const units = this.options.rotation.deg >= 0 ? 'deg' : 'rad'; + this.setAngle(this.options.rotation[units], units); + } else { + this.rotation = {deg: 0, rad: 0}; + this._reset(); + } + + /* Initialize default corners if not already set */ + if (!this._corners) { + if (map.options.zoomAnimation && L.Browser.any3d) { + map.on('zoomanim', this._animateZoom, this); + } + } + + /** if there is a featureGroup, only its editable option matters */ + const eventParents = this._eventParents; + if (eventParents) { + this.eP = eventParents[Object.keys(eventParents)[0]]; + if (this.eP.editable) { this.editing.enable(); } + } else { + if (this.editable) { this.editing.enable(); } + this.eP = null; + } + }, + _initImageDimensions() { const map = this._map; const originalImageWidth = L.DomUtil.getStyle(this.getElement(), 'width');