Gravity words
Words as physical discs.
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 gravity-pillsOr install the package
Don’t want the file? Import from
asta-ui/gravity-pillsand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { GravityPills } from "asta-ui/gravity-pills";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"; const WORDS = ["motion", "type", "rust", "grid", "lab", "brand", "spring", "ui", "craft", "pixels"]; type Body = { x: number; y: number; vx: number; vy: number; r: number; word: string; hot: boolean }; /** Words as physical discs: they fall, pile up, and can be grabbed and thrown. */ export default function GravityPills() { const ref = useRef<HTMLCanvasElement>(null); useEffect(() => { const cv = ref.current!; const css = getComputedStyle(cv); const SIG = css.getPropertyValue("--color-signal").trim() || css.color; const ON = css.getPropertyValue("--color-on-signal").trim() || css.getPropertyValue("--color-paper").trim() || "transparent"; const INK = css.getPropertyValue("--color-ink").trim() || css.color; const DISC = css.getPropertyValue("--color-paper-2").trim() || "transparent"; const LINE = css.getPropertyValue("--color-line").trim() || css.color; const ctx = cv.getContext("2d")!; let w = 0; let h = 0; let dpr = 1; let raf = 0; let seen = true; // Rest check: every 30 frames, how far each disc got since the last check. A settled pile only trembles // in place (well under a pixel), so under 0.5px means asleep. let frame = 0; let snap: number[] = []; let asleep = false; // Reduced motion: the discs land at once instead of falling. Dragging still follows the pointer. const still = matchMedia("(prefers-reduced-motion: reduce)").matches; let bodies: Body[] = []; let grab: { b: Body; ox: number; oy: number; px: number; py: number } | null = null; const reset = () => { bodies = WORDS.map((word, i) => ({ x: w * (0.2 + ((i * 0.37) % 0.6)), y: -40 - i * 45, vx: 0, vy: 0, r: Math.max(22, Math.min(w * 0.09, 14 + word.length * 4.2)), word, hot: i === 4, })); }; 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); if (!bodies.length) reset(); // Resizing clears the canvas: paint the current frame again, then keep simulating if needed. paint(); kick(); }; const step = () => { for (const b of bodies) { if (grab?.b === b) continue; b.vy += 0.45; b.vx *= 0.995; b.x += b.vx; b.y += b.vy; if (b.y > h - b.r) { b.y = h - b.r; b.vy *= -0.35; b.vx *= 0.9; } if (b.x < b.r) { b.x = b.r; b.vx *= -0.5; } if (b.x > w - b.r) { b.x = w - b.r; b.vx *= -0.5; } } for (let k = 0; k < 3; k++) { for (let i = 0; i < bodies.length; i++) { for (let j = i + 1; j < bodies.length; j++) { const a = bodies[i]; const b = bodies[j]; const dx = b.x - a.x; const dy = b.y - a.y; const d = Math.hypot(dx, dy) || 0.01; const min = a.r + b.r; if (d < min) { const push = (min - d) / 2; const nx = dx / d; const ny = dy / d; const ag = grab?.b === a; const bg = grab?.b === b; if (!ag) { a.x -= nx * push * (bg ? 2 : 1); a.y -= ny * push * (bg ? 2 : 1); } if (!bg) { b.x += nx * push * (ag ? 2 : 1); b.y += ny * push * (ag ? 2 : 1); } const rv = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny; if (rv < 0) { const imp = -rv * 0.6; if (!ag) { a.vx -= imp * nx; a.vy -= imp * ny; } if (!bg) { b.vx += imp * nx; b.vy += imp * ny; } } } } } } }; /** True once no disc has moved half a pixel over the last 30 frames. */ const rested = () => { if (++frame % 30) return asleep; let moved = snap.length === bodies.length * 2 ? 0 : Infinity; for (let i = 0; i < bodies.length && moved < 0.5; i++) moved = Math.max(moved, Math.abs(bodies[i].x - snap[2 * i]), Math.abs(bodies[i].y - snap[2 * i + 1])); snap = bodies.flatMap((b) => [b.x, b.y]); asleep = moved < 0.5; return asleep; }; const paint = () => { ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, w, h); ctx.textAlign = "center"; ctx.textBaseline = "middle"; for (const b of bodies) { ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2); ctx.fillStyle = b.hot ? SIG : grab?.b === b ? INK : DISC; ctx.fill(); ctx.strokeStyle = b.hot ? SIG : LINE; ctx.stroke(); ctx.fillStyle = b.hot || grab?.b === b ? ON : INK; ctx.font = `500 ${Math.round(b.r * 0.42)}px ui-sans-serif, system-ui`; ctx.fillText(b.word, b.x, b.y + 1); } }; /** Runs the physics without drawing until the pile is at rest (reduced motion). */ const settle = () => { for (let n = 0; n < 1200; n++) { step(); if (rested()) break; } asleep = true; }; const draw = () => { raf = 0; if (still && !grab) { settle(); paint(); return; } step(); paint(); // Sleep once the pile is at rest and no disc is held. A grab, a throw or a reset wakes it. if (grab || !rested()) wake(); }; const wake = () => { if (!raf && seen && !document.hidden) raf = requestAnimationFrame(draw); }; const kick = () => { frame = 0; snap = []; asleep = false; wake(); }; // offsetX/Y are relative to the canvas (also while captured), so there is no layout read per event. const pos = (e: PointerEvent) => ({ x: e.offsetX, y: e.offsetY }); const down = (e: PointerEvent) => { const p = pos(e); const b = [...bodies].reverse().find((b) => Math.hypot(b.x - p.x, b.y - p.y) < b.r); if (!b) return; cv.setPointerCapture(e.pointerId); grab = { b, ox: b.x - p.x, oy: b.y - p.y, px: p.x, py: p.y }; kick(); }; const move = (e: PointerEvent) => { if (!grab) return; const p = pos(e); grab.b.vx = p.x - grab.px; grab.b.vy = p.y - grab.py; grab.b.x = Math.max(grab.b.r, Math.min(w - grab.b.r, p.x + grab.ox)); grab.b.y = Math.max(grab.b.r, Math.min(h - grab.b.r, p.y + grab.oy)); grab.px = p.x; grab.py = p.y; }; const up = () => { grab = null; kick(); }; const dbl = () => { reset(); kick(); }; const sync = () => { cancelAnimationFrame(raf); raf = 0; wake(); }; size(); const ro = new ResizeObserver(size); ro.observe(cv); const io = new IntersectionObserver(([e]) => { seen = e.isIntersecting; sync(); }); io.observe(cv); document.addEventListener("visibilitychange", sync); cv.addEventListener("pointerdown", down); cv.addEventListener("pointermove", move); cv.addEventListener("pointerup", up); cv.addEventListener("pointercancel", up); cv.addEventListener("dblclick", dbl); return () => { cancelAnimationFrame(raf); ro.disconnect(); io.disconnect(); document.removeEventListener("visibilitychange", sync); cv.removeEventListener("pointerdown", down); cv.removeEventListener("pointermove", move); cv.removeEventListener("pointerup", up); cv.removeEventListener("pointercancel", up); cv.removeEventListener("dblclick", dbl); }; }, []); return ( <div className="relative h-full w-full"> <canvas ref={ref} className="absolute inset-0 h-full w-full cursor-grab [touch-action:none] active:cursor-grabbing" aria-label="Throwable words" /> <span className="font-mono text-[11px] uppercase tracking-[0.08em] pointer-events-none absolute left-4 top-3 text-mute">Grab & throw · double-click to drop again</span> </div> ); }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 Gravity words component to my project. Words as physical discs. 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 gravity-pills --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/gravity-pills.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 GravityPills from "@/components/astabyte/gravity-pills"; ``` Usage example and props: https://ui.astabyte.com/gravity-pills 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 GravityPills from "@/components/astabyte/gravity-pills";
export default function Page() {
// Gravity words
return (
<div className="relative h-96 w-full">
<GravityPills />
</div>
);
}