Loading…
← All components

Cursors · Cyberpunk

Cyber Glitch Reticle

A cyberpunk targeting ring with glitch bursts, a live speed readout and a shockwave on click.

Move over the words, then the buttons.

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

Customize

Primary#22d3ee
Accent#f472b6

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add cyber-glitch
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/CyberGlitchReticleCursor.tsx and paste it in.

components/CyberGlitchReticleCursor.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
20const MONO_FONT = "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
21
22/**
23 * False on touch screens (`pointer: coarse`) and during server rendering: the custom cursor is not
24 * drawn and the native cursor is left alone. Follows the media query live (tablet + mouse…).
25 */
26function useCursorEnabled() {
27 const [enabled, setEnabled] = useState(false);
28 useEffect(() => {
29 const query = window.matchMedia("(pointer: coarse)");
30 const sync = () => setEnabled(!query.matches);
31 sync();
32 query.addEventListener("change", sync);
33 return () => query.removeEventListener("change", sync);
34 }, []);
35 return enabled;
36}
37
38function usePrefersReducedMotion() {
39 const [reduced, setReduced] = useState(false);
40 useEffect(() => {
41 const query = window.matchMedia("(prefers-reduced-motion: reduce)");
42 const sync = () => setReduced(query.matches);
43 sync();
44 query.addEventListener("change", sync);
45 return () => query.removeEventListener("change", sync);
46 }, []);
47 return reduced;
48}
49
50/** Hides the system cursor on the whole page while `active` (restored on unmount). */
51function useHideNativeCursor(active: boolean) {
52 useEffect(() => {
53 if (!active) return undefined;
54 const style = document.createElement("style");
55 style.setAttribute("data-custom-cursor", "");
56 style.textContent = "*, *::before, *::after { cursor: none !important; }";
57 document.head.appendChild(style);
58 return () => style.remove();
59 }, [active]);
60}
61
62interface CursorPointer {
63 /** Position in CSS pixels, relative to the viewport. */
64 x: number;
65 y: number;
66 /** Smoothed velocity, in CSS pixels per second. */
67 vx: number;
68 vy: number;
69 speed: number;
70 /** False until the first move, and while the pointer is outside the window. */
71 visible: boolean;
72 down: boolean;
73 /** The interactive element under the pointer (closest match of the selector), or null. */
74 target: HTMLElement | null;
75 /** Incremented on every press, to trigger click effects. */
76 clicks: number;
77}
78
79/**
80 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
81 * moving the mouse never re-renders React. All listeners are removed on unmount.
82 */
83function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
84 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
85
86 useEffect(() => {
87 if (!enabled) return undefined;
88 let last = performance.now();
89 const p = pointer.current;
90 const pick = (event: PointerEvent) => {
91 const el = event.target instanceof Element ? event.target.closest(selector) : null;
92 p.target = el instanceof HTMLElement ? el : null;
93 };
94 const move = (event: PointerEvent) => {
95 if (event.pointerType === "touch") return;
96 const now = performance.now();
97 const dt = Math.max(1, now - last) / 1000;
98 last = now;
99 if (p.visible) {
100 // Smoothed: one jittery event must not make the effects jump
101 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
102 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
103 }
104 p.x = event.clientX;
105 p.y = event.clientY;
106 p.visible = true;
107 pick(event);
108 };
109 const down = (event: PointerEvent) => {
110 if (event.pointerType === "touch") return;
111 p.down = true;
112 p.clicks += 1;
113 pick(event);
114 };
115 const up = () => {
116 p.down = false;
117 };
118 const leave = () => {
119 p.visible = false;
120 p.down = false;
121 };
122 window.addEventListener("pointermove", move, { passive: true });
123 window.addEventListener("pointerdown", down, { passive: true });
124 window.addEventListener("pointerup", up, { passive: true });
125 document.documentElement.addEventListener("pointerleave", leave);
126 window.addEventListener("blur", leave);
127 return () => {
128 window.removeEventListener("pointermove", move);
129 window.removeEventListener("pointerdown", down);
130 window.removeEventListener("pointerup", up);
131 document.documentElement.removeEventListener("pointerleave", leave);
132 window.removeEventListener("blur", leave);
133 };
134 }, [enabled, selector]);
135
136 return pointer;
137}
138
139/**
140 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
141 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
142 */
143function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
144 const frameRef = useRef(frame);
145 useEffect(() => {
146 frameRef.current = frame;
147 });
148 useEffect(() => {
149 if (!enabled) return undefined;
150 let id = 0;
151 let last = performance.now();
152 const tick = (now: number) => {
153 const dt = Math.min((now - last) / 1000, 1 / 20);
154 last = now;
155 frameRef.current(dt, now / 1000);
156 id = requestAnimationFrame(tick);
157 };
158 id = requestAnimationFrame(tick);
159 return () => cancelAnimationFrame(id);
160 }, [enabled]);
161}
162
163/**
164 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
165 * stops, and read `speed` afterwards.
166 */
167function settlePointer(p: CursorPointer, dt: number) {
168 const k = Math.exp(-10 * dt);
169 p.vx *= k;
170 p.vy *= k;
171 p.speed = Math.hypot(p.vx, p.vy);
172}
173
174/** Exponential smoothing factor for a frame: frame-rate independent `lerp`. */
175const damp = (lambda: number, dt: number) => 1 - Math.exp(-lambda * dt);
176
177/**
178 * Full-viewport 2D canvas sized to the window (pixel ratio capped at 2) and kept in sync on resize.
179 * Returns the canvas ref; read `canvas.getContext("2d")` in the loop. Draw in CSS pixels.
180 */
181function useViewportCanvas(enabled: boolean) {
182 const ref = useRef<HTMLCanvasElement>(null);
183 useEffect(() => {
184 if (!enabled) return undefined;
185 const canvas = ref.current;
186 if (!canvas) return undefined;
187 const resize = () => {
188 const dpr = Math.min(window.devicePixelRatio || 1, 2);
189 canvas.width = Math.round(window.innerWidth * dpr);
190 canvas.height = Math.round(window.innerHeight * dpr);
191 canvas.getContext("2d")?.setTransform(dpr, 0, 0, dpr, 0, 0);
192 };
193 resize();
194 window.addEventListener("resize", resize);
195 return () => window.removeEventListener("resize", resize);
196 }, [enabled]);
197 return ref;
198}
199
200/** Clears a canvas drawn in CSS pixels. */
201function clearCanvas(ctx: CanvasRenderingContext2D) {
202 ctx.save();
203 ctx.setTransform(1, 0, 0, 1, 0, 0);
204 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
205 ctx.restore();
206}
207
208/** "#rrggbb" (or "#rgb") to "r, g, b", for rgba() strings. */
209function hexToRgb(hex: string) {
210 let h = hex.replace("#", "");
211 if (h.length === 3) h = h.split("").map((c) => c + c).join("");
212 const n = parseInt(h.slice(0, 6), 16);
213 if (Number.isNaN(n)) return "255, 255, 255";
214 return `${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}`;
215}
216
217/** 4. Rendering, materials and glow. */
218interface MaterialProps {
219 /** Main color of the object. Default "#6366f1". */
220 primaryColor?: string;
221 /** Color of the glow. Default "#00f0ff". */
222 emissiveColor?: string;
223 /** Strength of the glow, 0 (none) to 3. Default 1. */
224 emissiveIntensity?: number;
225 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
226 roughness?: number;
227 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
228 metalness?: number;
229 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
230 transmission?: number;
231 /** Technical wireframe rendering. Default false. */
232 wireframe?: boolean;
233}
234
235export interface CyberGlitchProps extends Pick<MaterialProps, "primaryColor"> {
236 primaryColor?: string;
237 accentColor?: string;
238 /** Average number of glitch bursts per second (0 = none). Default 0.8. */
239 glitchFrequency?: number;
240 /** Radius of the targeting ring, in CSS pixels. Default 22. */
241 size?: number;
242 /** Hide the system cursor while the reticle is shown. Default true. */
243 hideNativeCursor?: boolean;
244}
245
246interface Shock {
247 x: number;
248 y: number;
249 age: number;
250}
251
252/**
253 * Cyberpunk targeting ring drawn on a 2D canvas: rotating segments, a live speed readout, random
254 * glitch bursts (RGB-offset copies and torn slices), a tighter accent-colored lock over interactive
255 * elements and a shockwave on every click.
256 */
257function CyberGlitchReticleCursor({
258 primaryColor = "#22d3ee",
259 accentColor = "#f472b6",
260 glitchFrequency = 0.8,
261 size = 22,
262 hideNativeCursor = true,
263}: CyberGlitchProps) {
264 const enabled = useCursorEnabled();
265 const reducedMotion = usePrefersReducedMotion();
266 const pointer = useCursorPointer(enabled);
267 useHideNativeCursor(enabled && hideNativeCursor);
268 const canvas = useViewportCanvas(enabled);
269 const state = useRef({ x: -100, y: -100, spin: 0, lock: 0, glitch: 0, clicks: 0, shocks: [] as Shock[], shownSpeed: 0 });
270
271 useAnimationFrame(enabled, (dt, time) => {
272 const ctx = canvas.current?.getContext("2d");
273 if (!ctx) return;
274 const p = pointer.current;
275 settlePointer(p, dt);
276 const s = state.current;
277 clearCanvas(ctx);
278
279 s.x += (p.x - s.x) * damp(30, dt);
280 s.y += (p.y - s.y) * damp(30, dt);
281 s.lock += ((p.target ? 1 : 0) - s.lock) * damp(12, dt);
282 s.spin += dt * (1.2 + p.speed / 900) * (1 - s.lock * 0.7);
283 s.shownSpeed += (p.speed - s.shownSpeed) * damp(6, dt);
284 if (p.clicks !== s.clicks) {
285 s.clicks = p.clicks;
286 s.shocks.push({ x: p.x, y: p.y, age: 0 });
287 }
288 // Glitch bursts: random, about `glitchFrequency` per second, each lasting a few frames
289 if (!reducedMotion && s.glitch <= 0 && Math.random() < glitchFrequency * dt) s.glitch = 0.08 + Math.random() * 0.12;
290 s.glitch = Math.max(0, s.glitch - dt);
291
292 const primary = hexToRgb(primaryColor);
293 const accent = hexToRgb(accentColor);
294
295 // Shockwaves
296 s.shocks = s.shocks.filter((w) => (w.age += dt) < 0.6);
297 for (const w of s.shocks) {
298 const t = w.age / 0.6;
299 ctx.strokeStyle = `rgba(${accent}, ${(1 - t) * 0.9})`;
300 ctx.lineWidth = 2 * (1 - t) + 0.5;
301 ctx.beginPath();
302 ctx.arc(w.x, w.y, size + t * 90, 0, Math.PI * 2);
303 ctx.stroke();
304 }
305 if (!p.visible) return;
306
307 const radius = size * (1 - s.lock * 0.25) * (p.down ? 0.85 : 1);
308 const drawRing = (dx: number, color: string, alpha: number) => {
309 ctx.strokeStyle = `rgba(${color}, ${alpha})`;
310 ctx.lineWidth = 2;
311 // Four segments with gaps, rotating
312 for (let i = 0; i < 4; i++) {
313 const a = s.spin + (i * Math.PI) / 2;
314 ctx.beginPath();
315 ctx.arc(s.x + dx, s.y, radius, a + 0.18, a + Math.PI / 2 - 0.18);
316 ctx.stroke();
317 }
318 // Inner ticks pointing at the center
319 ctx.lineWidth = 1.5;
320 for (let i = 0; i < 4; i++) {
321 const a = -s.spin * 0.5 + (i * Math.PI) / 2;
322 const c = Math.cos(a);
323 const sn = Math.sin(a);
324 ctx.beginPath();
325 ctx.moveTo(s.x + dx + c * radius * 0.45, s.y + sn * radius * 0.45);
326 ctx.lineTo(s.x + dx + c * radius * 0.72, s.y + sn * radius * 0.72);
327 ctx.stroke();
328 }
329 };
330
331 const ringColor = s.lock > 0.5 ? accent : primary;
332 ctx.shadowColor = `rgba(${ringColor}, 0.9)`;
333 ctx.shadowBlur = 8;
334 if (s.glitch > 0) {
335 // RGB split copies, then torn horizontal slices
336 drawRing(-3 - Math.random() * 3, accent, 0.7);
337 drawRing(3 + Math.random() * 3, primary, 0.7);
338 }
339 drawRing(0, ringColor, 1);
340 ctx.fillStyle = `rgba(${ringColor}, 1)`;
341 ctx.fillRect(s.x - 1.5, s.y - 1.5, 3, 3);
342 ctx.shadowBlur = 0;
343
344 if (s.glitch > 0) {
345 for (let i = 0; i < 3; i++) {
346 const y = s.y - radius + Math.random() * radius * 2;
347 const h = 2 + Math.random() * 4;
348 const shift = (Math.random() - 0.5) * 16;
349 ctx.fillStyle = `rgba(${Math.random() > 0.5 ? accent : primary}, 0.55)`;
350 ctx.fillRect(s.x - radius + shift, y, radius * 2, h);
351 }
352 }
353
354 // Readouts
355 ctx.font = `600 12px ${MONO_FONT}`;
356 ctx.fillStyle = `rgba(${primary}, 0.95)`;
357 const tx = s.x + radius + 10;
358 const flicker = s.glitch > 0 ? Math.floor(Math.random() * 9999) : Math.round(s.shownSpeed);
359 ctx.fillText(`SPD ${String(flicker).padStart(4, "0")}`, tx, s.y - 2);
360 ctx.fillStyle = `rgba(${s.lock > 0.5 ? accent : primary}, 0.75)`;
361 ctx.fillText(s.lock > 0.5 ? "LOCKED" : `T+${(time % 100).toFixed(1)}`, tx, s.y + 13);
362 });
363
364 if (!enabled) return null;
365 return <canvas ref={canvas} aria-hidden="true" style={OVERLAY_STYLE} />;
366}
367
368export default memo(CyberGlitchReticleCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import CyberGlitchReticleCursor from "@/components/CyberGlitchReticleCursor";
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 <CyberGlitchReticleCursor
8 glitchFrequency={0.8}
9 size={22}
10 primaryColor="#22d3ee"
11 accentColor="#f472b6"
12 />
13 </div>
14 );
15}
Next componentInverted Disc→