Loading…
← All components

Cursors · Particles

Constellation

Your path drawn as a fading constellation: stars linked by hairlines, throwing tiny sparks.

Move over the words, then the buttons.

Read the docsdata-cursor-text
<ConstellationSparkCursor />Live

Customize

Color#c4b5fd

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add constellation
01

Install

The 3D packages this component is built on.

Nothing to install: React is all it needs.

02

Add the component

One self-contained file: create components/ConstellationSparkCursor.tsx and paste it in.

components/ConstellationSparkCursor.tsx
1"use client";
2
3import { memo, useEffect, useRef, useState, type CSSProperties, type RefObject } from "react";
4
5/** What counts as interactive: the cursors react when the pointer is over one of these. */
6const INTERACTIVE_SELECTOR =
7 "a, button, [role='button'], input, select, textarea, label, summary, [data-cursor], [data-cursor-text], [data-cursor-hover]";
8
9/** Full-viewport, click-through layer above the page. */
10const OVERLAY_STYLE: CSSProperties = {
11 position: "fixed",
12 top: 0,
13 left: 0,
14 width: "100vw",
15 height: "100vh",
16 pointerEvents: "none",
17 zIndex: 9999,
18};
19
20/**
21 * False on touch screens (`pointer: coarse`) and during server rendering: the custom cursor is not
22 * drawn and the native cursor is left alone. Follows the media query live (tablet + mouse…).
23 */
24function useCursorEnabled() {
25 const [enabled, setEnabled] = useState(false);
26 useEffect(() => {
27 const query = window.matchMedia("(pointer: coarse)");
28 const sync = () => setEnabled(!query.matches);
29 sync();
30 query.addEventListener("change", sync);
31 return () => query.removeEventListener("change", sync);
32 }, []);
33 return enabled;
34}
35
36function usePrefersReducedMotion() {
37 const [reduced, setReduced] = useState(false);
38 useEffect(() => {
39 const query = window.matchMedia("(prefers-reduced-motion: reduce)");
40 const sync = () => setReduced(query.matches);
41 sync();
42 query.addEventListener("change", sync);
43 return () => query.removeEventListener("change", sync);
44 }, []);
45 return reduced;
46}
47
48/** Hides the system cursor on the whole page while `active` (restored on unmount). */
49function useHideNativeCursor(active: boolean) {
50 useEffect(() => {
51 if (!active) return undefined;
52 const style = document.createElement("style");
53 style.setAttribute("data-custom-cursor", "");
54 style.textContent = "*, *::before, *::after { cursor: none !important; }";
55 document.head.appendChild(style);
56 return () => style.remove();
57 }, [active]);
58}
59
60interface CursorPointer {
61 /** Position in CSS pixels, relative to the viewport. */
62 x: number;
63 y: number;
64 /** Smoothed velocity, in CSS pixels per second. */
65 vx: number;
66 vy: number;
67 speed: number;
68 /** False until the first move, and while the pointer is outside the window. */
69 visible: boolean;
70 down: boolean;
71 /** The interactive element under the pointer (closest match of the selector), or null. */
72 target: HTMLElement | null;
73 /** Incremented on every press, to trigger click effects. */
74 clicks: number;
75}
76
77/**
78 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
79 * moving the mouse never re-renders React. All listeners are removed on unmount.
80 */
81function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
82 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
83
84 useEffect(() => {
85 if (!enabled) return undefined;
86 let last = performance.now();
87 const p = pointer.current;
88 const pick = (event: PointerEvent) => {
89 const el = event.target instanceof Element ? event.target.closest(selector) : null;
90 p.target = el instanceof HTMLElement ? el : null;
91 };
92 const move = (event: PointerEvent) => {
93 if (event.pointerType === "touch") return;
94 const now = performance.now();
95 const dt = Math.max(1, now - last) / 1000;
96 last = now;
97 if (p.visible) {
98 // Smoothed: one jittery event must not make the effects jump
99 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
100 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
101 }
102 p.x = event.clientX;
103 p.y = event.clientY;
104 p.visible = true;
105 pick(event);
106 };
107 const down = (event: PointerEvent) => {
108 if (event.pointerType === "touch") return;
109 p.down = true;
110 p.clicks += 1;
111 pick(event);
112 };
113 const up = () => {
114 p.down = false;
115 };
116 const leave = () => {
117 p.visible = false;
118 p.down = false;
119 };
120 window.addEventListener("pointermove", move, { passive: true });
121 window.addEventListener("pointerdown", down, { passive: true });
122 window.addEventListener("pointerup", up, { passive: true });
123 document.documentElement.addEventListener("pointerleave", leave);
124 window.addEventListener("blur", leave);
125 return () => {
126 window.removeEventListener("pointermove", move);
127 window.removeEventListener("pointerdown", down);
128 window.removeEventListener("pointerup", up);
129 document.documentElement.removeEventListener("pointerleave", leave);
130 window.removeEventListener("blur", leave);
131 };
132 }, [enabled, selector]);
133
134 return pointer;
135}
136
137/**
138 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
139 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
140 */
141function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
142 const frameRef = useRef(frame);
143 useEffect(() => {
144 frameRef.current = frame;
145 });
146 useEffect(() => {
147 if (!enabled) return undefined;
148 let id = 0;
149 let last = performance.now();
150 const tick = (now: number) => {
151 const dt = Math.min((now - last) / 1000, 1 / 20);
152 last = now;
153 frameRef.current(dt, now / 1000);
154 id = requestAnimationFrame(tick);
155 };
156 id = requestAnimationFrame(tick);
157 return () => cancelAnimationFrame(id);
158 }, [enabled]);
159}
160
161/**
162 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
163 * stops, and read `speed` afterwards.
164 */
165function settlePointer(p: CursorPointer, dt: number) {
166 const k = Math.exp(-10 * dt);
167 p.vx *= k;
168 p.vy *= k;
169 p.speed = Math.hypot(p.vx, p.vy);
170}
171
172/**
173 * Full-viewport 2D canvas sized to the window (pixel ratio capped at 2) and kept in sync on resize.
174 * Returns the canvas ref; read `canvas.getContext("2d")` in the loop. Draw in CSS pixels.
175 */
176function useViewportCanvas(enabled: boolean) {
177 const ref = useRef<HTMLCanvasElement>(null);
178 useEffect(() => {
179 if (!enabled) return undefined;
180 const canvas = ref.current;
181 if (!canvas) return undefined;
182 const resize = () => {
183 const dpr = Math.min(window.devicePixelRatio || 1, 2);
184 canvas.width = Math.round(window.innerWidth * dpr);
185 canvas.height = Math.round(window.innerHeight * dpr);
186 canvas.getContext("2d")?.setTransform(dpr, 0, 0, dpr, 0, 0);
187 };
188 resize();
189 window.addEventListener("resize", resize);
190 return () => window.removeEventListener("resize", resize);
191 }, [enabled]);
192 return ref;
193}
194
195/** Clears a canvas drawn in CSS pixels. */
196function clearCanvas(ctx: CanvasRenderingContext2D) {
197 ctx.save();
198 ctx.setTransform(1, 0, 0, 1, 0, 0);
199 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
200 ctx.restore();
201}
202
203/** "#rrggbb" (or "#rgb") to "r, g, b", for rgba() strings. */
204function hexToRgb(hex: string) {
205 let h = hex.replace("#", "");
206 if (h.length === 3) h = h.split("").map((c) => c + c).join("");
207 const n = parseInt(h.slice(0, 6), 16);
208 if (Number.isNaN(n)) return "255, 255, 255";
209 return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
210}
211
212/** 4. Rendering, materials and glow. */
213interface MaterialProps {
214 /** Main color of the object. Default "#6366f1". */
215 primaryColor?: string;
216 /** Color of the glow. Default "#00f0ff". */
217 emissiveColor?: string;
218 /** Strength of the glow, 0 (none) to 3. Default 1. */
219 emissiveIntensity?: number;
220 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
221 roughness?: number;
222 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
223 metalness?: number;
224 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
225 transmission?: number;
226 /** Technical wireframe rendering. Default false. */
227 wireframe?: boolean;
228}
229
230/** 5. Particles, idle motion and shader animation. */
231interface MotionProps {
232 /** Number of instanced elements. Default depends on the component. */
233 particleCount?: number;
234 /** Spin on its own, in radians per second (negative: the other way; 0.5 ≈ one turn in 12 s). Default 0. */
235 autoRotateSpeed?: number;
236 /** Pulses or ripples per second. Default 1. */
237 pulseRate?: number;
238 /** Amplitude of the noise deformation, 0 (none) to 2. Default 1. */
239 displacementScale?: number;
240}
241
242export interface ConstellationCursorProps extends Pick<MaterialProps, "primaryColor">, Pick<MotionProps, "particleCount"> {
243 /** Color of the stars, links and sparks. Default "#c4b5fd". */
244 primaryColor?: string;
245 /** @deprecated Use `primaryColor`. */
246 lineColor?: string;
247 /** Sparks thrown per 100 px of movement. Default 6. */
248 particleCount?: number;
249 /** @deprecated Use `particleCount`. */
250 sparkCount?: number;
251 /** Number of stars in the trail. Default 22. */
252 tailLength?: number;
253 /** Hide the system cursor (the trail then leads from an empty spot). Default false. */
254 hideNativeCursor?: boolean;
255}
256
257interface Star {
258 x: number;
259 y: number;
260 age: number;
261 size: number;
262}
263interface Spark {
264 x: number;
265 y: number;
266 vx: number;
267 vy: number;
268 life: number;
269}
270
271const STAR_SPACING = 26; // a new star every N px of travel
272const STAR_LIFE = 1.4; // seconds
273const LINK_DISTANCE = 70;
274
275/**
276 * The pointer draws a constellation: stars dropped along its path, joined to their neighbors by
277 * hairlines, throwing tiny sparks that fall under light gravity, all fading within a second or two.
278 */
279function ConstellationSparkCursor({ primaryColor, lineColor = primaryColor ?? "#c4b5fd", particleCount, sparkCount = particleCount ?? 6, tailLength = 22, hideNativeCursor = false }: ConstellationCursorProps) {
280 const enabled = useCursorEnabled();
281 const reducedMotion = usePrefersReducedMotion();
282 const pointer = useCursorPointer(enabled);
283 useHideNativeCursor(enabled && hideNativeCursor);
284 const canvas = useViewportCanvas(enabled);
285 const state = useRef({ stars: [] as Star[], sparks: [] as Spark[], lastX: NaN, lastY: NaN, travel: 0, sparkDebt: 0 });
286
287 useAnimationFrame(enabled, (dt) => {
288 const ctx = canvas.current?.getContext("2d");
289 if (!ctx) return;
290 const p = pointer.current;
291 settlePointer(p, dt);
292 const s = state.current;
293 clearCanvas(ctx);
294
295 // Drop stars and sparks along the path
296 if (p.visible) {
297 if (Number.isNaN(s.lastX)) {
298 s.lastX = p.x;
299 s.lastY = p.y;
300 }
301 const moved = Math.hypot(p.x - s.lastX, p.y - s.lastY);
302 s.travel += moved;
303 s.sparkDebt += (moved / 100) * sparkCount * (reducedMotion ? 0 : 1);
304 if (s.travel >= STAR_SPACING) {
305 s.travel = 0;
306 s.stars.push({ x: p.x, y: p.y, age: 0, size: 1.5 + Math.random() * 2 });
307 if (s.stars.length > tailLength) s.stars.shift();
308 }
309 while (s.sparkDebt >= 1) {
310 s.sparkDebt -= 1;
311 const a = Math.random() * Math.PI * 2;
312 const v = 40 + Math.random() * 90;
313 s.sparks.push({ x: p.x, y: p.y, vx: Math.cos(a) * v + p.vx * 0.05, vy: Math.sin(a) * v + p.vy * 0.05, life: 0.5 + Math.random() * 0.5 });
314 }
315 s.lastX = p.x;
316 s.lastY = p.y;
317 } else {
318 s.lastX = NaN;
319 }
320
321 const rgb = hexToRgb(lineColor);
322 s.stars = s.stars.filter((star) => (star.age += dt) < STAR_LIFE);
323
324 // Links: each star to the next one, and to any close neighbor
325 ctx.lineWidth = 0.8;
326 for (let i = 0; i < s.stars.length; i++) {
327 const a = s.stars[i];
328 const fadeA = 1 - a.age / STAR_LIFE;
329 for (let j = i + 1; j < Math.min(s.stars.length, i + 6); j++) {
330 const b = s.stars[j];
331 const d = Math.hypot(a.x - b.x, a.y - b.y);
332 if (j !== i + 1 && d > LINK_DISTANCE) continue;
333 const alpha = Math.min(fadeA, 1 - b.age / STAR_LIFE) * (j === i + 1 ? 0.7 : 0.3);
334 ctx.strokeStyle = `rgba(${rgb}, ${alpha})`;
335 ctx.beginPath();
336 ctx.moveTo(a.x, a.y);
337 ctx.lineTo(b.x, b.y);
338 ctx.stroke();
339 }
340 }
341 // Stars: small four-point diamonds
342 for (const star of s.stars) {
343 const fade = 1 - star.age / STAR_LIFE;
344 const r = star.size * (0.6 + fade * 0.6);
345 ctx.fillStyle = `rgba(${rgb}, ${fade})`;
346 ctx.beginPath();
347 ctx.moveTo(star.x, star.y - r * 2);
348 ctx.lineTo(star.x + r * 0.6, star.y);
349 ctx.lineTo(star.x, star.y + r * 2);
350 ctx.lineTo(star.x - r * 0.6, star.y);
351 ctx.closePath();
352 ctx.fill();
353 ctx.fillRect(star.x - r * 1.6, star.y - 0.4, r * 3.2, 0.8);
354 }
355 // Sparks: short streaks falling under light gravity
356 s.sparks = s.sparks.filter((sp) => (sp.life -= dt) > 0);
357 ctx.lineWidth = 1;
358 for (const sp of s.sparks) {
359 sp.vy += 160 * dt;
360 sp.vx *= Math.exp(-2 * dt);
361 sp.x += sp.vx * dt;
362 sp.y += sp.vy * dt;
363 ctx.strokeStyle = `rgba(${rgb}, ${Math.min(1, sp.life * 1.6)})`;
364 ctx.beginPath();
365 ctx.moveTo(sp.x, sp.y);
366 ctx.lineTo(sp.x - sp.vx * 0.03, sp.y - sp.vy * 0.03);
367 ctx.stroke();
368 }
369 });
370
371 if (!enabled) return null;
372 return <canvas ref={canvas} aria-hidden="true" style={OVERLAY_STYLE} />;
373}
374
375export default memo(ConstellationSparkCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import ConstellationSparkCursor from "@/components/ConstellationSparkCursor";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <ConstellationSparkCursor
8 particleCount={6}
9 tailLength={22}
10 primaryColor="#c4b5fd"
11 />
12 </div>
13 );
14}
Next componentRetro Terminal→