-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
drag_pan.js
313 lines (271 loc) · 9.37 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
// @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';
_pos: Point;
_previousPos: Point;
_inertia: Array<[number, Point]>;
_lastMoveEvent: MouseEvent | TouchEvent | void;
_frameId: ?TaskID;
/**
* @private
*/
constructor(map: Map) {
this._map = map;
this._el = map.getCanvasContainer();
this._state = 'disabled';
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._state !== 'enabled') return;
if (e.touches.length > 1) 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._previousPos = DOM.mousePos(this._el, e);
this._inertia = [[browser.now(), this._previousPos]];
}
_onMove(e: MouseEvent | TouchEvent) {
this._lastMoveEvent = e;
e.preventDefault();
this._pos = DOM.mousePos(this._el, e);
this._drainInertiaBuffer();
this._inertia.push([browser.now(), this._pos]);
if (this._state === 'pending') {
// we treat the first move event (rather than the mousedown event)
// as the start of the drag
this._state = 'active';
this._fireEvent('dragstart', e);
this._fireEvent('movestart', e);
}
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;
const tr = this._map.transform;
tr.setLocationAtPoint(tr.pointLocation(this._previousPos), this._pos);
this._fireEvent('drag', e);
this._fireEvent('move', e);
this._previousPos = this._pos;
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) {
switch (this._state) {
case 'active':
this._state = 'enabled';
this._unbind();
this._deactivate();
this._inertialPan(e);
break;
case 'pending':
this._state = 'enabled';
this._unbind();
break;
default:
assert(false);
break;
}
}
_onBlur(e: FocusEvent) {
switch (this._state) {
case 'active':
this._state = 'enabled';
this._unbind();
this._deactivate();
this._fireEvent('dragend', e);
this._fireEvent('moveend', e);
break;
case 'pending':
this._state = 'enabled';
this._unbind();
break;
default:
assert(false);
break;
}
}
_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._previousPos;
delete this._pos;
}
_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;