react-ui-animate 2.0.0-rc.1 → 2.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -3
- package/LICENSE +21 -21
- package/README.md +115 -115
- package/dist/animation/animationType.d.ts +8 -0
- package/dist/animation/getInitialConfig.d.ts +2 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -1
- package/package.json +49 -49
- package/rollup.config.js +18 -18
- package/src/animation/animationType.ts +17 -9
- package/src/animation/getInitialConfig.ts +61 -30
- package/src/animation/index.ts +9 -9
- package/src/animation/interpolation.ts +24 -24
- package/src/animation/modules.tsx +105 -105
- package/src/animation/useAnimatedValue.ts +62 -62
- package/src/animation/useMountedValue.ts +66 -66
- package/src/gestures/controllers/DragGesture.ts +177 -177
- package/src/gestures/controllers/Gesture.ts +54 -54
- package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
- package/src/gestures/controllers/ScrollGesture.ts +107 -107
- package/src/gestures/controllers/WheelGesture.ts +123 -123
- package/src/gestures/controllers/index.ts +4 -4
- package/src/gestures/eventAttacher.ts +67 -67
- package/src/gestures/hooks/index.ts +5 -5
- package/src/gestures/hooks/useDrag.ts +14 -14
- package/src/gestures/hooks/useGesture.ts +38 -38
- package/src/gestures/hooks/useMouseMove.ts +11 -11
- package/src/gestures/hooks/useRecognizer.ts +59 -59
- package/src/gestures/hooks/useScroll.ts +11 -11
- package/src/gestures/hooks/useWheel.ts +11 -11
- package/src/gestures/index.ts +2 -2
- package/src/gestures/math.ts +120 -120
- package/src/gestures/types.ts +49 -49
- package/src/gestures/withDefault.ts +3 -3
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useMeasure.ts +133 -133
- package/src/hooks/useOutsideClick.ts +36 -36
- package/src/hooks/useWindowDimension.ts +59 -59
- package/src/index.ts +5 -4
- package/src/utils/delay.ts +9 -9
- package/src/utils/index.ts +2 -2
- package/src/utils/isDefined.ts +4 -4
- package/tsconfig.json +25 -25
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
import { attachEvents } from '../eventAttacher';
|
|
2
|
-
import { Vector2 } from '../types';
|
|
3
|
-
import { clamp } from '../math';
|
|
4
|
-
import { withDefault } from '../withDefault';
|
|
5
|
-
import { Gesture } from './Gesture';
|
|
6
|
-
|
|
7
|
-
export class DragGesture extends Gesture {
|
|
8
|
-
movementStart: Vector2 = withDefault(0, 0);
|
|
9
|
-
initialMovement: Vector2 = withDefault(0, 0);
|
|
10
|
-
movement: Vector2 = withDefault(0, 0);
|
|
11
|
-
previousMovement: Vector2 = withDefault(0, 0);
|
|
12
|
-
translation: Vector2 = withDefault(0, 0);
|
|
13
|
-
offset: Vector2 = withDefault(0, 0);
|
|
14
|
-
velocity: Vector2 = withDefault(0, 0);
|
|
15
|
-
|
|
16
|
-
// @override
|
|
17
|
-
// initialize the events
|
|
18
|
-
_initEvents() {
|
|
19
|
-
if (this.targetElement || this.targetElements.length > 0) {
|
|
20
|
-
this._subscribe = attachEvents(
|
|
21
|
-
[window],
|
|
22
|
-
[
|
|
23
|
-
['mousedown', this.pointerDown.bind(this)],
|
|
24
|
-
['mousemove', this.pointerMove.bind(this)],
|
|
25
|
-
['mouseup', this.pointerUp.bind(this)],
|
|
26
|
-
['touchstart', this.pointerDown.bind(this), { passive: false }],
|
|
27
|
-
['touchmove', this.pointerMove.bind(this), { passive: false }],
|
|
28
|
-
['touchend', this.pointerUp.bind(this)],
|
|
29
|
-
]
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// @override - cancel events
|
|
35
|
-
// we only canceled down and move events because mouse up
|
|
36
|
-
// will not be triggered
|
|
37
|
-
_cancelEvents() {
|
|
38
|
-
if (this._subscribe) {
|
|
39
|
-
this._subscribe(['mousedown', 'mousemove', 'touchstart', 'touchmove']);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
_handleCallback() {
|
|
44
|
-
if (this.callback) {
|
|
45
|
-
this.callback({
|
|
46
|
-
args: [this.currentIndex],
|
|
47
|
-
down: this.isActive,
|
|
48
|
-
movementX: this.movement.x,
|
|
49
|
-
movementY: this.movement.y,
|
|
50
|
-
offsetX: this.translation.x,
|
|
51
|
-
offsetY: this.translation.y,
|
|
52
|
-
velocityX: this.velocity.x,
|
|
53
|
-
velocityY: this.velocity.y,
|
|
54
|
-
distanceX: Math.abs(this.movement.x),
|
|
55
|
-
distanceY: Math.abs(this.movement.y),
|
|
56
|
-
directionX: Math.sign(this.movement.x),
|
|
57
|
-
directionY: Math.sign(this.movement.y),
|
|
58
|
-
cancel: () => {
|
|
59
|
-
this._cancelEvents();
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
pointerDown(e: any) {
|
|
66
|
-
if (e.type === 'touchstart') {
|
|
67
|
-
this.movementStart = {
|
|
68
|
-
x: e.touches[0].clientX,
|
|
69
|
-
y: e.touches[0].clientY,
|
|
70
|
-
};
|
|
71
|
-
} else {
|
|
72
|
-
this.movementStart = { x: e.clientX, y: e.clientY };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.movement = { x: 0, y: 0 };
|
|
76
|
-
this.offset = { x: this.translation.x, y: this.translation.y };
|
|
77
|
-
this.previousMovement = { x: 0, y: 0 };
|
|
78
|
-
this.velocity = { x: 0, y: 0 };
|
|
79
|
-
|
|
80
|
-
// find current selected element
|
|
81
|
-
const currElem = this.targetElements.find((elem: any) => elem === e.target);
|
|
82
|
-
|
|
83
|
-
if (e.target === this.targetElement || currElem) {
|
|
84
|
-
this.isActive = true;
|
|
85
|
-
e.preventDefault();
|
|
86
|
-
|
|
87
|
-
// set args
|
|
88
|
-
if (currElem) {
|
|
89
|
-
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// if initial function is defined then call it to get initial movementX and movementY
|
|
93
|
-
// if only select to bounded draggable element
|
|
94
|
-
const initial = this.config?.initial && this.config.initial();
|
|
95
|
-
const initialMovementX = initial?.movementX;
|
|
96
|
-
const initialMovementY = initial?.movementY;
|
|
97
|
-
|
|
98
|
-
this.initialMovement = {
|
|
99
|
-
x: initialMovementX ?? 0,
|
|
100
|
-
y: initialMovementY ?? 0,
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
this.movement = {
|
|
104
|
-
x: this.initialMovement.x,
|
|
105
|
-
y: this.initialMovement.y,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
this.previousMovement = {
|
|
109
|
-
x: this.initialMovement.x,
|
|
110
|
-
y: this.initialMovement.y,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
this._handleCallback();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
pointerMove(e: any) {
|
|
118
|
-
if (this.isActive) {
|
|
119
|
-
e.preventDefault();
|
|
120
|
-
const now = Date.now();
|
|
121
|
-
const deltaTime = clamp(now - this.lastTimeStamp, 0.1, 64);
|
|
122
|
-
this.lastTimeStamp = now;
|
|
123
|
-
|
|
124
|
-
const t = deltaTime / 1000;
|
|
125
|
-
|
|
126
|
-
if (e.type === 'touchmove') {
|
|
127
|
-
this.movement = {
|
|
128
|
-
x:
|
|
129
|
-
this.initialMovement.x +
|
|
130
|
-
(e.touches[0].clientX - this.movementStart.x),
|
|
131
|
-
y:
|
|
132
|
-
this.initialMovement.y +
|
|
133
|
-
(e.touches[0].clientY - this.movementStart.y),
|
|
134
|
-
};
|
|
135
|
-
} else {
|
|
136
|
-
this.movement = {
|
|
137
|
-
x: this.initialMovement.x + (e.clientX - this.movementStart.x),
|
|
138
|
-
y: this.initialMovement.y + (e.clientY - this.movementStart.y),
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
this.translation = {
|
|
143
|
-
x: this.offset.x + this.movement.x,
|
|
144
|
-
y: this.offset.y + this.movement.y,
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
this.velocity = {
|
|
148
|
-
x: clamp(
|
|
149
|
-
(this.movement.x - this.previousMovement.x) / t / 1000,
|
|
150
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
151
|
-
Gesture._VELOCITY_LIMIT
|
|
152
|
-
),
|
|
153
|
-
y: clamp(
|
|
154
|
-
(this.movement.y - this.previousMovement.y) / t / 1000,
|
|
155
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
156
|
-
Gesture._VELOCITY_LIMIT
|
|
157
|
-
),
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
this.previousMovement = {
|
|
161
|
-
x: this.movement.x,
|
|
162
|
-
y: this.movement.y,
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
this._handleCallback();
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
pointerUp() {
|
|
170
|
-
if (this.isActive) {
|
|
171
|
-
this.isActive = false;
|
|
172
|
-
this._handleCallback();
|
|
173
|
-
this._cancelEvents();
|
|
174
|
-
this._initEvents();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
1
|
+
import { attachEvents } from '../eventAttacher';
|
|
2
|
+
import { Vector2 } from '../types';
|
|
3
|
+
import { clamp } from '../math';
|
|
4
|
+
import { withDefault } from '../withDefault';
|
|
5
|
+
import { Gesture } from './Gesture';
|
|
6
|
+
|
|
7
|
+
export class DragGesture extends Gesture {
|
|
8
|
+
movementStart: Vector2 = withDefault(0, 0);
|
|
9
|
+
initialMovement: Vector2 = withDefault(0, 0);
|
|
10
|
+
movement: Vector2 = withDefault(0, 0);
|
|
11
|
+
previousMovement: Vector2 = withDefault(0, 0);
|
|
12
|
+
translation: Vector2 = withDefault(0, 0);
|
|
13
|
+
offset: Vector2 = withDefault(0, 0);
|
|
14
|
+
velocity: Vector2 = withDefault(0, 0);
|
|
15
|
+
|
|
16
|
+
// @override
|
|
17
|
+
// initialize the events
|
|
18
|
+
_initEvents() {
|
|
19
|
+
if (this.targetElement || this.targetElements.length > 0) {
|
|
20
|
+
this._subscribe = attachEvents(
|
|
21
|
+
[window],
|
|
22
|
+
[
|
|
23
|
+
['mousedown', this.pointerDown.bind(this)],
|
|
24
|
+
['mousemove', this.pointerMove.bind(this)],
|
|
25
|
+
['mouseup', this.pointerUp.bind(this)],
|
|
26
|
+
['touchstart', this.pointerDown.bind(this), { passive: false }],
|
|
27
|
+
['touchmove', this.pointerMove.bind(this), { passive: false }],
|
|
28
|
+
['touchend', this.pointerUp.bind(this)],
|
|
29
|
+
]
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// @override - cancel events
|
|
35
|
+
// we only canceled down and move events because mouse up
|
|
36
|
+
// will not be triggered
|
|
37
|
+
_cancelEvents() {
|
|
38
|
+
if (this._subscribe) {
|
|
39
|
+
this._subscribe(['mousedown', 'mousemove', 'touchstart', 'touchmove']);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_handleCallback() {
|
|
44
|
+
if (this.callback) {
|
|
45
|
+
this.callback({
|
|
46
|
+
args: [this.currentIndex],
|
|
47
|
+
down: this.isActive,
|
|
48
|
+
movementX: this.movement.x,
|
|
49
|
+
movementY: this.movement.y,
|
|
50
|
+
offsetX: this.translation.x,
|
|
51
|
+
offsetY: this.translation.y,
|
|
52
|
+
velocityX: this.velocity.x,
|
|
53
|
+
velocityY: this.velocity.y,
|
|
54
|
+
distanceX: Math.abs(this.movement.x),
|
|
55
|
+
distanceY: Math.abs(this.movement.y),
|
|
56
|
+
directionX: Math.sign(this.movement.x),
|
|
57
|
+
directionY: Math.sign(this.movement.y),
|
|
58
|
+
cancel: () => {
|
|
59
|
+
this._cancelEvents();
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
pointerDown(e: any) {
|
|
66
|
+
if (e.type === 'touchstart') {
|
|
67
|
+
this.movementStart = {
|
|
68
|
+
x: e.touches[0].clientX,
|
|
69
|
+
y: e.touches[0].clientY,
|
|
70
|
+
};
|
|
71
|
+
} else {
|
|
72
|
+
this.movementStart = { x: e.clientX, y: e.clientY };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.movement = { x: 0, y: 0 };
|
|
76
|
+
this.offset = { x: this.translation.x, y: this.translation.y };
|
|
77
|
+
this.previousMovement = { x: 0, y: 0 };
|
|
78
|
+
this.velocity = { x: 0, y: 0 };
|
|
79
|
+
|
|
80
|
+
// find current selected element
|
|
81
|
+
const currElem = this.targetElements.find((elem: any) => elem === e.target);
|
|
82
|
+
|
|
83
|
+
if (e.target === this.targetElement || currElem) {
|
|
84
|
+
this.isActive = true;
|
|
85
|
+
e.preventDefault();
|
|
86
|
+
|
|
87
|
+
// set args
|
|
88
|
+
if (currElem) {
|
|
89
|
+
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// if initial function is defined then call it to get initial movementX and movementY
|
|
93
|
+
// if only select to bounded draggable element
|
|
94
|
+
const initial = this.config?.initial && this.config.initial();
|
|
95
|
+
const initialMovementX = initial?.movementX;
|
|
96
|
+
const initialMovementY = initial?.movementY;
|
|
97
|
+
|
|
98
|
+
this.initialMovement = {
|
|
99
|
+
x: initialMovementX ?? 0,
|
|
100
|
+
y: initialMovementY ?? 0,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
this.movement = {
|
|
104
|
+
x: this.initialMovement.x,
|
|
105
|
+
y: this.initialMovement.y,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
this.previousMovement = {
|
|
109
|
+
x: this.initialMovement.x,
|
|
110
|
+
y: this.initialMovement.y,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
this._handleCallback();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
pointerMove(e: any) {
|
|
118
|
+
if (this.isActive) {
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
const now = Date.now();
|
|
121
|
+
const deltaTime = clamp(now - this.lastTimeStamp, 0.1, 64);
|
|
122
|
+
this.lastTimeStamp = now;
|
|
123
|
+
|
|
124
|
+
const t = deltaTime / 1000;
|
|
125
|
+
|
|
126
|
+
if (e.type === 'touchmove') {
|
|
127
|
+
this.movement = {
|
|
128
|
+
x:
|
|
129
|
+
this.initialMovement.x +
|
|
130
|
+
(e.touches[0].clientX - this.movementStart.x),
|
|
131
|
+
y:
|
|
132
|
+
this.initialMovement.y +
|
|
133
|
+
(e.touches[0].clientY - this.movementStart.y),
|
|
134
|
+
};
|
|
135
|
+
} else {
|
|
136
|
+
this.movement = {
|
|
137
|
+
x: this.initialMovement.x + (e.clientX - this.movementStart.x),
|
|
138
|
+
y: this.initialMovement.y + (e.clientY - this.movementStart.y),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.translation = {
|
|
143
|
+
x: this.offset.x + this.movement.x,
|
|
144
|
+
y: this.offset.y + this.movement.y,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
this.velocity = {
|
|
148
|
+
x: clamp(
|
|
149
|
+
(this.movement.x - this.previousMovement.x) / t / 1000,
|
|
150
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
151
|
+
Gesture._VELOCITY_LIMIT
|
|
152
|
+
),
|
|
153
|
+
y: clamp(
|
|
154
|
+
(this.movement.y - this.previousMovement.y) / t / 1000,
|
|
155
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
156
|
+
Gesture._VELOCITY_LIMIT
|
|
157
|
+
),
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
this.previousMovement = {
|
|
161
|
+
x: this.movement.x,
|
|
162
|
+
y: this.movement.y,
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
this._handleCallback();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
pointerUp() {
|
|
170
|
+
if (this.isActive) {
|
|
171
|
+
this.isActive = false;
|
|
172
|
+
this._handleCallback();
|
|
173
|
+
this._cancelEvents();
|
|
174
|
+
this._initEvents();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
export class Gesture {
|
|
2
|
-
currentIndex?: number;
|
|
3
|
-
lastTimeStamp: number = Date.now();
|
|
4
|
-
isActive: boolean = false;
|
|
5
|
-
targetElement?: HTMLElement; // represents the bounded element
|
|
6
|
-
targetElements: Array<HTMLElement> = []; // represents the bounded elements
|
|
7
|
-
config?: any;
|
|
8
|
-
callback?: <T>(event: T) => void;
|
|
9
|
-
_subscribe?: (eventKeys?: Array<string>) => void;
|
|
10
|
-
static _VELOCITY_LIMIT: number = 20;
|
|
11
|
-
|
|
12
|
-
// it must be overridden by other child classes
|
|
13
|
-
_initEvents() {}
|
|
14
|
-
|
|
15
|
-
// cancel events
|
|
16
|
-
// we only canceled down and move events because mouse up
|
|
17
|
-
// will not be triggered
|
|
18
|
-
_cancelEvents() {
|
|
19
|
-
if (this._subscribe) {
|
|
20
|
-
this._subscribe();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// re-apply new callback
|
|
25
|
-
applyCallback(callback: <T>(event: T) => void) {
|
|
26
|
-
this.callback = callback;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// apply gesture
|
|
30
|
-
applyGesture({
|
|
31
|
-
targetElement,
|
|
32
|
-
targetElements,
|
|
33
|
-
callback,
|
|
34
|
-
config,
|
|
35
|
-
}: {
|
|
36
|
-
targetElement?: any;
|
|
37
|
-
targetElements?: any;
|
|
38
|
-
callback: <T>(event: T) => void;
|
|
39
|
-
config?: any;
|
|
40
|
-
}) {
|
|
41
|
-
this.targetElement = targetElement;
|
|
42
|
-
this.targetElements = targetElements.map(
|
|
43
|
-
(element: { current: any }) => element.current
|
|
44
|
-
);
|
|
45
|
-
this.callback = callback;
|
|
46
|
-
this.config = config;
|
|
47
|
-
|
|
48
|
-
// initialize events
|
|
49
|
-
this._initEvents();
|
|
50
|
-
|
|
51
|
-
// unbind
|
|
52
|
-
return () => this._subscribe && this._subscribe();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
1
|
+
export class Gesture {
|
|
2
|
+
currentIndex?: number;
|
|
3
|
+
lastTimeStamp: number = Date.now();
|
|
4
|
+
isActive: boolean = false;
|
|
5
|
+
targetElement?: HTMLElement; // represents the bounded element
|
|
6
|
+
targetElements: Array<HTMLElement> = []; // represents the bounded elements
|
|
7
|
+
config?: any;
|
|
8
|
+
callback?: <T>(event: T) => void;
|
|
9
|
+
_subscribe?: (eventKeys?: Array<string>) => void;
|
|
10
|
+
static _VELOCITY_LIMIT: number = 20;
|
|
11
|
+
|
|
12
|
+
// it must be overridden by other child classes
|
|
13
|
+
_initEvents() {}
|
|
14
|
+
|
|
15
|
+
// cancel events
|
|
16
|
+
// we only canceled down and move events because mouse up
|
|
17
|
+
// will not be triggered
|
|
18
|
+
_cancelEvents() {
|
|
19
|
+
if (this._subscribe) {
|
|
20
|
+
this._subscribe();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// re-apply new callback
|
|
25
|
+
applyCallback(callback: <T>(event: T) => void) {
|
|
26
|
+
this.callback = callback;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// apply gesture
|
|
30
|
+
applyGesture({
|
|
31
|
+
targetElement,
|
|
32
|
+
targetElements,
|
|
33
|
+
callback,
|
|
34
|
+
config,
|
|
35
|
+
}: {
|
|
36
|
+
targetElement?: any;
|
|
37
|
+
targetElements?: any;
|
|
38
|
+
callback: <T>(event: T) => void;
|
|
39
|
+
config?: any;
|
|
40
|
+
}) {
|
|
41
|
+
this.targetElement = targetElement;
|
|
42
|
+
this.targetElements = targetElements.map(
|
|
43
|
+
(element: { current: any }) => element.current
|
|
44
|
+
);
|
|
45
|
+
this.callback = callback;
|
|
46
|
+
this.config = config;
|
|
47
|
+
|
|
48
|
+
// initialize events
|
|
49
|
+
this._initEvents();
|
|
50
|
+
|
|
51
|
+
// unbind
|
|
52
|
+
return () => this._subscribe && this._subscribe();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
import { attachEvents } from "../eventAttacher";
|
|
2
|
-
import { Vector2 } from "../types";
|
|
3
|
-
import { clamp } from "../math";
|
|
4
|
-
import { withDefault } from "../withDefault";
|
|
5
|
-
import { Gesture } from "./Gesture";
|
|
6
|
-
|
|
7
|
-
export class MouseMoveGesture extends Gesture {
|
|
8
|
-
event?: MouseEvent;
|
|
9
|
-
isActiveID?: any;
|
|
10
|
-
movement: Vector2 = withDefault(0, 0);
|
|
11
|
-
previousMovement: Vector2 = withDefault(0, 0);
|
|
12
|
-
velocity: Vector2 = withDefault(0, 0);
|
|
13
|
-
direction: Vector2 = withDefault(0, 0);
|
|
14
|
-
|
|
15
|
-
// @override
|
|
16
|
-
// initialize the events
|
|
17
|
-
_initEvents() {
|
|
18
|
-
if (this.targetElement) {
|
|
19
|
-
this._subscribe = attachEvents(
|
|
20
|
-
[this.targetElement],
|
|
21
|
-
[["mousemove", this.onMouseMove.bind(this)]]
|
|
22
|
-
);
|
|
23
|
-
} else if (this.targetElements.length > 0) {
|
|
24
|
-
this._subscribe = attachEvents(this.targetElements, [
|
|
25
|
-
["mousemove", this.onMouseMove.bind(this)],
|
|
26
|
-
]);
|
|
27
|
-
} else {
|
|
28
|
-
this._subscribe = attachEvents(
|
|
29
|
-
[window],
|
|
30
|
-
[["mousemove", this.onMouseMove.bind(this)]]
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
_handleCallback() {
|
|
36
|
-
if (this.callback) {
|
|
37
|
-
this.callback({
|
|
38
|
-
args: [this.currentIndex],
|
|
39
|
-
event: this.event,
|
|
40
|
-
isMoving: this.isActive,
|
|
41
|
-
target: this.event?.target,
|
|
42
|
-
mouseX: this.movement.x,
|
|
43
|
-
mouseY: this.movement.y,
|
|
44
|
-
velocityX: this.velocity.x,
|
|
45
|
-
velocityY: this.velocity.y,
|
|
46
|
-
directionX: this.direction.x,
|
|
47
|
-
directionY: this.direction.y,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
onMouseMove(e: MouseEvent) {
|
|
53
|
-
// find current selected element
|
|
54
|
-
const currElem = this.targetElements.find((elem: any) => elem === e.target);
|
|
55
|
-
|
|
56
|
-
// set args
|
|
57
|
-
if (currElem) {
|
|
58
|
-
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.event = e;
|
|
62
|
-
|
|
63
|
-
const now: number = Date.now();
|
|
64
|
-
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
65
|
-
this.lastTimeStamp = now;
|
|
66
|
-
const t = deltaTime / 1000; // seconds
|
|
67
|
-
|
|
68
|
-
const x = e.clientX;
|
|
69
|
-
const y = e.clientY;
|
|
70
|
-
|
|
71
|
-
this.movement = { x, y };
|
|
72
|
-
|
|
73
|
-
if (this.isActiveID !== -1) {
|
|
74
|
-
this.isActive = true;
|
|
75
|
-
clearTimeout(this.isActiveID);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this.isActiveID = setTimeout(() => {
|
|
79
|
-
this.isActive = false;
|
|
80
|
-
this.direction = { x: 0, y: 0 };
|
|
81
|
-
this.velocity = { x: 0, y: 0 };
|
|
82
|
-
|
|
83
|
-
this._handleCallback();
|
|
84
|
-
}, 250); // Debounce 250 milliseconds
|
|
85
|
-
|
|
86
|
-
const diffX = this.movement.x - this.previousMovement.x;
|
|
87
|
-
const diffY = this.movement.y - this.previousMovement.y;
|
|
88
|
-
|
|
89
|
-
this.direction = {
|
|
90
|
-
x: Math.sign(diffX),
|
|
91
|
-
y: Math.sign(diffY),
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
this.velocity = {
|
|
95
|
-
x: clamp(
|
|
96
|
-
diffX / t / 1000,
|
|
97
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
98
|
-
Gesture._VELOCITY_LIMIT
|
|
99
|
-
),
|
|
100
|
-
y: clamp(
|
|
101
|
-
diffY / t / 1000,
|
|
102
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
103
|
-
Gesture._VELOCITY_LIMIT
|
|
104
|
-
),
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
this.previousMovement = { x: this.movement.x, y: this.movement.y };
|
|
108
|
-
|
|
109
|
-
this._handleCallback();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
1
|
+
import { attachEvents } from "../eventAttacher";
|
|
2
|
+
import { Vector2 } from "../types";
|
|
3
|
+
import { clamp } from "../math";
|
|
4
|
+
import { withDefault } from "../withDefault";
|
|
5
|
+
import { Gesture } from "./Gesture";
|
|
6
|
+
|
|
7
|
+
export class MouseMoveGesture extends Gesture {
|
|
8
|
+
event?: MouseEvent;
|
|
9
|
+
isActiveID?: any;
|
|
10
|
+
movement: Vector2 = withDefault(0, 0);
|
|
11
|
+
previousMovement: Vector2 = withDefault(0, 0);
|
|
12
|
+
velocity: Vector2 = withDefault(0, 0);
|
|
13
|
+
direction: Vector2 = withDefault(0, 0);
|
|
14
|
+
|
|
15
|
+
// @override
|
|
16
|
+
// initialize the events
|
|
17
|
+
_initEvents() {
|
|
18
|
+
if (this.targetElement) {
|
|
19
|
+
this._subscribe = attachEvents(
|
|
20
|
+
[this.targetElement],
|
|
21
|
+
[["mousemove", this.onMouseMove.bind(this)]]
|
|
22
|
+
);
|
|
23
|
+
} else if (this.targetElements.length > 0) {
|
|
24
|
+
this._subscribe = attachEvents(this.targetElements, [
|
|
25
|
+
["mousemove", this.onMouseMove.bind(this)],
|
|
26
|
+
]);
|
|
27
|
+
} else {
|
|
28
|
+
this._subscribe = attachEvents(
|
|
29
|
+
[window],
|
|
30
|
+
[["mousemove", this.onMouseMove.bind(this)]]
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_handleCallback() {
|
|
36
|
+
if (this.callback) {
|
|
37
|
+
this.callback({
|
|
38
|
+
args: [this.currentIndex],
|
|
39
|
+
event: this.event,
|
|
40
|
+
isMoving: this.isActive,
|
|
41
|
+
target: this.event?.target,
|
|
42
|
+
mouseX: this.movement.x,
|
|
43
|
+
mouseY: this.movement.y,
|
|
44
|
+
velocityX: this.velocity.x,
|
|
45
|
+
velocityY: this.velocity.y,
|
|
46
|
+
directionX: this.direction.x,
|
|
47
|
+
directionY: this.direction.y,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onMouseMove(e: MouseEvent) {
|
|
53
|
+
// find current selected element
|
|
54
|
+
const currElem = this.targetElements.find((elem: any) => elem === e.target);
|
|
55
|
+
|
|
56
|
+
// set args
|
|
57
|
+
if (currElem) {
|
|
58
|
+
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this.event = e;
|
|
62
|
+
|
|
63
|
+
const now: number = Date.now();
|
|
64
|
+
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
65
|
+
this.lastTimeStamp = now;
|
|
66
|
+
const t = deltaTime / 1000; // seconds
|
|
67
|
+
|
|
68
|
+
const x = e.clientX;
|
|
69
|
+
const y = e.clientY;
|
|
70
|
+
|
|
71
|
+
this.movement = { x, y };
|
|
72
|
+
|
|
73
|
+
if (this.isActiveID !== -1) {
|
|
74
|
+
this.isActive = true;
|
|
75
|
+
clearTimeout(this.isActiveID);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.isActiveID = setTimeout(() => {
|
|
79
|
+
this.isActive = false;
|
|
80
|
+
this.direction = { x: 0, y: 0 };
|
|
81
|
+
this.velocity = { x: 0, y: 0 };
|
|
82
|
+
|
|
83
|
+
this._handleCallback();
|
|
84
|
+
}, 250); // Debounce 250 milliseconds
|
|
85
|
+
|
|
86
|
+
const diffX = this.movement.x - this.previousMovement.x;
|
|
87
|
+
const diffY = this.movement.y - this.previousMovement.y;
|
|
88
|
+
|
|
89
|
+
this.direction = {
|
|
90
|
+
x: Math.sign(diffX),
|
|
91
|
+
y: Math.sign(diffY),
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
this.velocity = {
|
|
95
|
+
x: clamp(
|
|
96
|
+
diffX / t / 1000,
|
|
97
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
98
|
+
Gesture._VELOCITY_LIMIT
|
|
99
|
+
),
|
|
100
|
+
y: clamp(
|
|
101
|
+
diffY / t / 1000,
|
|
102
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
103
|
+
Gesture._VELOCITY_LIMIT
|
|
104
|
+
),
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
this.previousMovement = { x: this.movement.x, y: this.movement.y };
|
|
108
|
+
|
|
109
|
+
this._handleCallback();
|
|
110
|
+
}
|
|
111
|
+
}
|