Skip to content
Motion · Feedback

Ink trail

The pointer leaves a ribbon that thins and fades as it ages.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add ink-trail

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/ink-trail.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/ink-trail and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { InkTrail } from "asta-ui/ink-trail";
  3. Or copy it by hand

    Copy these files. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useEffect, useRef } from "react";
    import { useInView } from "../lab-hooks";
    
    type P = { x: number; y: number; t: number };
    
    /** Pointer leaves a ribbon that thins and fades out. */
    export default function InkTrail() {
      const ref = useRef<HTMLCanvasElement>(null);
      const visible = useInView(ref);
    
      useEffect(() => {
        // Off screen or in a hidden tab: no listeners, no frames.
        if (!visible) return;
        const canvas = ref.current!;
        const css = getComputedStyle(canvas);
        const SIG = css.getPropertyValue("--color-signal").trim() || css.color;
        const ctx = canvas.getContext("2d")!;
        const pts: P[] = [];
        let head = 0; // index of the oldest live point, so expiring is O(1) instead of shift()
        let raf = 0;
        let dpr = 1;
        const LIFE = 700;
    
        const resize = () => {
          dpr = Math.min(devicePixelRatio || 1, 2);
          canvas.width = canvas.clientWidth * dpr;
          canvas.height = canvas.clientHeight * dpr;
        };
        const loop = (now: number) => {
          ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
          ctx.clearRect(0, 0, canvas.width, canvas.height);
          while (head < pts.length && now - pts[head].t > LIFE) head++;
          // Compact now and then, so a long scribble doesn't keep every dead point (amortised O(1)).
          if (head > 256) {
            pts.splice(0, head);
            head = 0;
          }
          ctx.lineCap = "round";
          ctx.lineJoin = "round";
          for (let i = head + 1; i < pts.length; i++) {
            const a = pts[i - 1];
            const b = pts[i];
            const life = 1 - (now - b.t) / LIFE;
            ctx.strokeStyle = SIG;
            ctx.globalAlpha = Math.max(0, life);
            ctx.lineWidth = 2 + life * 14;
            ctx.beginPath();
            ctx.moveTo(a.x, a.y);
            ctx.lineTo(b.x, b.y);
            ctx.stroke();
          }
          ctx.globalAlpha = 1;
          // Every point has faded: drop them and stop until the pointer moves again.
          if (head >= pts.length) {
            pts.length = 0;
            head = 0;
            raf = 0;
            return;
          }
          raf = requestAnimationFrame(loop);
        };
        const move = (e: PointerEvent) => {
          // offsetX/Y are relative to the canvas itself, so there is no layout read per event.
          pts.push({ x: e.offsetX, y: e.offsetY, t: performance.now() });
          if (!raf) raf = requestAnimationFrame(loop);
        };
        resize();
        const ro = new ResizeObserver(resize);
        ro.observe(canvas);
        canvas.addEventListener("pointermove", move);
        return () => {
          cancelAnimationFrame(raf);
          ro.disconnect();
          canvas.removeEventListener("pointermove", move);
        };
      }, [visible]);
    
      return (
        <div className="relative h-full w-full">
          <canvas ref={ref} className="absolute inset-0 h-full w-full [touch-action:none]" />
          <span className="font-medium leading-[0.92] tracking-[-0.05em] pointer-events-none absolute inset-0 flex items-center justify-center text-3xl text-ink/15">draw here</span>
        </div>
      );
    }
    
  4. 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 Ink trail component to my project.
    
    The pointer leaves a ribbon that thins and fades as it ages.
    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 ink-trail --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/ink-trail.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 InkTrail from "@/components/astabyte/ink-trail";
    ```
    
    Usage example and props: https://ui.astabyte.com/ink-trail
    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 InkTrail from "@/components/astabyte/ink-trail";

export default function Page() {
  // Ink trail
  return (
    <div className="relative h-96 w-full">
      <InkTrail />
    </div>
  );
}