Components Cards & Layout DotMatrixReveal

DotMatrixReveal

Image hover/scroll reveal through a cursor-tracked grid of growing squares β€” the award-site dot-matrix signature, zero-dep Canvas 2D.

Live demo

01

Playground β€” sweep the cursor across the frame

Both frames share one alt β€” the reveal is pure delight, never extra information.

Variant β€” coarse cells, scroll trigger

Implementation

02
DotMatrixReveal.svelte
<script lang="ts">
  import DotMatrixReveal from '$lib/components/DotMatrixReveal.svelte';
</script>
​
<DotMatrixReveal
  src="/img/product-front.jpg"
  hoverSrc="/img/product-back.jpg"
  alt="Hover to see the reverse"
  cellSize={14}
  trigger="hover"
/>

DotMatrixReveal keeps both images loaded, then paints the hover image onto a canvas through a per-cell mask: each grid cell grows a square from its centre as the reveal frontier (a radius springing outward from the cursor) sweeps past. Only cells near the frontier are drawn, rAF idles at rest, and a ~4k-cell guard coarsens the grid on huge containers. Keyboard focus plays the centred reveal; prefers-reduced-motion swaps the canvas for a plain crossfade.

Logic explainer

03

What Does It Do? (Plain English)

Two images occupy the same frame. When the visitor's cursor crosses the frame (or the frame scrolls into view), the second image is revealed through a grid of growing squares that radiate outward from the cursor β€” like a dot-matrix printer developing the picture in real time. Move the cursor and the reveal frontier follows it.

How It Works (Pseudo-Code)

load both images; size the canvas to the wrapper (dpr ≀ 2)
on trigger (hover / scroll-into-view / focus):
    animate progress 0 β†’ 1 with a spring-ish approach
each frame:
    clear; for each grid cell within the reveal frontier band:
        grow = smoothstep falloff by distance from epicentre
        draw the hover image's cell-region, scaled by grow, centred in the cell
idle β†’ cancel rAF (zero cost when nothing moves)
reduced motion β†’ skip the canvas entirely; crossfade the top image instead

The Cell-Grid Frontier

The reveal isn't a circular mask β€” it's a per-cell grow. Each cell's square scales from 0 to full-bleed based on its distance from the epicentre, with a four-cell "bleed" band so the frontier reads as a soft wave of dots rather than a hard clip. A guard caps the grid at ~4k cells (auto-coarsening cellSize on huge images) so the frame budget survives on mobile.

State Flow Diagram

idle ──trigger──▢ animating (progress 0β†’1, frontier follows cursor)
  β–²                    β”‚
  └── pointerleave β”€β”€β”€β”€β”˜ (progress β†’ 0)
reduced motion: idle ⇄ revealed (crossfade, no canvas work)

Props Reference

Prop Type Default Description
src string required Base image URL
hoverSrc string required Reveal image URL
alt string '' Alt text (wrapper carries it as role="img")
cellSize number 14 Grid cell size (px, pre-guard)
trigger 'hover' | 'scroll' 'hover' What drives the reveal
duration number 600 Full-reveal time budget (ms)
class string '' Extra classes

Edge Cases

  • Slow networks: frames only draw once the reveal image has decoded (naturalWidth guard).
  • Huge containers: the 4k-cell cap coarsens the grid rather than dropping frames.
  • Keyboard parity: the reveal is decorative β€” both images share one alt, so removing the tab stop (a11y-linter clean) loses no information.
  • Reduced motion: canvas never initialises visually; a plain crossfade does the job.

Dependencies

Zero. Canvas 2D + IntersectionObserver + CSS.

File Structure

src/lib/components/DotMatrixReveal.svelte   β€” component
src/lib/components/DotMatrixReveal.md       β€” this doc
src/lib/components/DotMatrixReveal.test.ts  β€” 6 DOM contract tests
src/routes/dotmatrixreveal/+page.svelte     β€” demo (hover + scroll variants)