forked from claudio-silva/claude-artifact-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursorTrail.tsx
More file actions
275 lines (235 loc) · 6.81 KB
/
Copy pathCursorTrail.tsx
File metadata and controls
275 lines (235 loc) · 6.81 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import React, {
CSSProperties,
FC,
ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
interface CursorTrailProps {
images: string[];
children: ReactNode;
className?: string;
spawnInterval?: number;
maxItems?: number;
fadeDuration?: number;
}
interface TrailItem {
id: number;
x: number;
y: number;
image: string;
createdAt: number;
size: number;
rotation: number;
}
type TrailStyle = CSSProperties & {
'--cursor-trail-rotate'?: string;
'--cursor-trail-duration'?: string;
};
type RevealStyle = CSSProperties & {
'--cursor-trail-duration'?: string;
};
type VeilStyle = CSSProperties & {
'--cursor-trail-veil-opacity'?: string;
};
const DEFAULT_SPAWN_INTERVAL = 70;
const DEFAULT_MAX_ITEMS = 28;
const DEFAULT_FADE_DURATION = 900;
const CursorTrail: FC<CursorTrailProps> = ({
images,
children,
className,
spawnInterval = DEFAULT_SPAWN_INTERVAL,
maxItems = DEFAULT_MAX_ITEMS,
fadeDuration = DEFAULT_FADE_DURATION,
}) => {
const [items, setItems] = useState<TrailItem[]>([]);
const lastSpawnRef = useRef(0);
const idRef = useRef(0);
const containerRef = useRef<HTMLDivElement | null>(null);
const isInsideRef = useRef(false);
const rectRef = useRef<DOMRect | null>(null);
const rafRef = useRef<number>();
const chosenImages = useMemo(() => images.filter(Boolean), [images]);
const hasImages = chosenImages.length > 0;
useEffect(() => {
if (!hasImages) {
return undefined;
}
const prune = () => {
const now = performance.now();
setItems((current) => {
const filtered = current.filter((item) => now - item.createdAt < fadeDuration);
if (filtered.length > 0) {
rafRef.current = window.requestAnimationFrame(prune);
} else {
rafRef.current = undefined;
}
return filtered;
});
};
if (items.length > 0 && rafRef.current === undefined) {
rafRef.current = window.requestAnimationFrame(prune);
}
return () => {
if (rafRef.current !== undefined) {
window.cancelAnimationFrame(rafRef.current);
rafRef.current = undefined;
}
};
}, [fadeDuration, hasImages, items.length]);
const updateRect = useCallback(() => {
const node = containerRef.current;
if (!node) {
rectRef.current = null;
return;
}
rectRef.current = node.getBoundingClientRect();
}, []);
useEffect(() => {
if (!hasImages) {
return undefined;
}
updateRect();
const handleResize = () => updateRect();
window.addEventListener('resize', handleResize);
window.addEventListener('scroll', handleResize, true);
return () => {
window.removeEventListener('resize', handleResize);
window.removeEventListener('scroll', handleResize, true);
};
}, [hasImages, updateRect]);
const spawnItem = useCallback(
(clientX: number, clientY: number) => {
if (!hasImages) {
return;
}
const now = performance.now();
if (now - lastSpawnRef.current < spawnInterval) {
return;
}
const rect = rectRef.current ?? containerRef.current?.getBoundingClientRect();
if (!rect) {
return;
}
if (
clientX < rect.left ||
clientX > rect.right ||
clientY < rect.top ||
clientY > rect.bottom
) {
return;
}
lastSpawnRef.current = now;
const x = clientX - rect.left;
const y = clientY - rect.top;
const image = chosenImages[Math.floor(Math.random() * chosenImages.length)];
const size = 36 + Math.random() * 32;
const rotation = -22 + Math.random() * 44;
setItems((current) => {
const filtered = current.filter((item) => now - item.createdAt < fadeDuration);
const nextItems = [
...filtered,
{
id: idRef.current++,
x,
y,
image,
createdAt: now,
size,
rotation,
},
];
if (nextItems.length > maxItems) {
return nextItems.slice(nextItems.length - maxItems);
}
return nextItems;
});
},
[chosenImages, fadeDuration, hasImages, maxItems, spawnInterval],
);
const handlePointerEnter = useCallback(
(event: React.PointerEvent<HTMLDivElement>) => {
isInsideRef.current = true;
updateRect();
spawnItem(event.clientX, event.clientY);
},
[spawnItem, updateRect],
);
const handlePointerLeave = useCallback(() => {
isInsideRef.current = false;
setItems([]);
if (rafRef.current !== undefined) {
window.cancelAnimationFrame(rafRef.current);
rafRef.current = undefined;
}
}, []);
useEffect(() => {
if (!hasImages) {
return undefined;
}
const handleWindowPointerMove = (event: PointerEvent) => {
if (!isInsideRef.current) {
return;
}
spawnItem(event.clientX, event.clientY);
};
window.addEventListener('pointermove', handleWindowPointerMove, { passive: true });
return () => {
window.removeEventListener('pointermove', handleWindowPointerMove);
};
}, [hasImages, spawnItem]);
const outerClassName = ['relative block isolate', className].filter(Boolean).join(' ');
const veilOpacity = items.length > 0 ? '0.56' : '0.26';
const veilStyle: VeilStyle = {
'--cursor-trail-veil-opacity': veilOpacity,
};
return (
<div
ref={containerRef}
className={outerClassName}
onPointerEnter={handlePointerEnter}
onPointerLeave={handlePointerLeave}
>
<div className="relative z-[1]">{children}</div>
<div className="pointer-events-none absolute inset-0 z-[2] overflow-visible">
<div className="cursor-trail-veil" aria-hidden="true" style={veilStyle} />
{items.map((item) => {
const commonDuration = `${fadeDuration}ms`;
const revealStyle: RevealStyle = {
left: item.x,
top: item.y,
width: item.size * 1.85,
height: item.size * 1.85,
'--cursor-trail-duration': commonDuration,
};
const style: TrailStyle = {
left: item.x,
top: item.y,
width: item.size,
height: item.size,
position: 'absolute',
'--cursor-trail-rotate': `${item.rotation}deg`,
'--cursor-trail-duration': commonDuration,
};
return (
<React.Fragment key={item.id}>
<div className="cursor-trail-reveal" style={revealStyle} />
<img
src={item.image}
alt=""
aria-hidden="true"
className="cursor-trail-item select-none"
style={style}
/>
</React.Fragment>
);
})}
</div>
</div>
);
};
export default CursorTrail;