-
-
Notifications
You must be signed in to change notification settings - Fork 643
/
two_fingers_touch.ts
336 lines (273 loc) · 9.83 KB
/
two_fingers_touch.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import Point from '@mapbox/point-geometry';
import {DOM} from '../../util/dom';
import type {Map} from '../map';
import {Handler, HandlerResult} from '../handler_manager';
/**
* An options object sent to the enable function of some of the handlers
*/
export type AroundCenterOptions = {
/**
* If "center" is passed, map will zoom around the center of map
*/
around: 'center';
}
/**
* The `TwoFingersTouchHandler`s allows the user to zoom, pitch and rotate the map using two fingers
*
*/
abstract class TwoFingersTouchHandler implements Handler {
_enabled?: boolean;
_active?: boolean;
_firstTwoTouches?: [number, number];
_vector?: Point;
_startVector?: Point;
_aroundCenter?: boolean;
/** @internal */
constructor() {
this.reset();
}
reset(): void {
this._active = false;
delete this._firstTwoTouches;
}
abstract _start(points: [Point, Point]): void;
abstract _move(points: [Point, Point], pinchAround: Point | null, e: TouchEvent): HandlerResult | void;
touchstart(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>): void {
//log('touchstart', points, e.target.innerHTML, e.targetTouches.length ? e.targetTouches[0].target.innerHTML: undefined);
if (this._firstTwoTouches || mapTouches.length < 2) return;
this._firstTwoTouches = [
mapTouches[0].identifier,
mapTouches[1].identifier
];
// implemented by child classes
this._start([points[0], points[1]]);
}
touchmove(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>): HandlerResult | void {
if (!this._firstTwoTouches) return;
e.preventDefault();
const [idA, idB] = this._firstTwoTouches;
const a = getTouchById(mapTouches, points, idA);
const b = getTouchById(mapTouches, points, idB);
if (!a || !b) return;
const pinchAround = this._aroundCenter ? null : a.add(b).div(2);
// implemented by child classes
return this._move([a, b], pinchAround, e);
}
touchend(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>): void {
if (!this._firstTwoTouches) return;
const [idA, idB] = this._firstTwoTouches;
const a = getTouchById(mapTouches, points, idA);
const b = getTouchById(mapTouches, points, idB);
if (a && b) return;
if (this._active) DOM.suppressClick();
this.reset();
}
touchcancel(): void {
this.reset();
}
/**
* Enables the "drag to pitch" interaction.
*
* @example
* ```ts
* map.touchPitch.enable();
* ```
*/
enable(options?: AroundCenterOptions | boolean | null): void {
this._enabled = true;
this._aroundCenter = !!options && (options as AroundCenterOptions).around === 'center';
}
/**
* Disables the "drag to pitch" interaction.
*
* @example
* ```ts
* map.touchPitch.disable();
* ```
*/
disable(): void {
this._enabled = false;
this.reset();
}
/**
* Returns a Boolean indicating whether the "drag to pitch" interaction is enabled.
*
* @returns `true` if the "drag to pitch" interaction is enabled.
*/
isEnabled(): boolean {
return !!this._enabled;
}
/**
* Returns a Boolean indicating whether the "drag to pitch" interaction is active, i.e. currently being used.
*
* @returns `true` if the "drag to pitch" interaction is active.
*/
isActive(): boolean {
return !!this._active;
}
}
function getTouchById(mapTouches: Array<Touch>, points: Array<Point>, identifier: number): Point | undefined {
for (let i = 0; i < mapTouches.length; i++) {
if (mapTouches[i].identifier === identifier) return points[i];
}
return undefined;
}
/* ZOOM */
const ZOOM_THRESHOLD = 0.1;
function getZoomDelta(distance: number, lastDistance: number): number {
return Math.log(distance / lastDistance) / Math.LN2;
}
/**
* The `TwoFingersTouchHandler`s allows the user to zoom the map two fingers
*
* @group Handlers
*/
export class TwoFingersTouchZoomHandler extends TwoFingersTouchHandler {
_distance?: number;
_startDistance?: number;
reset() {
super.reset();
delete this._distance;
delete this._startDistance;
}
_start(points: [Point, Point]): void {
this._startDistance = this._distance = points[0].dist(points[1]);
}
_move(points: [Point, Point], pinchAround: Point | null): HandlerResult | void {
const lastDistance = this._distance!;
this._distance = points[0].dist(points[1]);
if (!this._active && Math.abs(getZoomDelta(this._distance, this._startDistance!)) < ZOOM_THRESHOLD) return;
this._active = true;
return {
zoomDelta: getZoomDelta(this._distance, lastDistance),
pinchAround
};
}
}
/* ROTATE */
const ROTATION_THRESHOLD = 25; // pixels along circumference of touch circle
function getBearingDelta(a: Point, b: Point): number {
return a.angleWith(b) * 180 / Math.PI;
}
/**
* The `TwoFingersTouchHandler`s allows the user to rotate the map two fingers
*
* @group Handlers
*/
export class TwoFingersTouchRotateHandler extends TwoFingersTouchHandler {
_minDiameter?: number;
reset(): void {
super.reset();
delete this._minDiameter;
delete this._startVector;
delete this._vector;
}
_start(points: [Point, Point]): void {
this._startVector = this._vector = points[0].sub(points[1]);
this._minDiameter = points[0].dist(points[1]);
}
_move(points: [Point, Point], pinchAround: Point | null, _e: TouchEvent): HandlerResult | void {
const lastVector = this._vector!;
this._vector = points[0].sub(points[1]);
if (!this._active && this._isBelowThreshold(this._vector)) return;
this._active = true;
return {
bearingDelta: getBearingDelta(this._vector, lastVector),
pinchAround
};
}
_isBelowThreshold(vector: Point): boolean {
/*
* The threshold before a rotation actually happens is configured in
* pixels along the circumference of the circle formed by the two fingers.
* This makes the threshold in degrees larger when the fingers are close
* together and smaller when the fingers are far apart.
*
* Use the smallest diameter from the whole gesture to reduce sensitivity
* when pinching in and out.
*/
this._minDiameter = Math.min(this._minDiameter!, vector.mag());
const circumference = Math.PI * this._minDiameter;
const threshold = ROTATION_THRESHOLD / circumference * 360;
const bearingDeltaSinceStart = getBearingDelta(vector, this._startVector!);
return Math.abs(bearingDeltaSinceStart) < threshold;
}
}
/* PITCH */
function isVertical(vector: Point): boolean {
return Math.abs(vector.y) > Math.abs(vector.x);
}
const ALLOWED_SINGLE_TOUCH_TIME = 100;
/**
* The `TwoFingersTouchPitchHandler` allows the user to pitch the map by dragging up and down with two fingers.
*
* @group Handlers
*/
export class TwoFingersTouchPitchHandler extends TwoFingersTouchHandler {
_valid?: boolean;
_firstMove?: number;
_lastPoints?: [Point, Point];
_map: Map;
_currentTouchCount: number = 0;
constructor(map: Map) {
super();
this._map = map;
}
reset(): void {
super.reset();
this._valid = undefined;
delete this._firstMove;
delete this._lastPoints;
}
touchstart(e: TouchEvent, points: Array<Point>, mapTouches: Array<Touch>): void {
super.touchstart(e, points, mapTouches);
this._currentTouchCount = mapTouches.length;
}
_start(points: [Point, Point]): void {
this._lastPoints = points;
if (isVertical(points[0].sub(points[1]))) {
// fingers are more horizontal than vertical
this._valid = false;
}
}
_move(points: [Point, Point], center: Point | null, e: TouchEvent): HandlerResult | void {
// If cooperative gestures is enabled, we need a 3-finger minimum for this gesture to register
if (this._map.cooperativeGestures.isEnabled() && this._currentTouchCount < 3) {
return;
}
const vectorA = points[0].sub(this._lastPoints![0]);
const vectorB = points[1].sub(this._lastPoints![1]);
this._valid = this.gestureBeginsVertically(vectorA, vectorB, e.timeStamp);
if (!this._valid) return;
this._lastPoints = points;
this._active = true;
const yDeltaAverage = (vectorA.y + vectorB.y) / 2;
const degreesPerPixelMoved = -0.5;
return {
pitchDelta: yDeltaAverage * degreesPerPixelMoved
};
}
gestureBeginsVertically(vectorA: Point, vectorB: Point, timeStamp: number): boolean | undefined {
if (this._valid !== undefined) return this._valid;
const threshold = 2;
const movedA = vectorA.mag() >= threshold;
const movedB = vectorB.mag() >= threshold;
// neither finger has moved a meaningful amount, wait
if (!movedA && !movedB) return;
// One finger has moved and the other has not.
// If enough time has passed, decide it is not a pitch.
if (!movedA || !movedB) {
if (this._firstMove === undefined) {
this._firstMove = timeStamp;
}
if (timeStamp - this._firstMove < ALLOWED_SINGLE_TOUCH_TIME) {
// still waiting for a movement from the second finger
return undefined;
} else {
return false;
}
}
const isSameDirection = vectorA.y > 0 === vectorB.y > 0;
return isVertical(vectorA) && isVertical(vectorB) && isSameDirection;
}
}