Impeccable

Cube flip image

Click a picture and tiny cubes roll over from that spot, a wave that shows the next one. Use it on a gallery or hero with 3 to 10 photos.

ImageClickSoundNo extra packagesLight + darkMobile
Click anywhere on the photo
npx shadcn@latest add https://impeccable-ui.com/r/cube-flip-image.json
Click anywhere on the photo, with sound on. The cubes nearest your click roll first, rising toward you as they roll a half turn away from it, and the wave spreads to the edges. Try a corner, then the middle. Clicks during a change are ignored.

Run it in a project set up with shadcn. No other packages needed. Or open and copy the file into components/ui/cube-flip-image.tsx.

Cube sizes

Three sizes, each its own picture to click. Fine cubes ripple like a shimmer, the default shows tiny cubes rolling, and bold ones are blocks you can watch turn one by one.

Fine · tile={12}
Default · tile={20}
Bold · tile={36}
<CubeFlipImage images={shots} tile={12} />   // fine
<CubeFlipImage images={shots} />              // default, about 20px
<CubeFlipImage images={shots} tile={36} />   // bold

Prompts

Paste one into Claude Code, Cursor or any AI coding tool. Install adds this exact picture and asks only where it goes, which pictures it shows and whether it plays the sound. Blend asks five short questions (where, the cube size, the sound, the shape and the corners), then fits it to your product without changing how the cubes roll.

# Cube flip image

A 1-file React component from Impeccable. Add it to this project so it works and looks exactly like the source below, in the one place I choose, and change nothing else.

A picture you click to change. It is made of tiny cubes (about 20px each), the current picture on their front faces and the next one on their backs. Click anywhere and a wave spreads out from that exact spot: a dark gap opens round each cube so you see it on its own, it rises toward you, casting a soft shadow on the picture below, and rolls a half turn away from the click, past a darker side face, onto its new face, seen in perspective so each face narrows toward its far edge. The wave travels slowly enough to watch, like an ocean wave, and the next picture has taken over in about 2.6 seconds. Clicks during a change are ignored: one click, one change. An optional sound, a cascade of small cube clacks, plays with each change.

## Before you build: one question

Look through this project first: its hero, galleries, portfolio or product pictures, carousels, and any place that already shows several pictures one after another. Then ask me this in one message and wait for my answer:

- **Where should it go, which pictures does it show, and should it play the cube sound?** Suggest the 1–3 places in my code it fits best and the 3 to 10 pictures each would cycle through. Sound is on in the source (suggested); I can say off.
  *Why: it changes only when someone clicks it, so it belongs where people want to see the next picture, and it needs real pictures to show.*

Ask nothing else. If I say *skip*, use the best suggestion.

## Keep it as it is

The tiny cubes (about 20px by default) rolling a half turn away from the click past a darker side face, rising toward you with a soft shadow as the wave passes, the wave spreading from the exact spot clicked, one change per click with clicks during a change ignored, the perspective of each rolling cube, the shading of each face and the shadow in the gap, the thin edge where the faces meet, the dark gap that opens round each cube as it swells, the 0.76 second roll inside a 1.16 second swell, the slow wave (about 2.6 seconds for the whole picture wherever you click), the soft fade back to the plain image at the end, and their easing, are the design. Keep them exactly as in the source. If something doesn't fit my project, tell me instead of changing it.

- Add only the component file, the sound file if I want sound, and the one place I picked. If it replaces an existing picture or slideshow there, pass those same pictures to `images` and remove only the old display. Don't add packages, change other files, or restyle anything.
- Don't add the demo parts from the Impeccable website: the nine travel photos it plays, the "Click anywhere on the photo" line, the dotted card and the home page's TV, which clicks on its own.

## Fastest path

If this project has a `components.json` (shadcn), run `npx shadcn@latest add https://impeccable-ui.com/r/cube-flip-image.json`.
If the command succeeds, only fix import paths to match this project and skip ahead. If it fails or there is no `components.json`, build it by hand as below.

## Build it

- Stack: React 18 or 19 with TypeScript. No other packages: the change is drawn on one `<canvas>` with `requestAnimationFrame`, only while it runs, the sound is a plain `Audio` element, and its styles sit in a `<style>` tag inside the file.
- JavaScript project? Name the file `cube-flip-image.jsx` and remove the type annotations; nothing else changes.
- Create this file, in this project's usual place for components:
  - `components/ui/cube-flip-image.tsx`
- The sound is a file, not code. If I want it, download `https://impeccable-ui.com/cube-flip-image/cube-roll.mp3` (26KB) into this project's public or static folder, for example `public/sounds/cube-roll.mp3`, and pass its path to `sound`. If the project's Content Security Policy limits `media-src`, the file must be served from this site itself.
- Keep these exports and their names: `CubeFlipImage` (also the default export), `CubeFlipImageProps` and `CubeFlipPicture`.
- Keep `"use client"` at the top in Next.js App Router projects. In other setups it is harmless.
- Keep the tuning values at the top exactly as written: `ROLL_MS`, `SWELL_MS`, `SPREAD_MS`, `JITTER_MS`, `TOTAL_MS`, `TILE`, `DEPTH`, `GAP`, `LIFT`, `SHADE`, `SIDE`, `EDGE`, `BEHIND`, `SHADOW`, `DROP`, `HANDOFF_MS`, `FADE_MS`, `LOUDNESS`, `MAX_TILES`, the `roll` curve, the cube geometry in `cubeFaces` and the `CSS` block. They are the design and its timing.
- Keep the fallbacks: the first picture is drawn as a plain image with no motion, so the server and the browser draw the same thing. It is a real `<button>`, so Enter or Space starts the wave from the middle, and screen readers hear the picture's `alt` and what a click does. With `prefers-reduced-motion: reduce` no cube rolls and no sound plays: the next picture simply fades in over 0.3 seconds. If a picture fails to load, or the tab is hidden, it swaps with no motion. A phone on silent or a browser that blocks sound just stays quiet.

## Use it

```tsx
"use client";
import { CubeFlipImage } from "@/components/ui/cube-flip-image";

const SHOTS = [
  { src: "/shots/settle-counter.webp", alt: "Settle counter, a live count of humans on Impeccable UI" },
  { src: "/shots/tumble-gallery.webp", alt: "Tumble Gallery, a split flap board of pictures" },
  { src: "/shots/tile-flip-image.webp", alt: "Tile flip image, a picture turning over tile by tile" },
];

// Each click rolls the cubes out from that spot to the next shot. The box takes its size from className.
export function ShotWall() {
  return <CubeFlipImage images={SHOTS} sound="/sounds/cube-roll.mp3" className="w-full max-w-xl rounded-2xl" />;
}
```

- `images` are the pictures, each with `src` and `alt`, in order; each click shows the next one, then it starts again.
- `sound` is the path to the cube sound; leave it out for silence.
- `tile` is about how wide each cube is, in pixels (20 by default). Smaller is finer, larger shows the cubes more.
- `ratio` is the box's shape as a CSS aspect ratio (`"3 / 2"` by default). The picture covers the box, centered.
- `label` is what a click does, read by screen readers after the picture's alt ("show the next picture" by default). `onChange` gets the new index after each change.
- Corners and size come from `className` or `style`. The box clips the cubes, so rounded corners stay round while they roll.
- Use pictures of about the same shape, so they line up the same way.

## How the picture works

Read this before you touch anything, so you know what the code does and why it must stay as it is.

1. **At rest.** It is a button holding one plain `<img>` that covers its box, centered, like a cover photo. Nothing runs and there is no canvas. The next picture is fetched in the background, so a click can start at once.
2. **One click, one change.** A click starts a change only when none is running. Clicks during a change are ignored, so many quick clicks still give one change. After the last picture it starts again from the first.
3. **The point.** The wave starts from the exact spot the click landed on. A keyboard press starts it from the middle.
4. **The sound.** If `sound` is set, it plays inside the click itself (browsers only allow sound that starts from one), at 60% volume, and not with reduced motion.
5. **One canvas.** For the change, a canvas the size of the box (at the screen's sharpness, up to 2x) is laid over the image and every frame is drawn on it, so thousands of cubes stay smooth.
6. **The cubes.** The box is cut into square cubes about `tile` pixels wide (20 by default), edges on whole pixels. On a very big picture they grow so there are never more than 2,500. Cubes the wave hasn't reached are drawn as one piece of the old picture, and cubes that have landed as one piece of the new one, so no line ever shows between them.
7. **The wave.** Each cube starts rolling when the wave reaches it: 0.2 seconds, plus its distance from the click as a share of the distance to the farthest corner times 1.4 seconds, plus up to 0.04 seconds of randomness so the wave looks hand made. The wave travels at an even, watchable speed, so wherever you click, the last cube settles at about 2.6 seconds.
8. **The roll.** Each cube rolls a half turn (180 degrees) in 0.76 seconds, away from the click: sideways when the click is more to its side, up or down when it is more above or below. Its front face holds the old picture, its back face the new one, and the side face between them, which faces you at a quarter turn, shows the new picture 18% darker so the cube reads as solid. It is seen in perspective from 3 cube sides away (`DEPTH`, worked out in `cubeFaces`): each face is drawn in thin strips so it narrows toward its far edge, with a thin dark line where two faces meet, and each face darkens up to 42% as it turns away from you. The roll tips over slowly, turns fastest at the middle and settles softly onto the new face.
9. **The ocean wave.** Each cube swells up 0.2 seconds before it rolls, rolls half a turn at the crest, and settles back down over 0.2 seconds after, like water lifting and falling as a wave passes. As it swells, a dark gap opens round it: it shrinks by up to 20% (`GAP`) and grows back 6% as it rises toward you (`LIFT`), so at the crest it is 14% smaller than its cell and every cube is seen turning on its own. Both follow a smooth curve with no kink at either end (sine squared). Risen cubes are drawn over their neighbors, lowest first, and each casts a soft shadow down and to the right on the picture below, in three layers so its edge is soft: up to 50% dark and 26% of a cube away at the crest (`SHADOW`, `DROP`). The shadow is what makes the cubes look lifted. The gap round a rolling cube shows the new picture 62% darker (`BEHIND`), so the cubes stand apart.
10. **Landing.** The last frame is the whole new picture. The plain image takes it under the canvas, then the canvas fades away over it in 0.16 seconds, so the swap never shows. `onChange` is called with the new index.
11. **Edge cases.** If the box changes size mid change, it lands at once. A hidden tab never leaves it stuck halfway.

## Check

- It type-checks and the page renders with no console errors.
- A click anywhere starts a wave from that exact spot: the cubes near it roll first, each rising toward you with a soft shadow and rolling a half turn away from the click, and the whole picture is the next one about 2.6 seconds later, with no flash or jump at the end. Clicking a corner and clicking the middle both take the same time.
- Clicking many times during a change still gives one change. A click after it lands gives the next one.
- With sound on, the cube cascade plays with each change that starts, never for an ignored click.
- Tab to it and press Enter or Space: the wave starts from the middle, and a focus ring shows.
- It looks right in light and dark mode (check both).
- It fits on a phone without scrolling sideways, a tap works like a click, and the rolling stays smooth.
- With `prefers-reduced-motion: reduce`, the next picture only fades in, with no cubes and no sound.
- Nothing else on the site looks or behaves differently.
- Finish with a short report: the files you added or changed, where it's placed, which pictures it shows, whether sound is on, and every place where you had to do something differently from the source, and why.

## Source

The reference implementation. Copy it as-is, then apply only the changes listed above.

### `components/ui/cube-flip-image.tsx`

```tsx
"use client";

/**
 * Cube flip image, from Impeccable.
 *
 * A picture you click to change. The picture is made of tiny cubes, the
 * current picture on their front faces and the next one on their backs. Click
 * anywhere and a wave spreads out from that exact point: each cube rises toward
 * you, casting a soft shadow, and rolls a half turn away from the click, past a
 * darker side, onto its new face, so the next picture takes over in about 2.4
 * seconds, a wave slow enough to watch. Clicks during a change are ignored: one click, one change.
 *
 * The turning cubes are drawn on one canvas, so even thousands of them stay
 * smooth. Between changes it is a plain image. An optional sound plays with
 * each change.
 *
 * No packages. It fills its box like a cover photo; size it with className or
 * style.
 */

import { useCallback, useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type MouseEvent } from "react";

export type CubeFlipPicture = { src: string; alt: string };

export type CubeFlipImageProps = {
  /** The pictures, in order. Each click shows the next one, and after the last it starts again. */
  images: CubeFlipPicture[];
  /** About how wide each cube is, in pixels. Smaller is finer. Default: 20. */
  tile?: number;
  /** The box's shape, as a CSS aspect-ratio. Ignored when className or style sets the height. Default: "3 / 2". */
  ratio?: string;
  /** A sound file to play with each change, like "/sounds/cube-roll.mp3". Leave it out for silence. */
  sound?: string;
  /** What a click does, for screen readers, read after the picture's alt. Default: "show the next picture". */
  label?: string;
  /** Called with the new picture's index once a change has landed. */
  onChange?: (index: number) => void;
  className?: string;
  style?: CSSProperties;
};

/* ---------- the design: keep as written ---------- */

const ROLL_MS = 760; // one cube rolling a half turn, slow enough to follow
const SWELL_MS = 200; // a cube starts rising this long before it rolls, and settles this long after: the wave's swell
const SPREAD_MS = 1400; // the wave travelling from the click to the farthest corner, slow enough to watch
const JITTER_MS = 40; // a little randomness in each start, so the wave looks hand made, not ruled
const TOTAL_MS = SWELL_MS + SPREAD_MS + JITTER_MS + ROLL_MS + SWELL_MS; // 2.6 seconds, wherever you click
const TILE = 20; // about how wide a cube is, in CSS pixels
const DEPTH = 3; // how far you sit from a cube, in cube sides: smaller looks more 3D (faces narrow more toward their far edge)
const GAP = 0.2; // the wave: each cube shrinks this much at the crest, so a dark gap opens round it and you see it flip on its own
const LIFT = 0.06; // and grows back this much as it rises toward you (the shadow does most of the lifting)
const SHADE = 0.42; // how dark a face gets as it turns away from you
const SIDE = 0.18; // the side face, seen at mid roll, is the new picture this much darker, so the cube reads as solid
const EDGE = 0.2; // the thin dark line where two faces of a cube meet
const BEHIND = 0.62; // the gap round a rising cube shows the new picture, this much darker, so the cubes stand apart
const SHADOW = 0.5; // the soft shadow a risen cube casts on the picture below it, at the crest
const DROP = 0.26; // how far that shadow falls down and to the right, as a share of the cube, at the crest
const HANDOFF_MS = 160; // at the end the drawing fades into the plain image, so the swap never shows
const FADE_MS = 300; // reduced motion: a plain crossfade, no cubes and no sound
const LOUDNESS = 0.6; // 0 to 1, the sound
const MAX_TILES = 2500; // on a very big picture the cubes grow, so it never draws more than this

/** A cube's roll: tips over slowly, falls fastest at the middle, settles softly onto its new face. */
const roll = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);

/** One edge of a cube as you see it: where it sits along the roll (0 to 1 across the cell) and how tall it looks. */
type Edge = { at: number; tall: number };
/** One face of a cube as you see it: its two edges, which picture is on it, and how dark it is. */
type Face = { a: Edge; b: Edge; pic: "old" | "side" | "new"; dark: number };

/**
 * A cube of side 1 turned by `a` (0 to a half turn), seen in perspective from DEPTH sides away, rolling toward +.
 * Its front face holds the old picture, the face that comes round at a quarter turn is the side, and the back face,
 * which arrives at a half turn, holds the new picture. Returns the faces you can see, left to right, sized so the
 * cube fits its cell, then grown by `grow` for the wave (it rises toward you at mid roll). `edges` are the lines
 * where two visible faces meet.
 */
function cubeFaces(a: number, grow: number) {
  const c = Math.cos(a);
  const s = Math.sin(a);
  // The cube's four upright edges, seen from above: x across, z away from you. Turning moves the front away to +.
  const corner = (x: number, z: number) => {
    const rx = x * c - z * s;
    const rz = x * s + z * c;
    const p = DEPTH / (DEPTH + rz); // nearer edges look bigger
    return { x: rx * p, p };
  };
  const A = corner(-0.5, -0.5); // front left
  const B = corner(0.5, -0.5); // front right
  const C = corner(0.5, 0.5); // back right (the new picture's left edge, once it faces you)
  const D = corner(-0.5, 0.5); // back left
  // How squarely each face looks at you: 1 = straight on, 0 or less = edge on or turned away.
  const all = [
    { from: A, to: B, pic: "old" as const, facing: c },
    { from: D, to: A, pic: "side" as const, facing: s },
    { from: C, to: D, pic: "new" as const, facing: -c },
  ].filter((f) => f.to.x - f.from.x > 1e-4);
  let left = Infinity;
  let right = -Infinity;
  for (const f of all) {
    left = Math.min(left, f.from.x);
    right = Math.max(right, f.to.x);
  }
  const k = 1 / (right - left || 1);
  const edge = (e: { x: number; p: number }): Edge => ({ at: 0.5 + ((e.x - left) * k - 0.5) * grow, tall: e.p * k * grow });
  const faces: Face[] = all.map((f) => ({
    a: edge(f.from),
    b: edge(f.to),
    pic: f.pic,
    dark: SHADE * (1 - Math.max(0, f.facing)) + (f.pic === "side" ? SIDE : 0),
  }));
  const edges = faces.slice(1).map((f) => f.a);
  return { faces, edges, left: faces[0]?.a ?? edge(A), right: faces[faces.length - 1]?.b ?? edge(B) };
}

const CSS = `
.cfi{position:relative;display:block;width:100%;padding:0;margin:0;border:0;background:none;overflow:hidden;isolation:isolate;cursor:pointer;-webkit-tap-highlight-color:transparent;touch-action:manipulation;user-select:none;-webkit-user-select:none}
.cfi:focus-visible{outline:2px solid currentColor;outline-offset:3px}
.cfi-img,.cfi-canvas{position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none}
.cfi-img{object-fit:cover}
.cfi-canvas{transition:opacity ${HANDOFF_MS}ms ease-out}
`;

/* ---------- picture maths ---------- */

type Cover = { x: number; y: number; w: number; h: number; s: number };
/** One cube: its cell, when it starts, and which way it rolls (away from the click). */
type Cube = { x: number; y: number; w: number; h: number; delay: number; across: boolean; dir: 1 | -1 };
type Job = { id: number; to: number; from: HTMLImageElement; next: HTMLImageElement; ox: number; oy: number; reduced: boolean };

/** Where a picture sits when it covers a W by H box, centered: its drawn size, top left corner and scale. */
function cover(im: HTMLImageElement, W: number, H: number): Cover {
  const s = Math.max(W / im.naturalWidth, H / im.naturalHeight);
  const w = im.naturalWidth * s;
  const h = im.naturalHeight * s;
  return { x: (W - w) / 2, y: (H - h) / 2, w, h, s };
}

/** The cubes for a W by H canvas, about `size` wide, edges on whole pixels, starting in a wave from (ox, oy). */
function cut(W: number, H: number, size: number, ox: number, oy: number): Cube[] {
  let s = Math.max(4, size);
  if ((W / s) * (H / s) > MAX_TILES) s = Math.sqrt((W * H) / MAX_TILES);
  const cols = Math.max(1, Math.round(W / s));
  const rows = Math.max(1, Math.round(H / s));
  const xs = Array.from({ length: cols + 1 }, (_, i) => Math.round((i * W) / cols));
  const ys = Array.from({ length: rows + 1 }, (_, i) => Math.round((i * H) / rows));
  // The farthest corner from the click, so the wave always takes the same time to cross.
  const far = Math.max(Math.hypot(ox, oy), Math.hypot(W - ox, oy), Math.hypot(ox, H - oy), Math.hypot(W - ox, H - oy)) || 1;
  const cubes: Cube[] = [];
  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      const x = xs[c];
      const y = ys[r];
      const w = xs[c + 1] - x;
      const h = ys[r + 1] - y;
      const dx = x + w / 2 - ox;
      const dy = y + h / 2 - oy;
      const across = Math.abs(dx) >= Math.abs(dy);
      cubes.push({
        x,
        y,
        w,
        h,
        delay: SWELL_MS + (Math.hypot(dx, dy) / far) * SPREAD_MS + Math.random() * JITTER_MS,
        across,
        dir: (across ? dx : dy) < 0 ? -1 : 1,
      });
    }
  }
  return cubes;
}

/** Loads each picture once, so a click can start the change at once. */
const loaded = new Map<string, Promise<HTMLImageElement | null>>();
function load(src: string): Promise<HTMLImageElement | null> {
  let p = loaded.get(src);
  if (!p) {
    p = new Promise((done) => {
      const im = new Image();
      im.decoding = "async";
      const ok = () => done(im.naturalWidth && im.naturalHeight ? im : null);
      // Decoding first keeps the first frames smooth, but a page that isn't being drawn may never finish it: wait 250ms at most.
      im.onload = () => {
        if (typeof im.decode === "function") im.decode().then(ok, ok);
        window.setTimeout(ok, 250);
      };
      im.onerror = () => {
        loaded.delete(src);
        done(null);
      };
      im.src = src;
    });
    loaded.set(src, p);
  }
  return p;
}

/* ---------- the picture ---------- */

export function CubeFlipImage({
  images,
  tile = TILE,
  ratio = "3 / 2",
  sound,
  label = "show the next picture",
  onChange,
  className,
  style,
}: CubeFlipImageProps) {
  const root = useRef<HTMLButtonElement>(null);
  const img = useRef<HTMLImageElement>(null);
  const canvas = useRef<HTMLCanvasElement>(null);
  const audio = useRef<HTMLAudioElement | null>(null);
  const [index, setIndex] = useState(0);
  const [run, setRun] = useState<{ id: number; ending: boolean } | null>(null);
  const job = useRef<Job | null>(null);
  const busy = useRef(false); // a change is loading or running: clicks are ignored until it lands
  const land = useRef<(() => void) | null>(null);
  const ids = useRef(0);
  const alive = useRef(true);
  const live = useRef({ tile, onChange });
  useEffect(() => {
    live.current = { tile, onChange };
  }, [tile, onChange]);

  const count = images.length;
  const at = count ? Math.min(index, count - 1) : 0;
  const pic = images[at];
  const nextSrc = count > 1 ? images[(at + 1) % count].src : null;

  // Fetch the next picture early, so a click can start the change the moment it lands.
  useEffect(() => {
    if (nextSrc) load(nextSrc);
  }, [nextSrc]);

  // The sound: one Audio element, fetched on the first click.
  useEffect(() => {
    if (!sound) {
      audio.current = null;
      return;
    }
    const a = new Audio(sound);
    a.preload = "none"; // fetched on the first click (12KB), so pages that never click download nothing
    a.volume = LOUDNESS;
    audio.current = a;
    return () => {
      a.pause();
      if (audio.current === a) audio.current = null;
    };
  }, [sound]);

  const play = () => {
    const a = audio.current;
    if (!a) return;
    try {
      a.currentTime = 0;
      a.play()?.catch(() => {}); // a phone on silent, or a browser that blocks it: stay quiet
    } catch {
      // no sound support: the picture still changes
    }
  };

  // A click (or Enter / Space): start the wave from that point, unless a change is already running.
  const click = (e: MouseEvent<HTMLButtonElement>) => {
    // A click on the picture changes the picture, even when it sits inside a link (like a card).
    e.preventDefault();
    if (busy.current || count < 2 || !root.current) return;
    const from = img.current;
    const box = root.current.getBoundingClientRect();
    if (!from || !from.complete || !from.naturalWidth || box.width < 1 || box.height < 1) return;
    busy.current = true;
    // A keyboard press has no point on the picture (detail 0): start from the middle.
    const keyboard = e.detail === 0;
    const ox = keyboard ? box.width / 2 : Math.min(box.width, Math.max(0, e.clientX - box.left));
    const oy = keyboard ? box.height / 2 : Math.min(box.height, Math.max(0, e.clientY - box.top));
    const to = (at + 1) % count;
    const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    // Sound starts inside the click itself, because browsers only allow sound that starts from one.
    if (!reduced) play();
    load(images[to].src).then((im) => {
      if (!alive.current) return;
      // The next picture failed to load, or the tab is hidden: just show it.
      if (!im || document.hidden) {
        setIndex(to);
        busy.current = false;
        live.current.onChange?.(to);
        return;
      }
      const id = ++ids.current;
      job.current = { id, to, from, next: im, ox, oy, reduced };
      setRun({ id, ending: false });
    });
  };

  // The change: drawn frame by frame on the canvas, then handed back to the plain image.
  useLayoutEffect(() => {
    const cv = canvas.current;
    const box = root.current;
    const j = job.current;
    if (!run || run.ending || !cv || !box || !j || j.id !== run.id) return;
    const r = box.getBoundingClientRect();
    const dpr = Math.min(2, window.devicePixelRatio || 1);
    const W = Math.max(1, Math.round(r.width * dpr));
    const H = Math.max(1, Math.round(r.height * dpr));
    cv.width = W;
    cv.height = H;
    const ctx = cv.getContext("2d");
    if (ctx) ctx.imageSmoothingQuality = "high";
    const old = cover(j.from, W, H);
    const nw = cover(j.next, W, H);
    const cubes = j.reduced ? [] : cut(W, H, live.current.tile * dpr, j.ox * dpr, j.oy * dpr);
    const total = j.reduced ? FADE_MS : TOTAL_MS;
    const whole = (im: HTMLImageElement, c: Cover) => ctx!.drawImage(im, c.x, c.y, c.w, c.h);
    /**
     * One face of a rolling cube, from edge A to edge B: the cube's own piece of a picture, drawn in thin strips so
     * it narrows toward its far edge (perspective), then darkened by `dark`. `along` is the cell's size in the roll's
     * direction, `cross` the other way. Rolling backward (dir -1) mirrors the positions, never the picture.
     */
    const face = (im: HTMLImageElement, c: Cover, q: Cube, A: Edge, B: Edge, dark: number) => {
      const along = q.across ? q.w : q.h;
      const cross = q.across ? q.h : q.w;
      const span = (B.at - A.at) * along;
      if (span < 0.25) return;
      const strips = Math.max(1, Math.min(8, Math.ceil(span / 5)));
      for (let i = 0; i < strips; i++) {
        const t0 = i / strips;
        const t1 = (i + 1) / strips;
        let f0 = A.at + (B.at - A.at) * t0;
        let f1 = A.at + (B.at - A.at) * t1;
        let u0 = t0;
        let u1 = t1;
        if (q.dir < 0) [f0, f1, u0, u1] = [1 - f1, 1 - f0, 1 - t1, 1 - t0];
        const tall = A.tall + (B.tall - A.tall) * ((t0 + t1) / 2);
        const lo = ((1 - tall) / 2) * cross;
        // Source: the strip of this cube's piece of the picture. Destination: where that strip sits now.
        const [sx, sy, sw, sh, dx, dy, dw, dh] = q.across
          ? [q.x + u0 * q.w, q.y, (u1 - u0) * q.w, q.h, q.x + f0 * q.w, q.y + lo, (f1 - f0) * q.w + 0.35, tall * q.h]
          : [q.x, q.y + u0 * q.h, q.w, (u1 - u0) * q.h, q.x + lo, q.y + f0 * q.h, tall * q.w, (f1 - f0) * q.h + 0.35];
        ctx!.drawImage(im, (sx - c.x) / c.s, (sy - c.y) / c.s, sw / c.s, sh / c.s, dx, dy, dw, dh);
      }
      if (dark <= 0.01) return;
      // The shade, once over the whole face as one tapered shape, so the strips never show as stripes.
      const corner = (f: number, side: number, tall: number) => {
        const at = (q.dir < 0 ? 1 - f : f) * along;
        const off = ((1 - tall) / 2 + side * tall) * cross;
        return q.across ? [q.x + at, q.y + off] : [q.x + off, q.y + at];
      };
      const pts = [corner(A.at, 0, A.tall), corner(B.at, 0, B.tall), corner(B.at, 1, B.tall), corner(A.at, 1, A.tall)];
      ctx!.beginPath();
      ctx!.moveTo(pts[0][0], pts[0][1]);
      for (const p of pts.slice(1)) ctx!.lineTo(p[0], p[1]);
      ctx!.closePath();
      ctx!.fillStyle = `rgba(0,0,0,${dark})`;
      ctx!.fill();
    };

    const draw = (t: number) => {
      if (!ctx) return;
      ctx.clearRect(0, 0, W, H);
      ctx.globalAlpha = 1;
      if (j.reduced) {
        whole(j.from, old);
        ctx.globalAlpha = Math.min(1, t / FADE_MS);
        whole(j.next, nw);
        ctx.globalAlpha = 1;
        return;
      }
      // 1. The new picture everywhere: the cubes that have landed.
      whole(j.next, nw);
      // 2. The old picture, in one piece, on every cube the wave's swell hasn't reached, so no line shows between them.
      const rolling: Cube[] = [];
      ctx.save();
      ctx.beginPath();
      let waiting = false;
      for (const q of cubes) {
        if (t < q.delay - SWELL_MS) {
          ctx.rect(q.x, q.y, q.w, q.h);
          waiting = true;
        } else if (t < q.delay + ROLL_MS + SWELL_MS) rolling.push(q);
      }
      if (waiting) {
        ctx.clip();
        whole(j.from, old);
      }
      ctx.restore();
      if (!rolling.length) return;
      // 3. Under each rolling cube, the new picture a little darker: the gap a risen cube leaves.
      ctx.beginPath();
      for (const q of rolling) ctx.rect(q.x, q.y, q.w, q.h);
      ctx.fillStyle = `rgba(0,0,0,${BEHIND})`;
      ctx.fill();
      // Each cube in the wave: it swells up, rolls half a turn at the crest, then settles back down, like water.
      const shapes = rolling.map((q) => {
        const p = Math.min(1, Math.max(0, (t - q.delay) / ROLL_MS));
        const u = (t - q.delay + SWELL_MS) / (ROLL_MS + 2 * SWELL_MS);
        const rise = Math.sin(Math.PI * u) ** 2; // 0 flat, 1 at the crest, easing in and out with no kink
        return { q, rise, cube: cubeFaces(roll(p) * Math.PI, 1 - (GAP - LIFT) * rise) };
      });
      // 4. The wave's shadow: each risen cube casts a soft shadow down and to the right, darker and farther the higher it is.
      for (const { q, rise, cube } of shapes) {
        if (rise < 0.02) continue;
        const along = q.across ? q.w : q.h;
        const cross = q.across ? q.h : q.w;
        const f0 = q.dir > 0 ? cube.left.at : 1 - cube.right.at;
        const f1 = q.dir > 0 ? cube.right.at : 1 - cube.left.at;
        const tall = Math.max(cube.left.tall, cube.right.tall);
        const lo = ((1 - tall) / 2) * cross;
        const [x, y, w, h] = q.across
          ? [q.x + f0 * q.w, q.y + lo, (f1 - f0) * q.w, tall * q.h]
          : [q.x + lo, q.y + f0 * q.h, tall * q.w, (f1 - f0) * q.h];
        const off = DROP * rise * Math.max(along, cross);
        // Three layers, widest and faintest first, so the shadow has a soft edge without a costly blur.
        const soft = Math.max(1, off);
        for (const [grow, share] of [
          [1, 0.25],
          [0.5, 0.3],
          [0, 0.45],
        ]) {
          const g = soft * grow;
          ctx.fillStyle = `rgba(0,0,0,${SHADOW * rise * share})`;
          ctx.fillRect(x + off - g, y + off - g, w + g * 2, h + g * 2);
        }
      }
      // 5. The cubes, lowest first, so a cube at the crest is drawn over its neighbors. The old face turns away, the
      //    darker side comes round at a quarter turn, then the new face arrives at a half turn, each face narrowing
      //    toward its far edge.
      shapes.sort((m, n) => m.rise - n.rise);
      for (const { q, cube } of shapes) {
        for (const f of cube.faces) {
          if (f.pic === "old") face(j.from, old, q, f.a, f.b, f.dark);
          else face(j.next, nw, q, f.a, f.b, f.dark);
        }
        // The thin line where two faces meet, as tall as that edge looks.
        const along = q.across ? q.w : q.h;
        const cross = q.across ? q.h : q.w;
        ctx.fillStyle = `rgba(0,0,0,${EDGE})`;
        for (const e of cube.edges) {
          const at = (q.dir > 0 ? e.at : 1 - e.at) * along;
          const tall = e.tall * cross;
          const lo = (cross - tall) / 2;
          if (q.across) ctx.fillRect(q.x + at - 0.5, q.y + lo, 1, tall);
          else ctx.fillRect(q.x + lo, q.y + at - 0.5, tall, 1);
        }
      }
    };

    let done = false;
    let raf = 0;
    let guard = 0;
    const start = performance.now();
    const finish = () => {
      if (done) return;
      done = true;
      cancelAnimationFrame(raf);
      clearTimeout(guard);
      land.current = null;
      if (ctx) {
        ctx.clearRect(0, 0, W, H);
        whole(j.next, nw);
      }
      if (!alive.current) return;
      // The plain image takes the new picture under the canvas, then the canvas fades away over it.
      setIndex(j.to);
      setRun((x) => (x && x.id === j.id ? { ...x, ending: true } : x));
      window.setTimeout(() => {
        if (!alive.current) return;
        setRun((x) => (x && x.id === j.id ? null : x));
        busy.current = false;
        live.current.onChange?.(j.to);
      }, HANDOFF_MS + 40);
    };
    const tick = () => {
      const t = performance.now() - start;
      if (t >= total) return finish();
      draw(t);
      raf = requestAnimationFrame(tick);
    };
    // A background tab pauses frames: never wait longer than the change itself.
    guard = window.setTimeout(finish, total + 500);
    land.current = finish;
    if (!ctx) finish();
    else {
      draw(0);
      raf = requestAnimationFrame(tick);
    }
    return () => {
      if (done) return;
      done = true;
      cancelAnimationFrame(raf);
      clearTimeout(guard);
      land.current = null;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [run?.id]);

  // The box changes size mid change (a phone turned, a window resized): land at once rather than draw out of place.
  useEffect(() => {
    const box = root.current;
    if (!box || typeof ResizeObserver === "undefined") return;
    let last = "";
    const ro = new ResizeObserver(([e]) => {
      const key = `${Math.round(e.contentRect.width)}x${Math.round(e.contentRect.height)}`;
      if (last && key !== last) land.current?.();
      last = key;
    });
    ro.observe(box);
    return () => ro.disconnect();
  }, []);

  useEffect(() => {
    alive.current = true;
    return () => {
      alive.current = false;
      busy.current = false;
    };
  }, []);

  if (!pic) return null;
  const drawing = !!run && !run.ending;

  return (
    <button
      ref={root}
      type="button"
      onClick={click}
      aria-label={`${pic.alt}, ${label}`}
      className={["cfi", className].filter(Boolean).join(" ")}
      style={{ aspectRatio: ratio, ...style }}
    >
      <style>{CSS}</style>
      {/* Hidden while the canvas draws the change, so a see-through picture never shows twice. */}
      <img ref={img} className="cfi-img" src={pic.src} alt="" draggable={false} style={drawing ? { opacity: 0 } : undefined} />
      {run && <canvas ref={canvas} className="cfi-canvas" aria-hidden="true" style={run.ending ? { opacity: 0 } : undefined} />}
    </button>
  );
}

export default CubeFlipImage;
```

Usage

Pass your pictures to images and size the box with className. For the sound, download cube-roll.mp3 (26KB) into your public folder and pass its path to sound. For a picture that changes on its own, use Tile flip image.

"use client";
import { CubeFlipImage } from "@/components/ui/cube-flip-image";

const SHOTS = [
  { src: "/shots/lake.webp", alt: "A rowing boat on a mountain lake" },
  { src: "/shots/city.webp", alt: "A city skyline at night" },
  { src: "/shots/road.webp", alt: "A desert road toward the mountains" },
];

// Each click rolls the cubes out from that spot to the next picture.
// The sound file: download impeccable-ui.com/cube-flip-image/cube-roll.mp3 into public/sounds.
export function PhotoWall() {
  return <CubeFlipImage images={SHOTS} sound="/sounds/cube-roll.mp3" className="w-full max-w-xl rounded-2xl" />;
}

How it moves

One click, one wave, rolling out from the exact spot you clicked like a wave on water.

The picture is made of tiny cubes about 20 pixels wide, the current picture on their fronts and the next one on their backs. A click sends a slow wave out from that spot, crossing to the farthest corner in 1.4 seconds, so you can watch it travel. As the wave reaches a cube, a dark gap opens round it so you see it on its own, and it swells up toward you, casting a soft shadow down and to the right that makes it look lifted. At the crest it rolls a half turn away from the click in 0.76 seconds: the old face turns away, a darker side face comes round, then the new face arrives. Then it settles back down flat, like water after a wave. Every face is seen in perspective, narrowing toward its far edge. The last cube settles at about 2.6 seconds, then the drawing fades into a plain image so the end never blinks. Clicks during a change are ignored. The sound, a cascade of small cube clacks made with ElevenLabs, plays with each click that starts a change. With reduced motion no cube rolls and no sound plays: the next picture simply fades in over 0.3 seconds.

Props

PropDefaultWhat it does
images—The pictures, each with src and alt, in order. Each click shows the next one, then it starts again.
sound—A sound file to play with each change, like "/sounds/cube-roll.mp3". Leave it out for silence.
tile20About how wide each cube is, in pixels. Smaller is finer, larger shows the cubes more. A very big picture never gets more than 2,500.
ratio"3 / 2"The box's shape, as a CSS aspect ratio like "16 / 9". The picture covers it, centered. Ignored when you set the height yourself.
label"show the next picture"What a click does, read by screen readers after the picture's alt.
onChange—Called with the new picture's index once a change has landed.
className / style—On the box: its width, height and corners. The box clips the cubes, so rounded corners stay round while they roll.