Skip to content
Motion · Surfaces

Pixel reveal

Rub away a wall of pixels to read the message underneath.

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 pixel-reveal

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

  2. Or install the package

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

    $ pnpm add asta-ui
    import { PixelReveal } from "asta-ui/pixel-reveal";
  3. Or copy it by hand

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

    "use client";
    
    import { useEffect, useRef } from "react";
    
    const PX = 12;
    
    /** A wall of pixels hides a message. Rub it away with the pointer; it slowly grows back. */
    export default function PixelReveal() {
      const ref = useRef<HTMLCanvasElement>(null);
    
      useEffect(() => {
        const cv = ref.current!;
        const ctx = cv.getContext("2d")!;
        let w = 0;
        let h = 0;
        let cols = 0;
        let rows = 0;
        let dpr = 1;
        let cells = new Float32Array(0); // 1 = covered, 0 = cleared
        let idle = new Float32Array(0); // ms since cleared
        // Cells that are not fully covered. Each frame only touches these, and only repaints the ones that changed.
        let open: number[] = [];
        let isOpen = new Uint8Array(0);
        let raf = 0;
        let last = 0;
        let seen = true;
        let cover = "";
    
        /** Repaints one cell inside its own square, so a change costs O(1) instead of a full redraw. */
        const paint = (i: number) => {
          const x = (i % cols) * PX;
          const y = Math.floor(i / cols) * PX;
          ctx.clearRect(x, y, PX, PX);
          const v = cells[i];
          if (v <= 0.02) return;
          const s = PX * v;
          ctx.globalAlpha = 0.82 + ((i * 7919) % 9) / 50;
          ctx.fillStyle = cover;
          ctx.fillRect(x + (PX - s) / 2, y + (PX - s) / 2, s, s);
        };
    
        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);
          cols = Math.ceil(w / PX);
          rows = Math.ceil(h / PX);
          cells = new Float32Array(cols * rows).fill(1);
          idle = new Float32Array(cols * rows);
          const cs = getComputedStyle(cv);
          cover = cs.getPropertyValue("--color-paper-2").trim() || cs.color;
          isOpen = new Uint8Array(cols * rows);
          open = [];
          ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
          ctx.clearRect(0, 0, w, h);
          for (let i = 0; i < cells.length; i++) paint(i);
        };
    
        const tick = (t: number) => {
          // Clamp, so a tab that was hidden doesn't regrow everything in one frame.
          const dt = Math.min(100, t - last);
          last = t;
          ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
          for (let k = open.length - 1; k >= 0; k--) {
            const i = open[k];
            idle[i] += dt;
            if (idle[i] <= 1800) continue;
            cells[i] = Math.min(1, cells[i] + dt / 900);
            paint(i);
            if (cells[i] >= 1) {
              // Covered again: swap-remove from the open list.
              isOpen[i] = 0;
              open[k] = open[open.length - 1];
              open.pop();
            }
          }
          // Fully covered: nothing left to grow back, so stop until the next rub.
          raf = open.length && seen && !document.hidden ? requestAnimationFrame(tick) : 0;
        };
        const wake = () => {
          if (raf || !open.length || !seen || document.hidden) return;
          last = performance.now();
          raf = requestAnimationFrame(tick);
        };
    
        const move = (e: PointerEvent) => {
          // offsetX/Y are relative to the canvas itself, so there is no layout read per event.
          const px = e.offsetX;
          const py = e.offsetY;
          const R = 34;
          ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
          for (let y = Math.max(0, Math.floor((py - R) / PX)); y <= Math.min(rows - 1, Math.floor((py + R) / PX)); y++) {
            for (let x = Math.max(0, Math.floor((px - R) / PX)); x <= Math.min(cols - 1, Math.floor((px + R) / PX)); x++) {
              const d = Math.hypot(x * PX + PX / 2 - px, y * PX + PX / 2 - py);
              if (d < R) {
                const i = y * cols + x;
                const v = Math.min(cells[i], d / R - 0.3 < 0 ? 0 : (d / R) * 0.6);
                idle[i] = 0;
                if (v === cells[i]) continue;
                cells[i] = v;
                paint(i);
                if (!isOpen[i]) {
                  isOpen[i] = 1;
                  open.push(i);
                }
              }
            }
          }
          wake();
        };
    
        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("pointermove", move);
        return () => {
          cancelAnimationFrame(raf);
          ro.disconnect();
          io.disconnect();
          document.removeEventListener("visibilitychange", sync);
          cv.removeEventListener("pointermove", move);
        };
      }, []);
    
      return (
        <div className="relative h-full w-full overflow-hidden">
          <div className="absolute inset-0 grid place-items-center p-6 text-center">
            <p className="font-medium leading-[0.92] tracking-[-0.05em] text-[clamp(2rem,4.2vw,3.2rem)] text-signal">
              Made by
              <br />
              hand.
            </p>
          </div>
          <canvas ref={ref} className="absolute inset-0 h-full w-full [touch-action:none]" aria-label="Rub to reveal" />
          <span className="font-mono text-[11px] uppercase tracking-[0.08em] pointer-events-none absolute bottom-3 left-4 text-mute">Rub to reveal</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 Pixel reveal component to my project.
    
    Rub away a wall of pixels to read the message underneath.
    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 pixel-reveal --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/pixel-reveal.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 PixelReveal from "@/components/astabyte/pixel-reveal";
    ```
    
    Usage example and props: https://ui.astabyte.com/pixel-reveal
    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 PixelReveal from "@/components/astabyte/pixel-reveal";

export default function Page() {
  // Pixel reveal
  return (
    <div className="relative h-96 w-full">
      <PixelReveal />
    </div>
  );
}