-
-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathdrag_pan.ts
111 lines (103 loc) · 2.9 KB
/
drag_pan.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
import type {MousePanHandler} from '../mouse';
import type {TouchPanHandler} from './../touch_pan';
/**
* A {@link DragPanHandler} options object
*/
export type DragPanOptions = {
/**
* factor used to scale the drag velocity
* @defaultValue 0
*/
linearity?: number;
/**
* easing function applied to `map.panTo` when applying the drag.
* @param t - the easing function
* @defaultValue bezier(0, 0, 0.3, 1)
*/
easing?: (t: number) => number;
/**
* the maximum value of the drag velocity.
* @defaultValue 1400
*/
deceleration?: number;
/**
* the rate at which the speed reduces after the pan ends.
* @defaultValue 2500
*/
maxSpeed?: number;
/**
* the buttons that can be used to drag the map.
* @defaultValue [0]
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
*/
buttons?: number[];
};
/**
* The `DragPanHandler` allows the user to pan the map by clicking and dragging
* the cursor.
*
* @group Handlers
*/
export class DragPanHandler {
_el: HTMLElement;
_mousePan: MousePanHandler;
_touchPan: TouchPanHandler;
_inertiaOptions: DragPanOptions | boolean;
/** @internal */
constructor(el: HTMLElement, mousePan: MousePanHandler, touchPan: TouchPanHandler) {
this._el = el;
this._mousePan = mousePan;
this._touchPan = touchPan;
}
/**
* Enables the "drag to pan" interaction.
*
* @param options - Options object
* @example
* ```ts
* map.dragPan.enable();
* map.dragPan.enable({
* linearity: 0.3,
* easing: bezier(0, 0, 0.3, 1),
* maxSpeed: 1400,
* deceleration: 2500,
* buttons: [0,1] // left and middle mouse buttons
* });
* ```
*/
enable(options?: DragPanOptions | boolean) {
this._inertiaOptions = options || {};
this._mousePan.enable(this._inertiaOptions);
this._touchPan.enable();
this._el.classList.add('maplibregl-touch-drag-pan');
}
/**
* Disables the "drag to pan" interaction.
*
* @example
* ```ts
* map.dragPan.disable();
* ```
*/
disable() {
this._mousePan.disable();
this._touchPan.disable();
this._el.classList.remove('maplibregl-touch-drag-pan');
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is enabled.
*
* @returns `true` if the "drag to pan" interaction is enabled.
*/
isEnabled() {
return this._mousePan.isEnabled() && this._touchPan.isEnabled();
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is active, i.e. currently being used.
*
* @returns `true` if the "drag to pan" interaction is active.
*/
isActive() {
return this._mousePan.isActive() || this._touchPan.isActive();
}
}