mapbox-gl
Version:
A WebGL interactive maps library
98 lines (82 loc) • 2.67 kB
JavaScript
// @flow
import {TapRecognizer} from './tap_recognizer.js';
import type Point from '@mapbox/point-geometry';
import type Map from '../map.js';
import type {Handler, HandlerResult} from '../handler.js';
export default class TapZoomHandler implements Handler {
_enabled: boolean;
_active: boolean;
_zoomIn: TapRecognizer;
_zoomOut: TapRecognizer;
constructor() {
this._zoomIn = new TapRecognizer({
numTouches: 1,
numTaps: 2
});
this._zoomOut = new TapRecognizer({
numTouches: 2,
numTaps: 1
});
this.reset();
}
reset() {
this._active = false;
this._zoomIn.reset();
this._zoomOut.reset();
}
// $FlowFixMe[method-unbinding]
touchstart(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
this._zoomIn.touchstart(e, points, mapTouches);
this._zoomOut.touchstart(e, points, mapTouches);
}
// $FlowFixMe[method-unbinding]
touchmove(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
this._zoomIn.touchmove(e, points, mapTouches);
this._zoomOut.touchmove(e, points, mapTouches);
}
// $FlowFixMe[method-unbinding]
touchend(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>): ?HandlerResult {
const zoomInPoint = this._zoomIn.touchend(e, points, mapTouches);
const zoomOutPoint = this._zoomOut.touchend(e, points, mapTouches);
if (zoomInPoint) {
this._active = true;
e.preventDefault();
setTimeout(() => this.reset(), 0);
return {
cameraAnimation: (map: Map) => map.easeTo({
duration: 300,
zoom: map.getZoom() + 1,
around: map.unproject(zoomInPoint)
}, {originalEvent: e})
};
} else if (zoomOutPoint) {
this._active = true;
e.preventDefault();
setTimeout(() => this.reset(), 0);
return {
cameraAnimation: (map: Map) => map.easeTo({
duration: 300,
zoom: map.getZoom() - 1,
around: map.unproject(zoomOutPoint)
}, {originalEvent: e})
};
}
}
// $FlowFixMe[method-unbinding]
touchcancel() {
this.reset();
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled(): boolean {
return this._enabled;
}
isActive(): boolean {
return this._active;
}
}