# Pixel Reveal

> A headline that resolves like an image loading: it arrives as a few big blocks of colour, and each step halves the pixels until the letters snap sharp.

Category: Text · Free · Updated 2026-11-10 · Live demo: https://www.motionkitui.com/components/pixel-reveal

## When to use it

Tech and gaming launches, retro and creative studio sites, loading and coming-soon pages, and headlines that should feel like they're arriving.

## How it works

Once fonts have loaded and the heading scrolls into view, the text is drawn into a canvas in its real font, size and colour. Each step draws that canvas much smaller and scales it back up with image smoothing turned off, which shows it as blocks; the block size shrinks by a constant factor each step until it reaches single pixels. Then the canvas hides and the real text, which was in the page all along, takes over, so it stays selectable and readable by screen readers.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/pixel-reveal.json
```

Or copy `components/motionkit/pixel-reveal.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `text` | `string` |  | The headline, on one line. |
| `duration` | `number` | `1.4` | Seconds from coarsest pixels to sharp. |
| `block` | `number` | `28` | Size of the first, largest pixels, in px. |
| `repeat` | `boolean` | `false` | Play again each time it comes back into view. |
| `as` | `ElementType` | `"span"` | Element to render, e.g. h1. |

## Accessibility

- **Keyboard: Not applicable.** Text, not a control.
- **Screen readers: Pass.** The real text is in the page the whole time; the pixel canvas is hidden from assistive tech.
- **Reduced motion: Pass.** With reduced motion on, the text simply appears, sharp.
- **Colour contrast: Pass.** It keeps your text colour; ivory on the site's dark measures 17:1.

## Source

```tsx
"use client";

import { useEffect, useRef, useState, type ElementType } from "react";
import { useInView, useReducedMotion } from "motion/react";

type PixelRevealProps = {
  text: string;
  /** Seconds from coarsest pixels to sharp. */
  duration?: number;
  /** Size of the first, largest pixels, in px. */
  block?: number;
  /** Play again each time it comes back into view. */
  repeat?: boolean;
  /** The element to render, e.g. "h1". */
  as?: ElementType;
  className?: string;
};

/**
 * A headline that resolves like an image loading over a slow connection: it arrives as a few large blocks of
 * colour, and each step halves the pixels until the letters snap sharp. The text itself is real and selectable.
 */
export function PixelReveal({ text, duration = 1.4, block = 28, repeat = false, as: Tag = "span", className = "" }: PixelRevealProps) {
  const reduceMotion = useReducedMotion();
  const ref = useRef<HTMLElement>(null);
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const inView = useInView(ref, { once: !repeat, amount: 0.6 });
  const [done, setDone] = useState(false);

  useEffect(() => {
    const element = ref.current;
    const canvas = canvasRef.current;
    if (!element || !canvas || reduceMotion) return;
    if (!inView) return;
    let cancelled = false;
    let timer = 0;

    (async () => {
      // the canvas must draw in the same font as the text, so wait for web fonts first
      await document.fonts?.ready;
      if (cancelled) return;
      const style = getComputedStyle(element);
      const rect = element.getBoundingClientRect();
      const ratio = Math.min(window.devicePixelRatio || 1, 2);
      const width = Math.ceil(rect.width);
      const height = Math.ceil(rect.height);
      canvas.width = width * ratio;
      canvas.height = height * ratio;
      canvas.style.width = `${width}px`;
      canvas.style.height = `${height}px`;

      // the sharp text, drawn once
      const source = document.createElement("canvas");
      source.width = canvas.width;
      source.height = canvas.height;
      const s = source.getContext("2d")!;
      s.scale(ratio, ratio);
      s.font = `${style.fontStyle} ${style.fontWeight} ${style.fontSize} ${style.fontFamily}`;
      s.fillStyle = style.color;
      s.textBaseline = "middle";
      // the element shrinks to fit the text, so drawing from the left edge lines up whatever the alignment
      s.textAlign = "left";
      if ("letterSpacing" in s) (s as CanvasRenderingContext2D & { letterSpacing: string }).letterSpacing = style.letterSpacing === "normal" ? "0px" : style.letterSpacing;
      s.fillText(text, 0, height / 2);

      // each step draws the text tiny, then scales it back up without smoothing, so it shows as blocks
      const c = canvas.getContext("2d")!;
      const small = document.createElement("canvas");
      const steps: number[] = [];
      for (let size = block; size > 1.5; size /= 1.7) steps.push(size);
      steps.push(1);
      const each = (duration * 1000) / steps.length;

      const draw = (index: number) => {
        if (cancelled) return;
        const size = steps[index] * ratio;
        small.width = Math.max(1, Math.round(canvas.width / size));
        small.height = Math.max(1, Math.round(canvas.height / size));
        const sc = small.getContext("2d")!;
        sc.imageSmoothingEnabled = true;
        sc.drawImage(source, 0, 0, small.width, small.height);
        c.clearRect(0, 0, canvas.width, canvas.height);
        c.imageSmoothingEnabled = false;
        c.globalAlpha = 0.35 + (index / steps.length) * 0.65;
        c.drawImage(small, 0, 0, canvas.width, canvas.height);
        if (index < steps.length - 1) timer = window.setTimeout(() => draw(index + 1), each);
        else timer = window.setTimeout(() => setDone(true), each);
      };
      setDone(false);
      draw(0);
    })();

    return () => {
      cancelled = true;
      window.clearTimeout(timer);
    };
  }, [inView, reduceMotion, text, duration, block, repeat]);

  const settled = done || reduceMotion;

  return (
    <Tag ref={ref} className={`relative inline-block whitespace-nowrap ${className}`}>
      <span style={{ opacity: settled ? 1 : 0 }}>{text}</span>
      {/* kept mounted, so a repeat play has a canvas to draw on */}
      <canvas ref={canvasRef} aria-hidden className="pointer-events-none absolute left-0 top-0" style={{ visibility: settled ? "hidden" : "visible" }} />
    </Tag>
  );
}
```
