Spaces:
Sleeping
Sleeping
File size: 2,651 Bytes
4a288a7 | 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 | // @flow
import Point from '@mapbox/point-geometry';
import {indexTouches} from './handler_util';
export default class TouchPanHandler {
_enabled: boolean;
_active: boolean;
_touches: { [string | number]: Point };
_minTouches: number;
_clickTolerance: number;
_sum: Point;
constructor(options: { clickTolerance: number }) {
this._minTouches = 1;
this._clickTolerance = options.clickTolerance || 1;
this.reset();
}
reset() {
this._active = false;
this._touches = {};
this._sum = new Point(0, 0);
}
touchstart(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
return this._calculateTransform(e, points, mapTouches);
}
touchmove(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
if (!this._active || mapTouches.length < this._minTouches) return;
e.preventDefault();
return this._calculateTransform(e, points, mapTouches);
}
touchend(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
this._calculateTransform(e, points, mapTouches);
if (this._active && mapTouches.length < this._minTouches) {
this.reset();
}
}
touchcancel() {
this.reset();
}
_calculateTransform(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>) {
if (mapTouches.length > 0) this._active = true;
const touches = indexTouches(mapTouches, points);
const touchPointSum = new Point(0, 0);
const touchDeltaSum = new Point(0, 0);
let touchDeltaCount = 0;
for (const identifier in touches) {
const point = touches[identifier];
const prevPoint = this._touches[identifier];
if (prevPoint) {
touchPointSum._add(point);
touchDeltaSum._add(point.sub(prevPoint));
touchDeltaCount++;
touches[identifier] = point;
}
}
this._touches = touches;
if (touchDeltaCount < this._minTouches || !touchDeltaSum.mag()) return;
const panDelta = touchDeltaSum.div(touchDeltaCount);
this._sum._add(panDelta);
if (this._sum.mag() < this._clickTolerance) return;
const around = touchPointSum.div(touchDeltaCount);
return {
around,
panDelta
};
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
|