MCPcopy Create free account
hub / github.com/code100x/daily-code / useFollowPointer

Function useFollowPointer

apps/web/components/use-follow-pointer.ts:6–30  ·  view source on GitHub ↗
(ref: RefObject<HTMLElement>)

Source from the content-addressed store, hash-verified

4const spring = { damping: 1, stiffness: 10, restDelta: 0.001 };
5
6export function useFollowPointer(ref: RefObject<HTMLElement>) {
7 const xPoint = useMotionValue(0);
8 const yPoint = useMotionValue(0);
9 const x = useSpring(xPoint, spring);
10 const y = useSpring(yPoint, spring);
11
12 useEffect(() => {
13 if (!ref.current) return;
14
15 const handlePointerMove = ({ clientX, clientY }: MouseEvent) => {
16 const element = ref.current!;
17
18 frame.read(() => {
19 xPoint.set(clientX - element.offsetLeft - element.offsetWidth / 4);
20 yPoint.set(clientY - element.offsetTop - element.offsetHeight / 4);
21 });
22 };
23
24 window.addEventListener("pointermove", handlePointerMove);
25
26 return () => window.removeEventListener("pointermove", handlePointerMove);
27 }, []);
28
29 return { x, y };
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected