Loading…
← All components

Cursors · Blueprint

Architect Compass

Drafting guides with ruler graduations, pointer coordinates and the dimensions of what you hover.

Move over the words, then the buttons.

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

Customize

Guides#60a5fa

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add architect-compass
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/ArchitectCompassCursor.tsx and paste it in.

components/ArchitectCompassCursor.tsx
1"use client";
2
3import { memo, useEffect, useId, 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/** 4. Rendering, materials and glow. */
163interface MaterialProps {
164 /** Main color of the object. Default "#6366f1". */
165 primaryColor?: string;
166 /** Color of the glow. Default "#00f0ff". */
167 emissiveColor?: string;
168 /** Strength of the glow, 0 (none) to 3. Default 1. */
169 emissiveIntensity?: number;
170 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
171 roughness?: number;
172 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
173 metalness?: number;
174 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
175 transmission?: number;
176 /** Technical wireframe rendering. Default false. */
177 wireframe?: boolean;
178}
179
180export interface ArchitectCursorProps extends Pick<MaterialProps, "primaryColor"> {
181 /** Color of the guides, graduations and dimensions. Default "#60a5fa". */
182 primaryColor?: string;
183 /** @deprecated Use `primaryColor`. */
184 gridColor?: string;
185 /** Pixel values every 100 px along the guides. Default true. */
186 showRulerLabels?: boolean;
187 /** Hide the system cursor (the guides cross at the pointer). Default true. */
188 hideNativeCursor?: boolean;
189}
190
191const LABEL_STEP = 100;
192
193/**
194 * Drafting-table cursor: infinite X / Y guides through the pointer, graduated like rulers (every
195 * 10 px, longer every 50, numbered every 100 in screen pixels), the pointer's coordinates, and —
196 * over an interactive element — its dimension lines with the width and height in pixels. SVG.
197 */
198function ArchitectCompassCursor({ primaryColor, gridColor = primaryColor ?? "#60a5fa", showRulerLabels = true, hideNativeCursor = true }: ArchitectCursorProps) {
199 const enabled = useCursorEnabled();
200 const pointer = useCursorPointer(enabled);
201 useHideNativeCursor(enabled && hideNativeCursor);
202 const id = useId().replace(/[^a-zA-Z0-9-]/g, "");
203 const root = useRef<SVGSVGElement>(null);
204 const [view, setView] = useState({ w: 0, h: 0 });
205
206 useEffect(() => {
207 if (!enabled) return undefined;
208 const sync = () => setView({ w: window.innerWidth, h: window.innerHeight });
209 sync();
210 window.addEventListener("resize", sync);
211 return () => window.removeEventListener("resize", sync);
212 }, [enabled]);
213
214 useAnimationFrame(enabled, (dt) => {
215 const svg = root.current;
216 const p = pointer.current;
217 if (!svg) return;
218 settlePointer(p, dt);
219 svg.style.opacity = p.visible ? "1" : "0";
220 const x = Math.round(p.x) + 0.5;
221 const y = Math.round(p.y) + 0.5;
222 const q = (sel: string) => svg.querySelector<SVGElement>(sel);
223 q("[data-h]")?.setAttribute("transform", `translate(0 ${y})`);
224 q("[data-v]")?.setAttribute("transform", `translate(${x} 0)`);
225 const coords = q("[data-coords]");
226 if (coords) {
227 coords.setAttribute("x", String(x + 10));
228 coords.setAttribute("y", String(y - 10));
229 coords.textContent = `X ${Math.round(p.x)} · Y ${Math.round(p.y)}`;
230 }
231 q("[data-pip]")?.setAttribute("transform", `translate(${x} ${y})`);
232
233 // Dimension lines around the hovered element
234 const dims = q("[data-dims]");
235 if (!dims) return;
236 const t = p.target;
237 if (!t) {
238 dims.setAttribute("opacity", "0");
239 return;
240 }
241 const r = t.getBoundingClientRect();
242 dims.setAttribute("opacity", "1");
243 const set = (sel: string, attrs: Record<string, number | string>) => {
244 const el = dims.querySelector(sel);
245 if (el) for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, String(v));
246 };
247 set("[data-box]", { x: r.left, y: r.top, width: r.width, height: r.height });
248 const top = r.top - 12;
249 const right = r.right + 12;
250 set("[data-wline]", { x1: r.left, y1: top, x2: r.right, y2: top });
251 set("[data-hline]", { x1: right, y1: r.top, x2: right, y2: r.bottom });
252 const wl = dims.querySelector("[data-wlabel]");
253 if (wl) {
254 wl.setAttribute("x", String(r.left + r.width / 2));
255 wl.setAttribute("y", String(top - 6));
256 wl.textContent = `W ${Math.round(r.width)}`;
257 }
258 const hl = dims.querySelector("[data-hlabel]");
259 if (hl) {
260 hl.setAttribute("x", String(right + 6));
261 hl.setAttribute("y", String(r.top + r.height / 2 + 4));
262 hl.textContent = `H ${Math.round(r.height)}`;
263 }
264 });
265
266 if (!enabled || !view.w) return null;
267 const text = { fill: gridColor, fontFamily: MONO_FONT, fontSize: 12, fontWeight: 600 } as const;
268 const xs = Array.from({ length: Math.ceil(view.w / LABEL_STEP) }, (_, i) => (i + 1) * LABEL_STEP).filter((v) => v < view.w);
269 const ys = Array.from({ length: Math.ceil(view.h / LABEL_STEP) }, (_, i) => (i + 1) * LABEL_STEP).filter((v) => v < view.h);
270
271 return (
272 <svg ref={root} aria-hidden="true" width={view.w} height={view.h} style={{ ...OVERLAY_STYLE, opacity: 0, overflow: "visible" }}>
273 <defs>
274 {/* Graduations anchored to the screen: a tick every 10 px, a long one every 50 px */}
275 <pattern id={`${id}-tx`} width="10" height="1" patternUnits="userSpaceOnUse">
276 <rect width="1" height="1" fill={gridColor} />
277 </pattern>
278 <pattern id={`${id}-tx50`} width="50" height="1" patternUnits="userSpaceOnUse">
279 <rect width="1" height="1" fill={gridColor} />
280 </pattern>
281 <pattern id={`${id}-ty`} width="1" height="10" patternUnits="userSpaceOnUse">
282 <rect width="1" height="1" fill={gridColor} />
283 </pattern>
284 <pattern id={`${id}-ty50`} width="1" height="50" patternUnits="userSpaceOnUse">
285 <rect width="1" height="1" fill={gridColor} />
286 </pattern>
287 <marker id={`${id}-end`} viewBox="0 0 10 10" refX="5" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
288 <path d="M0 0 L10 5 L0 10 z" fill={gridColor} />
289 </marker>
290 </defs>
291
292 {/* Horizontal guide, moved to the pointer's y */}
293 <g data-h="">
294 <line x1="0" x2={view.w} y1="0" y2="0" stroke={gridColor} strokeOpacity="0.55" strokeWidth="1" />
295 <rect x="0" y="-3" width={view.w} height="6" fill={`url(#${id}-tx)`} opacity="0.8" />
296 <rect x="0" y="-7" width={view.w} height="14" fill={`url(#${id}-tx50)`} />
297 {showRulerLabels && xs.map((v) => <text key={v} x={v + 3} y="-9" {...text} fillOpacity="0.75">{v}</text>)}
298 </g>
299 {/* Vertical guide, moved to the pointer's x */}
300 <g data-v="">
301 <line y1="0" y2={view.h} x1="0" x2="0" stroke={gridColor} strokeOpacity="0.55" strokeWidth="1" />
302 <rect y="0" x="-3" height={view.h} width="6" fill={`url(#${id}-ty)`} opacity="0.8" />
303 <rect y="0" x="-7" height={view.h} width="14" fill={`url(#${id}-ty50)`} />
304 {showRulerLabels && ys.map((v) => <text key={v} x="9" y={v + 4} {...text} fillOpacity="0.75">{v}</text>)}
305 </g>
306
307 <g data-pip="">
308 <circle r="5" fill="none" stroke={gridColor} strokeWidth="1.5" />
309 <circle r="1.5" fill={gridColor} />
310 </g>
311 <text data-coords="" {...text} />
312
313 <g data-dims="" opacity="0">
314 <rect data-box="" fill="none" stroke={gridColor} strokeDasharray="4 3" strokeWidth="1" />
315 <line data-wline="" stroke={gridColor} strokeWidth="1" markerStart={`url(#${id}-end)`} markerEnd={`url(#${id}-end)`} />
316 <line data-hline="" stroke={gridColor} strokeWidth="1" markerStart={`url(#${id}-end)`} markerEnd={`url(#${id}-end)`} />
317 <text data-wlabel="" textAnchor="middle" {...text} />
318 <text data-hlabel="" {...text} />
319 </g>
320 </svg>
321 );
322}
323
324export default memo(ArchitectCompassCursor);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import ArchitectCompassCursor from "@/components/ArchitectCompassCursor";
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 <ArchitectCompassCursor
8 primaryColor="#60a5fa"
9 showRulerLabels
10 />
11 </div>
12 );
13}
Next componentInk Streak→