/
drag_pan.js
394 lines (343 loc) · 12.7 KB
/
drag_pan.js
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// @flow
import DOM from '../../util/dom';
import {bezier, bindAll} from '../../util/util';
import window from '../../util/window';
import browser from '../../util/browser';
import {Event} from '../../util/evented';
import assert from 'assert';
import type Map from '../map';
import type Point from '@mapbox/point-geometry';
import type {TaskID} from '../../util/task_queue';
const inertiaLinearity = 0.3,
inertiaEasing = bezier(0, 0, inertiaLinearity, 1),
inertiaMaxSpeed = 1400, // px/s
inertiaDeceleration = 2500; // px/s^2
/**
* The `DragPanHandler` allows the user to pan the map by clicking and dragging
* the cursor.
*/
class DragPanHandler {
_map: Map;
_el: HTMLElement;
_state: 'disabled' | 'enabled' | 'pending' | 'active';
_startPos: Point;
_mouseDownPos: Point;
_prevPos: Point;
_lastPos: Point;
_startTouch: ?Array<Point>;
_lastTouch: ?Array<Point>;
_lastMoveEvent: MouseEvent | TouchEvent | void;
_inertia: Array<[number, Point]>;
_frameId: ?TaskID;
_clickTolerance: number;
_shouldStart: ?boolean;
/**
* @private
*/
constructor(map: Map, options: {
clickTolerance?: number
}) {
this._map = map;
this._el = map.getCanvasContainer();
this._state = 'disabled';
this._clickTolerance = options.clickTolerance || 1;
bindAll([
'_onMove',
'_onMouseUp',
'_onTouchEnd',
'_onBlur',
'_onDragFrame'
], this);
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is enabled.
*
* @returns {boolean} `true` if the "drag to pan" interaction is enabled.
*/
isEnabled() {
return this._state !== 'disabled';
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is active, i.e. currently being used.
*
* @returns {boolean} `true` if the "drag to pan" interaction is active.
*/
isActive() {
return this._state === 'active';
}
/**
* Enables the "drag to pan" interaction.
*
* @example
* map.dragPan.enable();
*/
enable() {
if (this.isEnabled()) return;
this._el.classList.add('mapboxgl-touch-drag-pan');
this._state = 'enabled';
}
/**
* Disables the "drag to pan" interaction.
*
* @example
* map.dragPan.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._el.classList.remove('mapboxgl-touch-drag-pan');
switch (this._state) {
case 'active':
this._state = 'disabled';
this._unbind();
this._deactivate();
this._fireEvent('dragend');
this._fireEvent('moveend');
break;
case 'pending':
this._state = 'disabled';
this._unbind();
break;
default:
this._state = 'disabled';
break;
}
}
onMouseDown(e: MouseEvent) {
if (this._state !== 'enabled') return;
if (e.ctrlKey || DOM.mouseButton(e) !== 0) return;
// Bind window-level event listeners for mousemove/up events. In the absence of
// the pointer capture API, which is not supported by all necessary platforms,
// window-level event listeners give us the best shot at capturing events that
// fall outside the map canvas element. Use `{capture: true}` for the move event
// to prevent map move events from being fired during a drag.
DOM.addEventListener(window.document, 'mousemove', this._onMove, {capture: true});
DOM.addEventListener(window.document, 'mouseup', this._onMouseUp);
this._start(e);
}
onTouchStart(e: TouchEvent) {
if (!this.isEnabled()) return;
if (e.touches && e.touches.length > 1) { // multi-finger touch
// If we are already dragging (e.g. with one finger) and add another finger,
// keep the handler active but don't attempt to ._start() again
if (this._state === 'pending' || this._state === 'active') return;
}
// Bind window-level event listeners for touchmove/end events. In the absence of
// the pointer capture API, which is not supported by all necessary platforms,
// window-level event listeners give us the best shot at capturing events that
// fall outside the map canvas element. Use `{capture: true}` for the move event
// to prevent map move events from being fired during a drag.
DOM.addEventListener(window.document, 'touchmove', this._onMove, {capture: true, passive: false});
DOM.addEventListener(window.document, 'touchend', this._onTouchEnd);
this._start(e);
}
_start(e: MouseEvent | TouchEvent) {
// Deactivate when the window loses focus. Otherwise if a mouseup occurs when the window
// isn't in focus, dragging will continue even though the mouse is no longer pressed.
window.addEventListener('blur', this._onBlur);
this._state = 'pending';
this._startPos = this._mouseDownPos = this._prevPos = this._lastPos = DOM.mousePos(this._el, e);
this._startTouch = this._lastTouch = (window.TouchEvent && e instanceof window.TouchEvent) ? DOM.touchPos(this._el, e) : null;
this._inertia = [[browser.now(), this._startPos]];
}
_touchesMatch(lastTouch: ?Array<Point>, thisTouch: ?Array<Point>) {
if (!lastTouch || !thisTouch || lastTouch.length !== thisTouch.length) return false;
return lastTouch.every((pos, i) => thisTouch[i] === pos);
}
_onMove(e: MouseEvent | TouchEvent) {
e.preventDefault();
const touchPos = (window.TouchEvent && e instanceof window.TouchEvent) ? DOM.touchPos(this._el, e) : null;
const pos = DOM.mousePos(this._el, e);
const matchesLastPos = touchPos ? this._touchesMatch(this._lastTouch, touchPos) : this._lastPos.equals(pos);
if (matchesLastPos || (this._state === 'pending' && pos.dist(this._mouseDownPos) < this._clickTolerance)) {
return;
}
this._lastMoveEvent = e;
this._lastPos = pos;
this._lastTouch = touchPos;
this._drainInertiaBuffer();
this._inertia.push([browser.now(), this._lastPos]);
if (this._state === 'pending') {
this._state = 'active';
this._shouldStart = true;
}
if (!this._frameId) {
this._frameId = this._map._requestRenderFrame(this._onDragFrame);
}
}
/**
* Called in each render frame while dragging is happening.
* @private
*/
_onDragFrame() {
this._frameId = null;
const e = this._lastMoveEvent;
if (!e) return;
if (this._map.touchZoomRotate.isActive()) {
this._abort(e);
return;
}
if (this._shouldStart) {
// we treat the first drag frame (rather than the mousedown event)
// as the start of the drag
this._fireEvent('dragstart', e);
this._fireEvent('movestart', e);
this._shouldStart = false;
}
if (!this.isActive()) return; // It's possible for the dragstart event to trigger a disable() call (#2419) so we must account for that
const tr = this._map.transform;
tr.setLocationAtPoint(tr.pointLocation(this._prevPos), this._lastPos);
this._fireEvent('drag', e);
this._fireEvent('move', e);
this._prevPos = this._lastPos;
delete this._lastMoveEvent;
}
_onMouseUp(e: MouseEvent) {
if (DOM.mouseButton(e) !== 0) return;
switch (this._state) {
case 'active':
this._state = 'enabled';
DOM.suppressClick();
this._unbind();
this._deactivate();
this._inertialPan(e);
break;
case 'pending':
this._state = 'enabled';
this._unbind();
break;
default:
assert(false);
break;
}
}
_onTouchEnd(e: TouchEvent) {
if (!e.touches || e.touches.length === 0) { // only stop drag if all fingers have been removed
switch (this._state) {
case 'active':
this._state = 'enabled';
this._unbind();
this._deactivate();
this._inertialPan(e);
break;
case 'pending':
this._state = 'enabled';
this._unbind();
break;
case 'enabled':
this._unbind();
break;
default:
assert(false);
break;
}
} else { // some finger(s) still touching the screen
switch (this._state) {
case 'pending':
case 'active':
// we are already dragging; continue
break;
case 'enabled':
// not currently dragging; get ready to start a new drag
this.onTouchStart(e);
break;
default:
assert(false);
break;
}
}
}
_abort(e: FocusEvent | MouseEvent | TouchEvent) {
switch (this._state) {
case 'active':
this._state = 'enabled';
if (!this._shouldStart) { // If we scheduled the dragstart but never fired, nothing to end
// We already started the drag, end it
this._fireEvent('dragend', e);
this._fireEvent('moveend', e);
}
this._unbind();
this._deactivate();
if ((window.TouchEvent && e instanceof window.TouchEvent) && e.touches.length > 1) {
// If there are multiple fingers touching, reattach touchend listener in case
// all but one finger is removed and we need to restart a drag on touchend
DOM.addEventListener(window.document, 'touchend', this._onTouchEnd);
}
break;
case 'pending':
this._state = 'enabled';
this._unbind();
break;
case 'enabled':
this._unbind();
break;
default:
assert(false);
break;
}
}
_onBlur(e: FocusEvent) {
this._abort(e);
}
_unbind() {
DOM.removeEventListener(window.document, 'touchmove', this._onMove, {capture: true, passive: false});
DOM.removeEventListener(window.document, 'touchend', this._onTouchEnd);
DOM.removeEventListener(window.document, 'mousemove', this._onMove, {capture: true});
DOM.removeEventListener(window.document, 'mouseup', this._onMouseUp);
DOM.removeEventListener(window, 'blur', this._onBlur);
}
_deactivate() {
if (this._frameId) {
this._map._cancelRenderFrame(this._frameId);
this._frameId = null;
}
delete this._lastMoveEvent;
delete this._startPos;
delete this._prevPos;
delete this._mouseDownPos;
delete this._lastPos;
delete this._startTouch;
delete this._lastTouch;
delete this._shouldStart;
}
_inertialPan(e: MouseEvent | TouchEvent) {
this._fireEvent('dragend', e);
this._drainInertiaBuffer();
const inertia = this._inertia;
if (inertia.length < 2) {
this._fireEvent('moveend', e);
return;
}
const last = inertia[inertia.length - 1],
first = inertia[0],
flingOffset = last[1].sub(first[1]),
flingDuration = (last[0] - first[0]) / 1000;
if (flingDuration === 0 || last[1].equals(first[1])) {
this._fireEvent('moveend', e);
return;
}
// calculate px/s velocity & adjust for increased initial animation speed when easing out
const velocity = flingOffset.mult(inertiaLinearity / flingDuration);
let speed = velocity.mag(); // px/s
if (speed > inertiaMaxSpeed) {
speed = inertiaMaxSpeed;
velocity._unit()._mult(speed);
}
const duration = speed / (inertiaDeceleration * inertiaLinearity),
offset = velocity.mult(-duration / 2);
this._map.panBy(offset, {
duration: duration * 1000,
easing: inertiaEasing,
noMoveStart: true
}, {originalEvent: e});
}
_fireEvent(type: string, e: *) {
return this._map.fire(new Event(type, e ? {originalEvent: e} : {}));
}
_drainInertiaBuffer() {
const inertia = this._inertia,
now = browser.now(),
cutoff = 160; // msec
while (inertia.length > 0 && now - inertia[0][0] > cutoff) inertia.shift();
}
}
export default DragPanHandler;