Skip to content

Commit 0c50f6b

Browse files
committed
feat(com):CursorAnimationBg
1 parent 5173c1a commit 0c50f6b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useEffect, useState } from "react";
2+
3+
export const CursorAnimationBg = () => {
4+
const [x, y] = usePointerMove();
5+
6+
return (
7+
<div className="fixed top-0 left-0 w-full h-full">
8+
<div style={{
9+
transform: `translate(${x - 40}px, ${y - 40}px)`,
10+
transition: 'transform .3s ease-out'
11+
}} className="blur-2xl bg-gradient-to-br from-slate-200 to-sky-400/2 absolute top-0 left-0 w-20 h-20 rounded-full pointer-events-none"></div>
12+
<div style={{
13+
transform: `translate(${x - 20}px, ${y - 20}px)`,
14+
transition: 'transform 1s ease-out'
15+
}} className="blur-3xl bg-gradient-to-br from-sky-300 to-sky-400/2 absolute top-0 left-0 w-28 h-28 rounded-full pointer-events-none"></div>
16+
</div>
17+
)
18+
}
19+
20+
const usePointerMove = (): [number, number] => {
21+
const [location, setLocation] = useState<[number, number]>([0, 0]);
22+
23+
useEffect(() => {
24+
const handlePointerMove = ({ pageX, pageY }: MouseEvent) => {
25+
setLocation([pageX, pageY]);
26+
};
27+
const handlePointerLeave = () => {
28+
setLocation([0, 0]);
29+
};
30+
const destroyListener = () => {
31+
window.removeEventListener("pointermove", handlePointerMove);
32+
window.removeEventListener("pointerleave", handlePointerLeave);
33+
};
34+
35+
window.addEventListener("pointerleave", handlePointerLeave);
36+
window.addEventListener("pointermove", handlePointerMove);
37+
38+
return destroyListener;
39+
}, []);
40+
41+
return location
42+
};

0 commit comments

Comments
 (0)