Number 37: Zoom into a photo where you point
Specification
- Catalogue
- No. 37 of 110
- Section
- Media and galleries
- Interaction
- Press
- Element
- Media
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Click a spot and the whole photo zooms in on it; move to look around, click again to zoom out.
For detail that needs the whole frame, such as a glaze or a texture. The zoom centres on the point the visitor chose, so they land on what they wanted to see.
2.5 times zoom in 400ms, cubic-bezier(.22, 1, .36, 1), from the point clicked. Mouse: move to pan. Phone: double tap to zoom, drag to pan. Keyboard: Enter, then arrows; Escape zooms out. Unlike the lens, the whole frame zooms; the lens keeps the context.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useRef, useState, type PointerEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./image-zoom.css";
const S = "/samples";
const ZOOM = 2.5;
function ImageZoomDemo() {
const [zoomed, setZoomed] = useState(false);
const [origin, setOrigin] = useState({ x: 50, y: 50 });
const lastTap = useRef(0);
const lastPointer = useRef("");
const panning = useRef<{ x: number; y: number; ox: number; oy: number; id: number } | null>(null);
const at = (e: PointerEvent<HTMLElement>) => {
const r = e.currentTarget.getBoundingClientRect();
return { x: ((e.clientX - r.left) / r.width) * 100, y: ((e.clientY - r.top) / r.height) * 100 };
};
const clamp = (v: number) => Math.min(100, Math.max(0, v));
const onPointerDown = (e: PointerEvent<HTMLButtonElement>) => {
lastPointer.current = e.pointerType;
if (e.pointerType === "mouse") return;
// Touch: double tap toggles, a drag pans while zoomed.
const now = performance.now();
if (now - lastTap.current < 300) {
setOrigin(at(e));
setZoomed((z) => !z);
lastTap.current = 0;
return;
}
lastTap.current = now;
if (zoomed) {
panning.current = { x: e.clientX, y: e.clientY, ox: origin.x, oy: origin.y, id: e.pointerId };
e.currentTarget.setPointerCapture(e.pointerId);
}
};
const onPointerMove = (e: PointerEvent<HTMLButtonElement>) => {
if (e.pointerType === "mouse") {
if (zoomed) setOrigin(at(e));
return;
}
const p = panning.current;
if (!p || p.id !== e.pointerId) return;
const r = e.currentTarget.getBoundingClientRect();
setOrigin({ x: clamp(p.ox - ((e.clientX - p.x) / r.width) * 100), y: clamp(p.oy - ((e.clientY - p.y) / r.height) * 100) });
};
return (
<div className="dm">
<div className="zoom">
<button
type="button"
className="zoom-frame"
data-zoomed={zoomed || undefined}
aria-pressed={zoomed}
aria-label={zoomed ? "Zoom out of the bowl photo" : "Zoom into the bowl photo"}
onClick={(e) => {
// Mouse and keyboard: click toggles. Touch uses double tap (above).
if (e.detail > 0 && lastPointer.current !== "mouse") return;
if (e.detail > 0) {
const r = e.currentTarget.getBoundingClientRect();
setOrigin({ x: ((e.clientX - r.left) / r.width) * 100, y: ((e.clientY - r.top) / r.height) * 100 });
}
setZoomed((z) => !z);
}}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={() => (panning.current = null)}
onKeyDown={(e) => {
lastPointer.current = "";
if (!zoomed) return;
const d: Record<string, [number, number]> = { ArrowLeft: [-8, 0], ArrowRight: [8, 0], ArrowUp: [0, -8], ArrowDown: [0, 8] };
const m = d[e.key];
if (m) {
e.preventDefault();
setOrigin((o) => ({ x: clamp(o.x + m[0]), y: clamp(o.y + m[1]) }));
}
if (e.key === "Escape") setZoomed(false);
}}
>
<img
src={`${S}/bowl.svg`}
alt="A wide bowl with a pale green crackle glaze and brown drips from the rim"
draggable={false}
style={{ transformOrigin: `${origin.x}% ${origin.y}%`, scale: zoomed ? String(ZOOM) : "1" }}
/>
</button>
<p className="dm-hint">{zoomed ? "Move to look around. Click, Escape or double tap to zoom out" : "Click where you want to look, or double tap on a phone"}</p>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: ".zoom-frame", wait: 600 },
{ sweep: ".zoom-frame", from: [0.3, 0.35], to: [0.7, 0.65], ms: 1600, wait: 500 },
{ click: ".zoom-frame", wait: 600 },
];
export default function ImageZoomEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Zoom into a photo where you point" sample tour={TOUR}>
<ImageZoomDemo />
</PieceStage>
);
}