Tile flip image
A picture that turns over in tiny tiles, in random order, onto the next one in 1.2 seconds. Use it on a hero or product shot that changes.

npx shadcn@latest add https://impeccable-ui.com/r/tile-flip-image.jsonRun it in a project set up with shadcn. No other packages needed. Or open and copy the file into components/ui/tile-flip-image.tsx.
Tile sizes
Three sizes, playing the same pictures. Fine tiles turn like a shimmer, the default reads as tiny tiles, and bold ones are blocks you can follow one by one.

tile={8}
tile={14}
tile={28}<TileFlipImage src={src} alt={alt} tile={8} /> // fine
<TileFlipImage src={src} alt={alt} /> // default, about 14px
<TileFlipImage src={src} alt={alt} tile={28} /> // boldPrompts
Paste one into Claude Code, Cursor or any AI coding tool. Install adds this exact picture and asks only where it goes and which pictures it shows. Blend asks four short questions (where, the tile size, the shape and the corners), then fits it to your product without changing how the tiles turn.
# Tile 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 that changes by tiles: give it a new picture and it splits into tiny square tiles (about 14px each), and every tile turns over on its side like a card, the old picture on its front and the new one on its back. The tiles start in a random order that builds up and eases off, each one turns in 0.56 seconds, darkening as it goes edge on, and the last one lands 1.2 seconds after the first started. Between changes it is a plain image.
## Before you build: one question
Look through this project first: its hero, product or feature pictures, galleries, carousels, and any place that already swaps one picture for another (a slideshow, a product color switch, a before and after). Then ask me this in one message and wait for my answer:
- **Where should it go, and which pictures does it show?** Suggest the 1–3 pictures in my code it fits best, what makes each one change (a timer, a button, a tab), and the pictures it would switch between. I can pick one or name another.
*Why: the tiles only turn when the picture changes, so it belongs on a picture that changes, 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 square tiles (about 14px by default), each turning over 180 degrees on its upright axis with the old picture on its front and the new one on its back, the random order that builds up and eases off, the darkening as a face turns edge on and the darker new picture in the gap, the 0.56 second turn, the 1.2 seconds for the whole picture, 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 and the one place I picked. If it replaces an existing picture there, pass that same picture to `src` and `alt` and remove only the old image. 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 change every 3 seconds, the Next picture button, the dotted card and the home page's TV.
## Fastest path
If this project has a `components.json` (shadcn), run `npx shadcn@latest add https://impeccable-ui.com/r/tile-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, and its styles sit in a `<style>` tag inside the file.
- JavaScript project? Name the file `tile-flip-image.jsx` and remove the type annotations; nothing else changes.
- Create this file, in this project's usual place for components:
- `components/ui/tile-flip-image.tsx`
- Keep these exports and their names: `TileFlipImage` (also the default export) and `TileFlipImageProps`.
- 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: `TOTAL_MS`, `FLIP_MS`, `TILE`, `SHADE`, `BEHIND`, `HANDOFF_MS`, `FADE_MS`, `MAX_TILES`, the `turn` and `wave` curves 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. With `prefers-reduced-motion: reduce` no tile turns: the new picture simply fades in over 0.3 seconds. If a picture fails to load, or the tab is hidden, it swaps with no motion. Screen readers always get one plain image with its `alt`.
## Use it
```tsx
"use client";
import { useState } from "react";
import { TileFlipImage } from "@/components/ui/tile-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" },
];
// Give it a new src and the tiles turn over to it. The box takes its size from className.
export function ShotSwitcher() {
const [i, setI] = useState(0);
return (
<div>
<TileFlipImage src={SHOTS[i].src} alt={SHOTS[i].alt} className="w-full max-w-xl rounded-2xl" />
<button type="button" onClick={() => setI((i + 1) % SHOTS.length)}>Next</button>
</div>
);
}
```
- `src` and `alt` are the picture, like an `<img>`; give it a new `src` and the tiles turn over to it.
- `tile` is about how wide each tile is, in pixels (14 by default). Smaller is finer, larger is bolder.
- `ratio` is the box's shape as a CSS aspect ratio (`"3 / 2"` by default). The picture covers the box, centered.
- Corners and size come from `className` or `style`. The box clips the tiles, so rounded corners stay round while they turn.
- Use pictures of about the same shape, so the old and new 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 one plain `<img>` that covers its box, centered, like a cover photo. Nothing runs and there is no canvas.
2. **Loading first.** When `src` changes, the new picture loads before anything moves, so no tile ever turns to an empty back.
3. **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. Thousands of tiny tiles stay smooth this way, where thousands of separate page elements would stutter.
4. **The tiles.** The box is cut into square tiles about `tile` pixels wide (14 by default), edges on whole pixels. On a very big picture the tiles grow so there are never more than 3,000. Tiles that haven't started are drawn as one piece of the old picture, and tiles that have landed as one piece of the new one, so no line ever shows between them.
5. **The turn.** Each tile turns over 180 degrees on its upright axis in 0.56 seconds: it lifts off gently, turns fastest when edge on and lands softly (an ease in and out). The old picture is on its front and the same piece of the new one on its back, and each face darkens up to 30% as it turns edge on. The gap beside a turning tile shows the new picture 20% darker, like depth, never the page behind.
6. **The order.** The tiles are shuffled, and their starts follow a soft curve over the first 0.64 seconds: a few first, most in the middle, a few last. So the whole change eases in and out, and the last tile lands at 1.2 seconds.
7. **Landing.** The last frame is the whole new picture. The plain image takes the new picture under the canvas, then the canvas fades away over it in 0.16 seconds, so the swap never shows.
8. **Fast changes.** A picture asked for during a change waits for that change to land, then the tiles turn to the latest one; pictures in between are skipped. 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.
- When the picture changes, the new one has fully loaded first, then small tiles turn over one by one in a random order, and the whole picture is the new one about 1.2 seconds later, with no blink at the end.
- It looks right in light and dark mode (check both), and there is no flash or jump when the last tiles land.
- It fits on a phone without scrolling sideways, and the turning stays smooth there.
- With `prefers-reduced-motion: reduce`, the new picture only fades in, with no tiles.
- 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, 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/tile-flip-image.tsx`
```tsx
"use client";
/**
* Tile flip image, from Impeccable.
*
* A picture that changes by tiles. Give it a new `src` and the picture splits
* into tiny square tiles. Each tile turns over on its side like a card, the old
* picture on its front and the new one on its back, in a random order that
* builds up and then eases off, and the whole picture is replaced in 1.2
* seconds. Between changes it is a plain image.
*
* The turning tiles are drawn on one canvas, so even thousands of them stay
* smooth, and tiles that are not turning are drawn as one piece of picture, so
* no line ever shows between them.
*
* No packages. It fills its box like a cover photo; size it with className or
* style.
*/
import { useCallback, useEffect, useLayoutEffect, useRef, useState, type CSSProperties } from "react";
export type TileFlipImageProps = {
/** The picture to show. Give it a new one and the tiles turn over to it. */
src: string;
/** What the picture shows, for screen readers. */
alt: string;
/** About how wide each tile is, in pixels. Smaller is finer. Default: 14. */
tile?: number;
/** The box's shape, as a CSS aspect-ratio. Ignored when className or style sets the height. Default: "3 / 2". */
ratio?: string;
className?: string;
style?: CSSProperties;
};
/* ---------- the design: keep as written ---------- */
const TOTAL_MS = 1200; // from the first tile starting to the last one landing
const FLIP_MS = 560; // one tile turning over
const TILE = 14; // about how wide a tile is, in CSS pixels
const SHADE = 0.3; // how dark a face gets as it turns edge on, so the turn reads in 3D
const BEHIND = 0.2; // the gap behind a turning tile shows the new picture, this much darker
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
const MAX_TILES = 3000; // on a very big picture the tiles grow, so it never draws more than this
/** Each tile: lifts off gently, turns fastest when edge on, lands softly. */
const turn = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
/** When the tiles start: a few at first, most in the middle, a few last, so the whole change eases in and out. */
const wave = (t: number) => (1 - Math.cos(Math.PI * t)) / 2;
const CSS = `
.tfi{position:relative;display:block;width:100%;overflow:hidden;isolation:isolate}
.tfi-img,.tfi-canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
.tfi-img{object-fit:cover}
.tfi-canvas{pointer-events:none;transition:opacity ${HANDOFF_MS}ms ease-out}
`;
/* ---------- picture maths ---------- */
type Pic = { src: string; alt: string };
type Cover = { x: number; y: number; w: number; h: number; s: number };
type Tile = { x: number; y: number; w: number; h: number; delay: number };
type Job = { id: number; to: Pic; from: HTMLImageElement; next: HTMLImageElement; 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 tiles for a W by H canvas, each about `size` wide, edges on whole pixels, starts shuffled. */
function cut(W: number, H: number, size: number): Tile[] {
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));
const n = cols * rows;
const order = Array.from({ length: n }, (_, i) => i);
for (let i = n - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[order[i], order[j]] = [order[j], order[i]];
}
const spread = TOTAL_MS - FLIP_MS;
return order.map((cell, rank) => {
const c = cell % cols;
const r = Math.floor(cell / cols);
return {
x: xs[c],
y: ys[r],
w: xs[c + 1] - xs[c],
h: ys[r + 1] - ys[r],
delay: n > 1 ? wave(rank / (n - 1)) * spread : 0,
};
});
}
/** Loads a picture before any tile turns, so no tile turns to an empty back. Null if it can't load. */
function load(src: string): Promise<HTMLImageElement | null> {
return 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 = () => done(null);
im.src = src;
});
}
/* ---------- the picture ---------- */
export function TileFlipImage({ src, alt, tile = TILE, ratio = "3 / 2", className, style }: TileFlipImageProps) {
const root = useRef<HTMLDivElement>(null);
const img = useRef<HTMLImageElement>(null);
const canvas = useRef<HTMLCanvasElement>(null);
const [shown, setShown] = useState<Pic>({ src, alt });
const [run, setRun] = useState<{ id: number; ending: boolean } | null>(null);
const job = useRef<Job | null>(null);
const now = useRef<Pic>({ src, alt }); // what is on screen (or landing)
const want = useRef<Pic>({ src, alt }); // the latest picture asked for
const busy = useRef(false); // loading or turning: a newer picture waits for this one to land
const land = useRef<(() => void) | null>(null); // lands the change now (used when the box resizes)
const ids = useRef(0);
const alive = useRef(true);
const tileRef = useRef(tile);
useEffect(() => {
tileRef.current = tile;
}, [tile]);
// Shows a picture at once, with no tiles.
const put = useCallback((pic: Pic) => {
now.current = pic;
setShown(pic);
setRun(null);
busy.current = false;
}, []);
// Starts the change to the latest picture, if it differs from what is on screen.
const go = useCallback(() => {
if (busy.current || !alive.current) return;
const next = want.current;
if (next.src === now.current.src) {
if (next.alt !== now.current.alt) put(next);
return;
}
busy.current = true;
load(next.src).then((im) => {
if (!alive.current) return;
// A newer picture was asked for while this one loaded: go straight to that one.
if (want.current.src !== next.src) {
busy.current = false;
return go();
}
const from = img.current;
const box = root.current?.getBoundingClientRect();
// The old picture never loaded, the new one failed, no size yet or the tab is hidden: just swap.
if (!im || !from || !from.complete || !from.naturalWidth || !box || box.width < 1 || box.height < 1 || document.hidden) {
put(next);
return go();
}
const id = ++ids.current;
job.current = { id, to: next, from, next: im, reduced: window.matchMedia("(prefers-reduced-motion: reduce)").matches };
setRun({ id, ending: false });
});
}, [put]);
// A new src or alt from outside.
useEffect(() => {
want.current = { src, alt };
go();
}, [src, alt, go]);
// 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 tiles = j.reduced ? [] : cut(W, H, tileRef.current * 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);
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 tiles that have landed.
whole(j.next, nw);
// 2. The old picture, in one piece, on every tile that hasn't started, so no line shows between them.
const turning: Tile[] = [];
ctx.save();
ctx.beginPath();
let waiting = false;
for (const tl of tiles) {
if (t < tl.delay) {
ctx.rect(tl.x, tl.y, tl.w, tl.h);
waiting = true;
} else if (t < tl.delay + FLIP_MS) turning.push(tl);
}
if (waiting) {
ctx.clip();
whole(j.from, old);
}
ctx.restore();
if (!turning.length) return;
// 3. Behind each turning tile, the new picture a little darker, like a gap with depth.
ctx.beginPath();
for (const tl of turning) ctx.rect(tl.x, tl.y, tl.w, tl.h);
ctx.fillStyle = `rgba(0,0,0,${BEHIND})`;
ctx.fill();
// 4. The turning tiles: squeezed by how far they have turned, old face first, then new, darker when edge on.
for (const tl of turning) {
const c = Math.cos(turn((t - tl.delay) / FLIP_MS) * Math.PI);
const a = Math.abs(c);
const dw = tl.w * a;
if (dw < 0.25) continue;
const im = c > 0 ? j.from : j.next;
const at = c > 0 ? old : nw;
const dx = tl.x + (tl.w - dw) / 2;
ctx.drawImage(im, (tl.x - at.x) / at.s, (tl.y - at.y) / at.s, tl.w / at.s, tl.h / at.s, dx, tl.y, dw, tl.h);
const shade = SHADE * (1 - a);
if (shade > 0.01) {
ctx.fillStyle = `rgba(0,0,0,${shade})`;
ctx.fillRect(dx, tl.y, dw, tl.h);
}
}
};
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.
now.current = j.to;
setShown(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;
go();
}, 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;
};
}, []);
const drawing = !!run && !run.ending;
return (
<div ref={root} className={["tfi", 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="tfi-img" src={shown.src} alt={shown.alt} draggable={false} style={drawing ? { opacity: 0 } : undefined} />
{run && <canvas ref={canvas} className="tfi-canvas" aria-hidden="true" style={run.ending ? { opacity: 0 } : undefined} />}
</div>
);
}
export default TileFlipImage;
```
Usage
Pass the picture to src and alt, and size the box with className. Use pictures of about the same shape, so the old and new line up the same way.
"use client";
import { TileFlipImage } from "@/components/ui/tile-flip-image";
// Give it a new src and the tiles turn over to it.
// The box covers its width at 3:2; corners come from className.
export function ProductShot({ shot }: { shot: { src: string; alt: string } }) {
return <TileFlipImage src={shot.src} alt={shot.alt} className="w-full max-w-xl rounded-2xl" />;
}How it moves
Nothing moves until the new picture has fully loaded. Then the whole picture changes in 1.2 seconds.
The picture is cut into tiny square tiles about 14 pixels wide, each holding its own piece of the old picture on its front and the same piece of the new one on its back. The tiles are shuffled, and their starts follow a soft curve: a few first, most in the middle, a few last, so the whole change eases in and out. Each one turns over on its upright axis in 0.56 seconds: it lifts off gently, turns fastest when edge on and lands softly, darkening as it goes edge on, with the new picture a little darker in the gap beside it. The last tile lands at 1.2 seconds, and the drawing fades into a plain image in 0.16 seconds, so the end never blinks and nothing runs between changes. It is all drawn on one canvas, so thousands of tiles stay smooth, even on a phone. A picture asked for mid change waits for that change to land. With reduced motion no tile turns: the new picture simply fades in over 0.3 seconds.
Props
| Prop | Default | What it does |
|---|---|---|
src | — | The picture to show. Give it a new one and the tiles turn over to it, once it has loaded. |
alt | — | What the picture shows, for screen readers, like on an image. |
tile | 14 | About how wide each tile is, in pixels. Smaller is finer, larger is bolder. A very big picture never gets more than 3,000 tiles. |
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. |
className / style | — | On the box: its width, height and corners. The box clips the tiles, so rounded corners stay round while they turn. |