maplair
Version:
JavaScript library for mobile-friendly interactive maps
87 lines (86 loc) • 2.69 kB
JavaScript
import { setOptions } from "../core/Util.js";
import { off } from "../dom/DomEvent.js";
import { create, setTransform } from "../dom/DomUtil.js";
import { Bounds } from "../geometry/Bounds.js";
import { Layer } from "./Layer.js";
//#region src/layer/BlanketOverlay.js
var BlanketOverlay = class BlanketOverlay extends Layer {
static {
this.setDefaultOptions({
padding: .1,
continuous: false
});
}
constructor(options) {
super();
setOptions(this, options);
this.callInitHooks(BlanketOverlay);
}
onAdd() {
if (!this._container) {
this._initContainer();
this._container.classList.add("maplair-zoom-animated");
}
this.getPane().appendChild(this._container);
this._resizeContainer();
this._onMoveEnd();
}
onRemove() {
this._destroyContainer();
}
getEvents() {
const events = {
viewreset: this._reset,
zoom: this._onZoom,
moveend: this._onMoveEnd,
resize: this._resizeContainer,
zoomend: this._onZoomEnd
};
if (this._zoomAnimated) events.zoomanim = this._onAnimZoom;
if (this.options.continuous) events.move = this._onMoveEnd;
return events;
}
_onAnimZoom(ev) {
this._updateTransform(ev.center, ev.zoom);
}
_onZoom() {
this._updateTransform(this._map.getCenter(), this._map.getZoom());
}
_updateTransform(center, zoom) {
const scale = this._map.getZoomScale(zoom, this._zoom), viewHalf = this._map.getSize().multiplyBy(.5 + this.options.padding), currentCenterPoint = this._map.project(this._center, zoom), topLeftOffset = viewHalf.multiplyBy(-scale)._add(currentCenterPoint)._subtract(this._map._getNewPixelOrigin(center, zoom))._round();
setTransform(this._container, topLeftOffset, scale);
}
_onMoveEnd(ev) {
const p = this.options.padding, size = this._map.getSize(), min = this._map.containerPointToLayerPoint(size.multiplyBy(-p)).round();
this._bounds = new Bounds(min, min.add(size.multiplyBy(1 + p * 2)).round());
this._center = this._map.getCenter();
this._zoom = this._map.getZoom();
this._updateTransform(this._center, this._zoom);
this._onSettled(ev);
}
_reset() {
this._onSettled();
this._updateTransform(this._center, this._zoom);
this._onViewReset();
}
_initContainer() {
this._container = create("div");
}
_destroyContainer() {
off(this._container);
this._container.remove();
delete this._container;
}
_resizeContainer() {
const p = this.options.padding, size = this._map.getSize().multiplyBy(1 + p * 2).round();
this._container.style.width = `${size.x}px`;
this._container.style.height = `${size.y}px`;
return size;
}
_onZoomEnd() {}
_onViewReset() {}
_onSettled() {}
};
//#endregion
export { BlanketOverlay };
//# sourceMappingURL=BlanketOverlay.js.map