Needle field
A grid of compass needles that all turn to face you.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add needle-fieldOr install the package
Don’t want the file? Import from
asta-ui/needle-fieldand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { NeedleField } from "asta-ui/needle-field";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useRef } from "react"; /** A field of compass needles that all turn to face the pointer; the nearest ones glow. */ export default function NeedleField() { const ref = useRef<HTMLCanvasElement>(null); useEffect(() => { const cv = ref.current!; const css = getComputedStyle(cv); const SIG = css.getPropertyValue("--color-signal").trim() || css.color; const INK = css.getPropertyValue("--color-ink").trim() || css.color; const ctx = cv.getContext("2d")!; let w = 0; let h = 0; let dpr = 1; let raf = 0; let seen = true; // Reduced motion: no idle orbit, and the needles turn straight to the pointer. const still = matchMedia("(prefers-reduced-motion: reduce)").matches; const target = { x: 0, y: 0, active: false }; const cur = { x: 0, y: 0 }; const size = () => { const r = cv.getBoundingClientRect(); w = r.width; h = r.height; dpr = Math.min(window.devicePixelRatio || 1, 2); cv.width = Math.round(w * dpr); cv.height = Math.round(h * dpr); }; const draw = (t: number) => { raf = 0; if (!target.active) { target.x = still ? w / 2 : w / 2 + Math.cos(t / 1400) * w * 0.3; target.y = still ? h / 2 : h / 2 + Math.sin(t / 1100) * h * 0.28; } cur.x += (target.x - cur.x) * (still ? 1 : 0.15); cur.y += (target.y - cur.y) * (still ? 1 : 0.15); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, w, h); const gap = 26; const cols = Math.floor(w / gap); const rows = Math.floor(h / gap); const ox = (w - (cols - 1) * gap) / 2; const oy = (h - (rows - 1) * gap) / 2; ctx.lineCap = "square"; for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { const x = ox + c * gap; const y = oy + r * gap; const a = Math.atan2(cur.y - y, cur.x - x); const d = Math.hypot(cur.x - x, cur.y - y); const k = Math.max(0, 1 - d / 120); const len = 5 + k * 6; ctx.strokeStyle = k > 0.35 ? SIG : INK; ctx.globalAlpha = k > 0.35 ? 1 : Math.min(1, 0.25 + k); ctx.lineWidth = 1.5; ctx.beginPath(); ctx.moveTo(x - Math.cos(a) * len, y - Math.sin(a) * len); ctx.lineTo(x + Math.cos(a) * len, y + Math.sin(a) * len); ctx.stroke(); } } // Keep going while orbiting or still easing; stop once the needles have caught up with a resting pointer. const moving = (!target.active && !still) || Math.abs(target.x - cur.x) > 0.1 || Math.abs(target.y - cur.y) > 0.1; if (moving) wake(); }; const wake = () => { if (!raf && seen && !document.hidden) raf = requestAnimationFrame(draw); }; const move = (e: PointerEvent) => { // offsetX/Y are relative to the canvas itself, so there is no layout read per event. target.x = e.offsetX; target.y = e.offsetY; target.active = true; wake(); }; const leave = () => { target.active = false; wake(); }; const sync = () => { cancelAnimationFrame(raf); raf = 0; wake(); }; size(); const ro = new ResizeObserver(() => { size(); // Resizing clears the canvas: draw it again even when resting. cancelAnimationFrame(raf); raf = 0; if (seen) draw(performance.now()); }); ro.observe(cv); const io = new IntersectionObserver(([e]) => { seen = e.isIntersecting; sync(); }); io.observe(cv); document.addEventListener("visibilitychange", sync); cv.addEventListener("pointermove", move); cv.addEventListener("pointerleave", leave); return () => { cancelAnimationFrame(raf); ro.disconnect(); io.disconnect(); document.removeEventListener("visibilitychange", sync); cv.removeEventListener("pointermove", move); cv.removeEventListener("pointerleave", leave); }; }, []); return <canvas ref={ref} className="absolute inset-0 h-full w-full [touch-action:none]" aria-label="Needles following the pointer" />; }Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Needle field component to my project. A grid of compass needles that all turn to face you. It fills its parent, so give the wrapper a size. It is designed on a dark surface: put it inside an element with the `dark` class. Install it: ```sh pnpm dlx asta-ui@latest add needle-field --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/needle-field.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import NeedleField from "@/components/astabyte/needle-field"; ``` Usage example and props: https://ui.astabyte.com/needle-field Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]
Usage
It fills its parent. Give the wrapper a size.
import NeedleField from "@/components/astabyte/needle-field";
export default function Page() {
// Needle field
return (
<div className="relative h-96 w-full">
<NeedleField />
</div>
);
}