react-ui-animate 1.4.5 → 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 -0
- package/LICENSE +21 -21
- package/README.md +115 -115
- package/dist/animation/animationType.d.ts +15 -0
- package/dist/animation/getInitialConfig.d.ts +3 -3
- package/dist/animation/index.d.ts +5 -4
- package/dist/animation/interpolation.d.ts +3 -11
- package/dist/animation/modules.d.ts +18 -10
- package/dist/animation/useAnimatedValue.d.ts +11 -23
- package/dist/animation/useMountedValue.d.ts +5 -14
- package/dist/gestures/controllers/DragGesture.d.ts +2 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +102 -114
- package/dist/index.js.map +1 -1
- package/dist/utils/delay.d.ts +5 -0
- package/dist/utils/index.d.ts +2 -1
- package/package.json +49 -49
- package/rollup.config.js +18 -18
- package/src/animation/animationType.ts +17 -0
- package/src/animation/getInitialConfig.ts +61 -31
- package/src/animation/index.ts +9 -4
- package/src/animation/interpolation.ts +24 -57
- package/src/animation/modules.tsx +105 -105
- package/src/animation/useAnimatedValue.ts +62 -132
- package/src/animation/useMountedValue.ts +66 -82
- package/src/gestures/controllers/DragGesture.ts +177 -176
- 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 -0
- package/src/utils/index.ts +2 -1
- package/src/utils/isDefined.ts +4 -4
- package/tsconfig.json +25 -25
|
@@ -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
|
+
}
|
|
@@ -1,107 +1,107 @@
|
|
|
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 ScrollGesture extends Gesture {
|
|
8
|
-
isActiveID?: any;
|
|
9
|
-
movement: Vector2 = withDefault(0, 0);
|
|
10
|
-
previousMovement: Vector2 = withDefault(0, 0);
|
|
11
|
-
direction: Vector2 = withDefault(0, 0);
|
|
12
|
-
velocity: Vector2 = withDefault(0, 0);
|
|
13
|
-
|
|
14
|
-
// @override
|
|
15
|
-
// initialize the events
|
|
16
|
-
_initEvents() {
|
|
17
|
-
if (this.targetElement) {
|
|
18
|
-
this._subscribe = attachEvents(
|
|
19
|
-
[this.targetElement],
|
|
20
|
-
[["scroll", this.scrollElementListener.bind(this)]]
|
|
21
|
-
);
|
|
22
|
-
} else {
|
|
23
|
-
this._subscribe = attachEvents(
|
|
24
|
-
[window],
|
|
25
|
-
[["scroll", this.scrollListener.bind(this)]]
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
_handleCallback() {
|
|
31
|
-
if (this.callback) {
|
|
32
|
-
this.callback({
|
|
33
|
-
isScrolling: this.isActive,
|
|
34
|
-
scrollX: this.movement.x,
|
|
35
|
-
scrollY: this.movement.y,
|
|
36
|
-
velocityX: this.velocity.x,
|
|
37
|
-
velocityY: this.velocity.y,
|
|
38
|
-
directionX: this.direction.x,
|
|
39
|
-
directionY: this.direction.y,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
onScroll({ x, y }: Vector2) {
|
|
45
|
-
const now: number = Date.now();
|
|
46
|
-
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
47
|
-
this.lastTimeStamp = now;
|
|
48
|
-
const t = deltaTime / 1000; // seconds
|
|
49
|
-
|
|
50
|
-
this.movement = { x, y };
|
|
51
|
-
|
|
52
|
-
// Clear if scrolling
|
|
53
|
-
if (this.isActiveID !== -1) {
|
|
54
|
-
this.isActive = true;
|
|
55
|
-
clearTimeout(this.isActiveID);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
this.isActiveID = setTimeout(() => {
|
|
59
|
-
this.isActive = false;
|
|
60
|
-
this.direction = { x: 0, y: 0 };
|
|
61
|
-
|
|
62
|
-
// Reset Velocity
|
|
63
|
-
this.velocity = { x: 0, y: 0 };
|
|
64
|
-
|
|
65
|
-
this._handleCallback(); // Debounce 250milliseconds
|
|
66
|
-
}, 250);
|
|
67
|
-
|
|
68
|
-
const diffX = this.movement.x - this.previousMovement.x;
|
|
69
|
-
const diffY = this.movement.y - this.previousMovement.y;
|
|
70
|
-
|
|
71
|
-
this.direction = {
|
|
72
|
-
x: Math.sign(diffX),
|
|
73
|
-
y: Math.sign(diffY),
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
this.velocity = {
|
|
77
|
-
x: clamp(
|
|
78
|
-
diffX / t / 1000,
|
|
79
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
80
|
-
Gesture._VELOCITY_LIMIT
|
|
81
|
-
),
|
|
82
|
-
y: clamp(
|
|
83
|
-
diffY / t / 1000,
|
|
84
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
85
|
-
Gesture._VELOCITY_LIMIT
|
|
86
|
-
),
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
this.previousMovement = {
|
|
90
|
-
x: this.movement.x,
|
|
91
|
-
y: this.movement.y,
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
this._handleCallback();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
scrollListener() {
|
|
98
|
-
const { pageYOffset: y, pageXOffset: x } = window;
|
|
99
|
-
this.onScroll({ x, y });
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
scrollElementListener() {
|
|
103
|
-
const x = this.targetElement?.scrollLeft || 0;
|
|
104
|
-
const y = this.targetElement?.scrollTop || 0;
|
|
105
|
-
this.onScroll({ x, y });
|
|
106
|
-
}
|
|
107
|
-
}
|
|
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 ScrollGesture extends Gesture {
|
|
8
|
+
isActiveID?: any;
|
|
9
|
+
movement: Vector2 = withDefault(0, 0);
|
|
10
|
+
previousMovement: Vector2 = withDefault(0, 0);
|
|
11
|
+
direction: Vector2 = withDefault(0, 0);
|
|
12
|
+
velocity: Vector2 = withDefault(0, 0);
|
|
13
|
+
|
|
14
|
+
// @override
|
|
15
|
+
// initialize the events
|
|
16
|
+
_initEvents() {
|
|
17
|
+
if (this.targetElement) {
|
|
18
|
+
this._subscribe = attachEvents(
|
|
19
|
+
[this.targetElement],
|
|
20
|
+
[["scroll", this.scrollElementListener.bind(this)]]
|
|
21
|
+
);
|
|
22
|
+
} else {
|
|
23
|
+
this._subscribe = attachEvents(
|
|
24
|
+
[window],
|
|
25
|
+
[["scroll", this.scrollListener.bind(this)]]
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_handleCallback() {
|
|
31
|
+
if (this.callback) {
|
|
32
|
+
this.callback({
|
|
33
|
+
isScrolling: this.isActive,
|
|
34
|
+
scrollX: this.movement.x,
|
|
35
|
+
scrollY: this.movement.y,
|
|
36
|
+
velocityX: this.velocity.x,
|
|
37
|
+
velocityY: this.velocity.y,
|
|
38
|
+
directionX: this.direction.x,
|
|
39
|
+
directionY: this.direction.y,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
onScroll({ x, y }: Vector2) {
|
|
45
|
+
const now: number = Date.now();
|
|
46
|
+
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
47
|
+
this.lastTimeStamp = now;
|
|
48
|
+
const t = deltaTime / 1000; // seconds
|
|
49
|
+
|
|
50
|
+
this.movement = { x, y };
|
|
51
|
+
|
|
52
|
+
// Clear if scrolling
|
|
53
|
+
if (this.isActiveID !== -1) {
|
|
54
|
+
this.isActive = true;
|
|
55
|
+
clearTimeout(this.isActiveID);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.isActiveID = setTimeout(() => {
|
|
59
|
+
this.isActive = false;
|
|
60
|
+
this.direction = { x: 0, y: 0 };
|
|
61
|
+
|
|
62
|
+
// Reset Velocity
|
|
63
|
+
this.velocity = { x: 0, y: 0 };
|
|
64
|
+
|
|
65
|
+
this._handleCallback(); // Debounce 250milliseconds
|
|
66
|
+
}, 250);
|
|
67
|
+
|
|
68
|
+
const diffX = this.movement.x - this.previousMovement.x;
|
|
69
|
+
const diffY = this.movement.y - this.previousMovement.y;
|
|
70
|
+
|
|
71
|
+
this.direction = {
|
|
72
|
+
x: Math.sign(diffX),
|
|
73
|
+
y: Math.sign(diffY),
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
this.velocity = {
|
|
77
|
+
x: clamp(
|
|
78
|
+
diffX / t / 1000,
|
|
79
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
80
|
+
Gesture._VELOCITY_LIMIT
|
|
81
|
+
),
|
|
82
|
+
y: clamp(
|
|
83
|
+
diffY / t / 1000,
|
|
84
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
85
|
+
Gesture._VELOCITY_LIMIT
|
|
86
|
+
),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
this.previousMovement = {
|
|
90
|
+
x: this.movement.x,
|
|
91
|
+
y: this.movement.y,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
this._handleCallback();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
scrollListener() {
|
|
98
|
+
const { pageYOffset: y, pageXOffset: x } = window;
|
|
99
|
+
this.onScroll({ x, y });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
scrollElementListener() {
|
|
103
|
+
const x = this.targetElement?.scrollLeft || 0;
|
|
104
|
+
const y = this.targetElement?.scrollTop || 0;
|
|
105
|
+
this.onScroll({ x, y });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -1,123 +1,123 @@
|
|
|
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
|
-
const LINE_HEIGHT = 40;
|
|
8
|
-
const PAGE_HEIGHT = 800;
|
|
9
|
-
|
|
10
|
-
export class WheelGesture extends Gesture {
|
|
11
|
-
isActiveID?: any;
|
|
12
|
-
movement: Vector2 = withDefault(0, 0);
|
|
13
|
-
previousMovement: Vector2 = withDefault(0, 0);
|
|
14
|
-
direction: Vector2 = withDefault(0, 0);
|
|
15
|
-
velocity: Vector2 = withDefault(0, 0);
|
|
16
|
-
delta: Vector2 = withDefault(0, 0);
|
|
17
|
-
|
|
18
|
-
// Holds offsets
|
|
19
|
-
offset: Vector2 = withDefault(0, 0);
|
|
20
|
-
translation: Vector2 = withDefault(0, 0);
|
|
21
|
-
|
|
22
|
-
// @override
|
|
23
|
-
// initialize the events
|
|
24
|
-
_initEvents() {
|
|
25
|
-
if (this.targetElement) {
|
|
26
|
-
this._subscribe = attachEvents(
|
|
27
|
-
[this.targetElement],
|
|
28
|
-
[["wheel", this.onWheel.bind(this)]]
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
_handleCallback() {
|
|
34
|
-
if (this.callback) {
|
|
35
|
-
this.callback({
|
|
36
|
-
target: this.targetElement,
|
|
37
|
-
isWheeling: this.isActive,
|
|
38
|
-
deltaX: this.delta.x,
|
|
39
|
-
deltaY: this.delta.y,
|
|
40
|
-
directionX: this.direction.x,
|
|
41
|
-
directionY: this.direction.y,
|
|
42
|
-
movementX: this.movement.x,
|
|
43
|
-
movementY: this.movement.y,
|
|
44
|
-
offsetX: this.offset.x,
|
|
45
|
-
offsetY: this.offset.y,
|
|
46
|
-
velocityX: this.velocity.x,
|
|
47
|
-
velocityY: this.velocity.y,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
onWheel(event: WheelEvent) {
|
|
53
|
-
let { deltaX, deltaY, deltaMode } = event;
|
|
54
|
-
|
|
55
|
-
const now: number = Date.now();
|
|
56
|
-
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
57
|
-
this.lastTimeStamp = now;
|
|
58
|
-
const t = deltaTime / 1000; // seconds
|
|
59
|
-
|
|
60
|
-
this.isActive = true;
|
|
61
|
-
|
|
62
|
-
if (this.isActiveID !== -1) {
|
|
63
|
-
this.isActive = true;
|
|
64
|
-
clearTimeout(this.isActiveID);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
this.isActiveID = setTimeout(() => {
|
|
68
|
-
this.isActive = false;
|
|
69
|
-
this.translation = { x: this.offset.x, y: this.offset.y };
|
|
70
|
-
this._handleCallback();
|
|
71
|
-
|
|
72
|
-
this.velocity = { x: 0, y: 0 }; // Reset Velocity
|
|
73
|
-
this.movement = { x: 0, y: 0 };
|
|
74
|
-
}, 200);
|
|
75
|
-
|
|
76
|
-
// normalize wheel values, especially for Firefox
|
|
77
|
-
if (deltaMode === 1) {
|
|
78
|
-
deltaX *= LINE_HEIGHT;
|
|
79
|
-
deltaY *= LINE_HEIGHT;
|
|
80
|
-
} else if (deltaMode === 2) {
|
|
81
|
-
deltaX *= PAGE_HEIGHT;
|
|
82
|
-
deltaY *= PAGE_HEIGHT;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.delta = { x: deltaX, y: deltaY };
|
|
86
|
-
this.movement = {
|
|
87
|
-
x: this.movement.x + deltaX,
|
|
88
|
-
y: this.movement.y + deltaY,
|
|
89
|
-
};
|
|
90
|
-
this.offset = {
|
|
91
|
-
x: this.translation.x + this.movement.x,
|
|
92
|
-
y: this.translation.y + this.movement.y,
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const diffX = this.movement.x - this.previousMovement.x;
|
|
96
|
-
const diffY = this.movement.y - this.previousMovement.y;
|
|
97
|
-
|
|
98
|
-
this.direction = {
|
|
99
|
-
x: Math.sign(diffX),
|
|
100
|
-
y: Math.sign(diffY),
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
this.velocity = {
|
|
104
|
-
x: clamp(
|
|
105
|
-
diffX / t / 1000,
|
|
106
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
107
|
-
Gesture._VELOCITY_LIMIT
|
|
108
|
-
),
|
|
109
|
-
y: clamp(
|
|
110
|
-
diffY / t / 1000,
|
|
111
|
-
-1 * Gesture._VELOCITY_LIMIT,
|
|
112
|
-
Gesture._VELOCITY_LIMIT
|
|
113
|
-
),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
this.previousMovement = {
|
|
117
|
-
x: this.movement.x,
|
|
118
|
-
y: this.movement.y,
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
this._handleCallback();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
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
|
+
const LINE_HEIGHT = 40;
|
|
8
|
+
const PAGE_HEIGHT = 800;
|
|
9
|
+
|
|
10
|
+
export class WheelGesture extends Gesture {
|
|
11
|
+
isActiveID?: any;
|
|
12
|
+
movement: Vector2 = withDefault(0, 0);
|
|
13
|
+
previousMovement: Vector2 = withDefault(0, 0);
|
|
14
|
+
direction: Vector2 = withDefault(0, 0);
|
|
15
|
+
velocity: Vector2 = withDefault(0, 0);
|
|
16
|
+
delta: Vector2 = withDefault(0, 0);
|
|
17
|
+
|
|
18
|
+
// Holds offsets
|
|
19
|
+
offset: Vector2 = withDefault(0, 0);
|
|
20
|
+
translation: Vector2 = withDefault(0, 0);
|
|
21
|
+
|
|
22
|
+
// @override
|
|
23
|
+
// initialize the events
|
|
24
|
+
_initEvents() {
|
|
25
|
+
if (this.targetElement) {
|
|
26
|
+
this._subscribe = attachEvents(
|
|
27
|
+
[this.targetElement],
|
|
28
|
+
[["wheel", this.onWheel.bind(this)]]
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
_handleCallback() {
|
|
34
|
+
if (this.callback) {
|
|
35
|
+
this.callback({
|
|
36
|
+
target: this.targetElement,
|
|
37
|
+
isWheeling: this.isActive,
|
|
38
|
+
deltaX: this.delta.x,
|
|
39
|
+
deltaY: this.delta.y,
|
|
40
|
+
directionX: this.direction.x,
|
|
41
|
+
directionY: this.direction.y,
|
|
42
|
+
movementX: this.movement.x,
|
|
43
|
+
movementY: this.movement.y,
|
|
44
|
+
offsetX: this.offset.x,
|
|
45
|
+
offsetY: this.offset.y,
|
|
46
|
+
velocityX: this.velocity.x,
|
|
47
|
+
velocityY: this.velocity.y,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onWheel(event: WheelEvent) {
|
|
53
|
+
let { deltaX, deltaY, deltaMode } = event;
|
|
54
|
+
|
|
55
|
+
const now: number = Date.now();
|
|
56
|
+
const deltaTime = Math.min(now - this.lastTimeStamp, 64);
|
|
57
|
+
this.lastTimeStamp = now;
|
|
58
|
+
const t = deltaTime / 1000; // seconds
|
|
59
|
+
|
|
60
|
+
this.isActive = true;
|
|
61
|
+
|
|
62
|
+
if (this.isActiveID !== -1) {
|
|
63
|
+
this.isActive = true;
|
|
64
|
+
clearTimeout(this.isActiveID);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.isActiveID = setTimeout(() => {
|
|
68
|
+
this.isActive = false;
|
|
69
|
+
this.translation = { x: this.offset.x, y: this.offset.y };
|
|
70
|
+
this._handleCallback();
|
|
71
|
+
|
|
72
|
+
this.velocity = { x: 0, y: 0 }; // Reset Velocity
|
|
73
|
+
this.movement = { x: 0, y: 0 };
|
|
74
|
+
}, 200);
|
|
75
|
+
|
|
76
|
+
// normalize wheel values, especially for Firefox
|
|
77
|
+
if (deltaMode === 1) {
|
|
78
|
+
deltaX *= LINE_HEIGHT;
|
|
79
|
+
deltaY *= LINE_HEIGHT;
|
|
80
|
+
} else if (deltaMode === 2) {
|
|
81
|
+
deltaX *= PAGE_HEIGHT;
|
|
82
|
+
deltaY *= PAGE_HEIGHT;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.delta = { x: deltaX, y: deltaY };
|
|
86
|
+
this.movement = {
|
|
87
|
+
x: this.movement.x + deltaX,
|
|
88
|
+
y: this.movement.y + deltaY,
|
|
89
|
+
};
|
|
90
|
+
this.offset = {
|
|
91
|
+
x: this.translation.x + this.movement.x,
|
|
92
|
+
y: this.translation.y + this.movement.y,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const diffX = this.movement.x - this.previousMovement.x;
|
|
96
|
+
const diffY = this.movement.y - this.previousMovement.y;
|
|
97
|
+
|
|
98
|
+
this.direction = {
|
|
99
|
+
x: Math.sign(diffX),
|
|
100
|
+
y: Math.sign(diffY),
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
this.velocity = {
|
|
104
|
+
x: clamp(
|
|
105
|
+
diffX / t / 1000,
|
|
106
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
107
|
+
Gesture._VELOCITY_LIMIT
|
|
108
|
+
),
|
|
109
|
+
y: clamp(
|
|
110
|
+
diffY / t / 1000,
|
|
111
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
112
|
+
Gesture._VELOCITY_LIMIT
|
|
113
|
+
),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
this.previousMovement = {
|
|
117
|
+
x: this.movement.x,
|
|
118
|
+
y: this.movement.y,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
this._handleCallback();
|
|
122
|
+
}
|
|
123
|
+
}
|