Loading…
← All components

Cursors · HUD

Tactical Crosshair

A military reticle with live X/Y coordinates that locks its brackets onto interactive elements and shows the action.

Move over the words, then the buttons.

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

Customize

Color#7cff6b

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add tactical-crosshair
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/TacticalCrosshairCursor.tsx and paste it in.

components/TacticalCrosshairCursor.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
38/** Hides the system cursor on the whole page while `active` (restored on unmount). */
39function useHideNativeCursor(active: boolean) {
40 useEffect(() => {
41 if (!active) return undefined;
42 const style = document.createElement("style");
43 style.setAttribute("data-custom-cursor", "");
44 style.textContent = "*, *::before, *::after { cursor: none !important; }";
45 document.head.appendChild(style);
46 return () => style.remove();
47 }, [active]);
48}
49
50interface CursorPointer {
51 /** Position in CSS pixels, relative to the viewport. */
52 x: number;
53 y: number;
54 /** Smoothed velocity, in CSS pixels per second. */
55 vx: number;
56 vy: number;
57 speed: number;
58 /** False until the first move, and while the pointer is outside the window. */
59 visible: boolean;
60 down: boolean;
61 /** The interactive element under the pointer (closest match of the selector), or null. */
62 target: HTMLElement | null;
63 /** Incremented on every press, to trigger click effects. */
64 clicks: number;
65}
66
67/**
68 * Tracks the pointer through window listeners and returns a ref read inside the animation loop:
69 * moving the mouse never re-renders React. All listeners are removed on unmount.
70 */
71function useCursorPointer(enabled: boolean, selector = INTERACTIVE_SELECTOR): RefObject<CursorPointer> {
72 const pointer = useRef<CursorPointer>({ x: -100, y: -100, vx: 0, vy: 0, speed: 0, visible: false, down: false, target: null, clicks: 0 });
73
74 useEffect(() => {
75 if (!enabled) return undefined;
76 let last = performance.now();
77 const p = pointer.current;
78 const pick = (event: PointerEvent) => {
79 const el = event.target instanceof Element ? event.target.closest(selector) : null;
80 p.target = el instanceof HTMLElement ? el : null;
81 };
82 const move = (event: PointerEvent) => {
83 if (event.pointerType === "touch") return;
84 const now = performance.now();
85 const dt = Math.max(1, now - last) / 1000;
86 last = now;
87 if (p.visible) {
88 // Smoothed: one jittery event must not make the effects jump
89 p.vx = p.vx * 0.6 + ((event.clientX - p.x) / dt) * 0.4;
90 p.vy = p.vy * 0.6 + ((event.clientY - p.y) / dt) * 0.4;
91 }
92 p.x = event.clientX;
93 p.y = event.clientY;
94 p.visible = true;
95 pick(event);
96 };
97 const down = (event: PointerEvent) => {
98 if (event.pointerType === "touch") return;
99 p.down = true;
100 p.clicks += 1;
101 pick(event);
102 };
103 const up = () => {
104 p.down = false;
105 };
106 const leave = () => {
107 p.visible = false;
108 p.down = false;
109 };
110 window.addEventListener("pointermove", move, { passive: true });
111 window.addEventListener("pointerdown", down, { passive: true });
112 window.addEventListener("pointerup", up, { passive: true });
113 document.documentElement.addEventListener("pointerleave", leave);
114 window.addEventListener("blur", leave);
115 return () => {
116 window.removeEventListener("pointermove", move);
117 window.removeEventListener("pointerdown", down);
118 window.removeEventListener("pointerup", up);
119 document.documentElement.removeEventListener("pointerleave", leave);
120 window.removeEventListener("blur", leave);
121 };
122 }, [enabled, selector]);
123
124 return pointer;
125}
126
127/**
128 * requestAnimationFrame loop: `frame(dt, time)` runs every frame (dt in seconds, capped) while
129 * `enabled`; the browser pauses it in background tabs. The latest callback is always used.
130 */
131function useAnimationFrame(enabled: boolean, frame: (dt: number, time: number) => void) {
132 const frameRef = useRef(frame);
133 useEffect(() => {
134 frameRef.current = frame;
135 });
136 useEffect(() => {
137 if (!enabled) return undefined;
138 let id = 0;
139 let last = performance.now();
140 const tick = (now: number) => {
141 const dt = Math.min((now - last) / 1000, 1 / 20);
142 last = now;
143 frameRef.current(dt, now / 1000);
144 id = requestAnimationFrame(tick);
145 };
146 id = requestAnimationFrame(tick);
147 return () => cancelAnimationFrame(id);
148 }, [enabled]);
149}
150
151/**
152 * Velocity only changes on pointer events: call this once per frame so it fades out when the pointer
153 * stops, and read `speed` afterwards.
154 */
155function settlePointer(p: CursorPointer, dt: number) {
156 const k = Math.exp(-10 * dt);
157 p.vx *= k;
158 p.vy *= k;
159 p.speed = Math.hypot(p.vx, p.vy);
160}
161
162/** Exponential smoothing factor for a frame: frame-rate independent `lerp`. */
163const damp = (lambda: number, dt: number) => 1 - Math.exp(-lambda * dt);
164
165/** The text a hovered element asks the cursor to show: data-cursor-text, then aria-label. */
166function cursorLabel(el: HTMLElement | null, fallback = "") {
167 if (!el) return "";
168 return el.dataset.cursorText ?? el.getAttribute("aria-label") ?? fallback;
169}
170
171/** 4. Rendering, materials and glow. */
172interface MaterialProps {
173 /** Main color of the object. Default "#6366f1". */
174 primaryColor?: string;
175 /** Color of the glow. Default "#00f0ff". */
176 emissiveColor?: string;
177 /** Strength of the glow, 0 (none) to 3. Default 1. */
178 emissiveIntensity?: number;
179 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
180 roughness?: number;
181 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
182 metalness?: number;
183 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
184 transmission?: number;
185 /** Technical wireframe rendering. Default false. */
186 wireframe?: boolean;
187}
188
189export interface TacticalCrosshairProps extends Pick<MaterialProps, "primaryColor"> {
190 /** Color of the reticle and its readouts. Default "#7CFF6B". */
191 primaryColor?: string;
192 /** @deprecated Use `primaryColor`. */
193 color?: string;
194 /** Show the live X / Y readout next to the reticle. Default true. */
195 showCoordinates?: boolean;
196 /** Size of the locked frame relative to the element it frames. Default 1.12. */
197 lockScale?: number;
198 /** Size of the free reticle, in CSS pixels. Default 44. */
199 size?: number;
200 /** Hide the system cursor while the reticle is shown. Default true. */
201 hideNativeCursor?: boolean;
202}
203
204const CORNERS = [
205 [-1, -1],
206 [1, -1],
207 [1, 1],
208 [-1, 1],
209] as const;
210
211/**
212 * Military targeting reticle: four corner brackets, a center pip and a live X / Y readout. Over an
213 * interactive element it locks on — the brackets tighten around the element's box — and shows the
214 * action label (`data-cursor-text`, else `aria-label`, else "TARGET").
215 */
216function TacticalCrosshairCursor({
217 primaryColor,
218 color = primaryColor ?? "#7CFF6B",
219 showCoordinates = true,
220 lockScale = 1.12,
221 size = 44,
222 hideNativeCursor = true,
223}: TacticalCrosshairProps) {
224 const enabled = useCursorEnabled();
225 const pointer = useCursorPointer(enabled);
226 useHideNativeCursor(enabled && hideNativeCursor);
227 const brackets = useRef<(HTMLDivElement | null)[]>([]);
228 const pip = useRef<HTMLDivElement>(null);
229 const readout = useRef<HTMLDivElement>(null);
230 const label = useRef<HTMLDivElement>(null);
231 // Frame box (center + half sizes), smoothed
232 const box = useRef({ x: -100, y: -100, hw: size / 2, hh: size / 2, lock: 0, spin: 0 });
233
234 useAnimationFrame(enabled, (dt, time) => {
235 const p = pointer.current;
236 settlePointer(p, dt);
237 const b = box.current;
238 const target = p.target;
239 let tx = p.x;
240 let ty = p.y;
241 let hw = size / 2;
242 let hh = size / 2;
243 if (target) {
244 const r = target.getBoundingClientRect();
245 tx = r.left + r.width / 2;
246 ty = r.top + r.height / 2;
247 hw = (r.width / 2) * lockScale + 4;
248 hh = (r.height / 2) * lockScale + 4;
249 } else if (p.down) {
250 hw = hh = size * 0.36;
251 }
252 const k = damp(target ? 16 : 24, dt);
253 b.x += (tx - b.x) * k;
254 b.y += (ty - b.y) * k;
255 b.hw += (hw - b.hw) * damp(14, dt);
256 b.hh += (hh - b.hh) * damp(14, dt);
257 b.lock += ((target ? 1 : 0) - b.lock) * damp(10, dt);
258 // The free reticle turns slowly; locked, it squares up
259 b.spin = target ? b.spin * (1 - damp(12, dt)) : (b.spin + dt * 0.6) % (Math.PI * 2);
260
261 const visible = p.visible ? 1 : 0;
262 const arm = Math.max(8, Math.min(b.hw, b.hh) * 0.45);
263 brackets.current.forEach((el, i) => {
264 if (!el) return;
265 const [sx, sy] = CORNERS[i];
266 // Rotate the free reticle around its center
267 const cos = Math.cos(b.spin);
268 const sin = Math.sin(b.spin);
269 const ox = sx * b.hw;
270 const oy = sy * b.hh;
271 const x = b.x + ox * cos - oy * sin;
272 const y = b.y + ox * sin + oy * cos;
273 el.style.width = `${arm}px`;
274 el.style.height = `${arm}px`;
275 el.style.opacity = String(visible);
276 el.style.transform = `translate3d(${x}px, ${y}px, 0) rotate(${b.spin}rad) translate(${sx < 0 ? 0 : -100}%, ${sy < 0 ? 0 : -100}%)`;
277 });
278 if (pip.current) {
279 // The pip stays on the pointer, blinking faster when locked
280 const blink = b.lock > 0.5 ? (Math.sin(time * 18) > 0 ? 1 : 0.35) : 1;
281 pip.current.style.opacity = String(visible * blink);
282 pip.current.style.transform = `translate3d(${p.x - 2}px, ${p.y - 2}px, 0)`;
283 }
284 if (readout.current) {
285 readout.current.style.opacity = String(visible * (showCoordinates ? 1 : 0));
286 readout.current.style.transform = `translate3d(${b.x + b.hw + 10}px, ${b.y + b.hh + 6}px, 0)`;
287 readout.current.textContent = `X ${String(Math.round(p.x)).padStart(4, "0")} Y ${String(Math.round(p.y)).padStart(4, "0")}`;
288 }
289 if (label.current) {
290 const text = target ? cursorLabel(target, "TARGET").toUpperCase() : "";
291 if (label.current.dataset.text !== text) {
292 label.current.dataset.text = text;
293 label.current.textContent = text ? `◆ LOCK · ${text}` : "";
294 }
295 label.current.style.opacity = String(visible * b.lock);
296 label.current.style.transform = `translate3d(${b.x - b.hw}px, ${b.y - b.hh - 22}px, 0)`;
297 }
298 });
299
300 if (!enabled) return null;
301 const glow = `drop-shadow(0 0 4px ${color})`;
302 const text: CSSProperties = {
303 position: "absolute",
304 top: 0,
305 left: 0,
306 font: `600 12px/1 ${MONO_FONT}`,
307 letterSpacing: "0.08em",
308 color,
309 whiteSpace: "nowrap",
310 filter: glow,
311 willChange: "transform",
312 };
313 return (
314 <div aria-hidden="true" style={OVERLAY_STYLE}>
315 {CORNERS.map(([sx, sy], i) => (
316 <div
317 key={i}
318 ref={(el) => {
319 brackets.current[i] = el;
320 }}
321 style={{
322 position: "absolute",
323 top: 0,
324 left: 0,
325 opacity: 0,
326 borderColor: color,
327 borderStyle: "solid",
328 borderWidth: 0,
329 [sy < 0 ? "borderTopWidth" : "borderBottomWidth"]: 2,
330 [sx < 0 ? "borderLeftWidth" : "borderRightWidth"]: 2,
331 filter: glow,
332 willChange: "transform",
333 }}
334 />
335 ))}
336 <div ref={pip} style={{ position: "absolute", top: 0, left: 0, width: 4, height: 4, background: color, filter: glow, opacity: 0 }} />
337 <div ref={readout} style={{ ...text, opacity: 0 }} />
338 <div ref={label} style={{ ...text, opacity: 0 }} />
339 </div>
340 );
341}
342
343export default memo(TacticalCrosshairCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import TacticalCrosshairCursor from "@/components/TacticalCrosshairCursor";
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 <TacticalCrosshairCursor
8 lockScale={1.12}
9 size={44}
10 primaryColor="#7cff6b"
11 showCoordinates
12 />
13 </div>
14 );
15}
Next componentGlass Lens→