-
Notifications
You must be signed in to change notification settings - Fork 0
/
useAnimationCursor.ts
80 lines (71 loc) · 2.32 KB
/
useAnimationCursor.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
import { lerp } from "@utils/utils";
import { useEffect, useRef, useState } from "react";
type CursorValues = {
cursorRef: React.RefObject<HTMLDivElement>;
visible: boolean;
};
export const useAnimationCursor = (speed: number): CursorValues => {
const cursorRef = useRef<HTMLDivElement>(null);
const positionRef = useRef({
mouseX: 0,
mouseY: 0,
destinationX: 0,
destinationY: 0,
distanceX: 0,
distanceY: 0,
key: -1,
});
const [visible, setVisibility] = useState(false);
useEffect(() => {
function setMousePosition(event: MouseEvent) {
if (positionRef.current && cursorRef.current) {
if (!visible) setVisibility(true);
const { clientX, clientY } = event;
const mouseX = clientX;
const mouseY = clientY;
positionRef.current.mouseX = mouseX - cursorRef.current.clientWidth / 2;
positionRef.current.mouseY =
mouseY - cursorRef.current.clientHeight / 2;
}
}
document.addEventListener("mousemove", setMousePosition);
return () => {
document.removeEventListener("mousemove", setMousePosition);
};
}, []);
useEffect(() => {
const followMouse = () => {
positionRef.current.key = requestAnimationFrame(followMouse);
const {
mouseX,
mouseY,
destinationX,
destinationY,
distanceX,
distanceY,
} = positionRef.current;
if (!destinationX || !destinationY) {
positionRef.current.destinationX = lerp(0, mouseX, 0.2);
positionRef.current.destinationY = lerp(0, mouseY, 0.2);
} else {
positionRef.current.distanceX = lerp(destinationX, mouseX, 0.2);
positionRef.current.distanceY = lerp(destinationY, mouseY, 0.2);
if (
Math.abs(positionRef.current.distanceX) +
Math.abs(positionRef.current.distanceY) <
0.1
) {
positionRef.current.destinationX = mouseX;
positionRef.current.destinationY = mouseY;
} else {
positionRef.current.destinationX = distanceX;
positionRef.current.destinationY = distanceY;
}
}
if (cursorRef && cursorRef.current)
cursorRef.current.style.transform = `translate3d(${destinationX}px, ${destinationY}px, 0)`;
};
followMouse();
}, [speed]);
return { cursorRef, visible };
};