maplair
Version:
JavaScript library for mobile-friendly interactive maps
190 lines (156 loc) • 6.43 kB
JavaScript
import {Handler} from '../../core/Handler.js';
import * as DomEvent from '../../dom/DomEvent.js';
import * as DomUtil from '../../dom/DomUtil.js';
import {Point} from '../../geometry/Point.js';
import * as PointerEvents from '../../dom/DomEvent.PointerEvents.js';
/** @import {MaplairMap} from '../Map.js' */
/**
* Map options of `PinchZoomHandler`, touch interaction options.
* @typedef {object} PinchZoomHandlerMapOptions
* @lends MaplairMapOptions
* @property {boolean|'center'} [pinchZoom=true] Whether the map can be zoomed by
* touch-dragging with two fingers. If passed `'center'`, it will zoom to the center of
* the view regardless of where the touch events (fingers) were. Enabled for
* touch-capable web browsers.
* @property {boolean} [bounceAtZoomLimits=true] Set it to false if you don't want the
* map to zoom beyond min/max zoom and then bounce back when pinch-zooming.
* @property {boolean|'center'} [touchZoom] Deprecated, use `pinchZoom` instead.
*/
/**
* Handler of `MaplairMap` for pinch zoom.
* @typedef {object} PinchZoomHandlerMapProperties
* @lends MaplairMap
* @property {PinchZoomHandler} pinchZoom Pinch zoom handler.
* @property {PinchZoomHandler} touchZoom Deprecated, the same as `pinchZoom`.
*/
/**
* Adds pinch zoom to the map on supported mobile browsers, enabled by default, see the
* map option `pinchZoom`.
*/
export class PinchZoomHandler extends Handler {
/**
* @param {...MaplairMap} args The map.
*/
constructor(...args) {
super(...args);
this.callInitHooks(PinchZoomHandler);
}
/**
* Called when the handler is enabled, adds the event hooks.
*/
addHooks() {
this._map._container.classList.add('maplair-touch-zoom');
DomEvent.on(this._map._container, 'pointerdown', this._onPointerStart, this);
}
/**
* Called when the handler is disabled, removes the event hooks.
*/
removeHooks() {
this._map._container.classList.remove('maplair-touch-zoom');
DomEvent.off(this._map._container, 'pointerdown', this._onPointerStart, this);
DomEvent.off(this._map._container, 'pointermove', this._onPointerMove, this);
DomEvent.off(this._map._container, 'pointerup pointercancel', this._onPointerEnd, this);
}
_onPointerStart(e) {
const map = this._map;
const pointers = PointerEvents.getPointers();
if (pointers.length !== 2 || map._animatingZoom || this._zooming) { return; }
const p1 = map.pointerEventToContainerPoint(pointers[0]);
const p2 = map.pointerEventToContainerPoint(pointers[1]);
const {x, y} = DomUtil.getScale(map._container);
// the container doesn't move during a pinch, so keep its client offset and CSS scale to avoid reading layout on every move
this._containerScale = new Point(x, y);
this._containerOrigin = new Point(pointers[0].clientX, pointers[0].clientY)._subtract(p1.scaleBy(this._containerScale));
this._centerPoint = map.getSize()._divideBy(2);
this._startLatLng = map.containerPointToLatLng(this._centerPoint);
if (map.options.pinchZoom !== 'center') {
this._pinchStartLatLng = map.containerPointToLatLng(p1.add(p2)._divideBy(2));
}
this._startDist = p1.distanceTo(p2);
this._startZoom = map.getZoom();
this._moved = false;
this._zooming = true;
map._stop();
DomEvent.on(map._container, 'pointermove', this._onPointerMove, this);
DomEvent.on(map._container, 'pointerup pointercancel', this._onPointerEnd, this);
e.preventDefault();
}
_onPointerMove(e) {
const pointers = PointerEvents.getPointers();
if (pointers.length !== 2 || !this._zooming) { return; }
const map = this._map,
p1 = this._toContainerPoint(pointers[0]),
p2 = this._toContainerPoint(pointers[1]),
scale = p1.distanceTo(p2) / this._startDist;
this._zoom = map.getScaleZoom(scale, this._startZoom);
if (!map.options.bounceAtZoomLimits && (
(this._zoom < map.getMinZoom() && scale < 1) ||
(this._zoom > map.getMaxZoom() && scale > 1))) {
this._zoom = map._limitZoom(this._zoom);
}
if (map.options.pinchZoom === 'center') {
this._center = this._startLatLng;
if (scale === 1) { return; }
} else {
// Get delta from pinch to center, so centerLatLng is delta applied to initial pinchLatLng
const delta = p1._add(p2)._divideBy(2)._subtract(this._centerPoint);
if (scale === 1 && delta.x === 0 && delta.y === 0) { return; }
this._center = map.unproject(map.project(this._pinchStartLatLng, this._zoom).subtract(delta), this._zoom);
}
if (!this._moved) {
map._setUserEvent(e);
map._moveStart(true, false);
this._moved = true;
}
cancelAnimationFrame(this._animRequest);
const moveFn = map._move.bind(map, this._center, this._zoom, {pinch: true}, undefined);
this._animRequest = requestAnimationFrame(moveFn.bind(this));
e.preventDefault();
}
_toContainerPoint(e) {
return new Point(e.clientX, e.clientY)._subtract(this._containerOrigin).unscaleBy(this._containerScale);
}
_onPointerEnd() {
DomEvent.off(this._map._container, 'pointermove', this._onPointerMove, this);
DomEvent.off(this._map._container, 'pointerup pointercancel', this._onPointerEnd, this);
if (!this._moved || !this._zooming) {
this._zooming = false;
return;
}
this._zooming = false;
cancelAnimationFrame(this._animRequest);
// Pinch updates GridLayers' levels only when zoomSnap is off, so zoomSnap becomes noUpdate.
if (this._map._zoomAnimated) {
this._map._animateZoom(this._center, this._map._limitZoom(this._zoom), this._map.options.zoomSnap);
} else {
this._map._resetView(this._center, this._map._limitZoom(this._zoom));
}
}
/**
* Registers this default feature on the map class, called by `MaplairMap` itself.
* @internal
* @param {typeof MaplairMap} MaplairMap
*/
static register(MaplairMap) {
MaplairMap.mergeOptions({
pinchZoom: true,
bounceAtZoomLimits: true
});
MaplairMap.addInitHook('addHandler', 'pinchZoom', PinchZoomHandler);
// Deprecated - Backward compatibility touchZoom
MaplairMap.addInitHook(function () {
this.touchZoom = this.pinchZoom;
if (this.options.touchZoom !== undefined) {
// To be removed in maplair 3
console.warn('Map: touchZoom option is deprecated and will be removed in future versions. Use pinchZoom instead.');
this.options.pinchZoom = this.options.touchZoom;
delete this.options.touchZoom;
}
if (this.options.pinchZoom) {
this.pinchZoom.enable();
} else {
this.pinchZoom.disable();
}
});
}
}