Specification
- Catalogue
- No. 14 of 110
- Section
- Media and galleries
- Interaction
- Drag
- Element
- Media
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Drag a ring onto the subject and see how the photo crops as a square, a portrait and a banner.
One photo is used in several shapes, and the subject should survive each one. The previews update as the ring moves.
Crops follow with object-position, no delay. 44px ring; arrow keys move it 2%, Shift for 10%. Tap or drag anywhere on the photo to move it.
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 "./focal-point.css";
const S = "/samples";
const CROPS = [
{ id: "square", label: "Square post", ratio: "1 / 1" },
{ id: "portrait", label: "Portrait print", ratio: "4 / 5" },
{ id: "banner", label: "Banner", ratio: "16 / 7" },
];
function FocalPointDemo() {
const [pt, setPt] = useState({ x: 50, y: 50 });
const dragging = useRef<number | null>(null);
const clamp = (v: number) => Math.round(Math.min(100, Math.max(0, v)));
const at = (e: PointerEvent<HTMLDivElement>) => {
const r = e.currentTarget.getBoundingClientRect();
setPt({ x: clamp(((e.clientX - r.left) / r.width) * 100), y: clamp(((e.clientY - r.top) / r.height) * 100) });
};
const pos = `${pt.x}% ${pt.y}%`;
return (
<div className="dm">
<div className="fp">
<div
className="fp-main"
onPointerDown={(e) => {
dragging.current = e.pointerId;
e.currentTarget.setPointerCapture(e.pointerId);
at(e);
}}
onPointerMove={(e) => dragging.current === e.pointerId && at(e)}
onPointerUp={() => (dragging.current = null)}
>
<img src={`${S}/studio-wide.svg`} alt="A terracotta jug at the right of a long table, a small cup at the far left" draggable={false} />
<button
type="button"
className="fp-marker"
style={{ left: `${pt.x}%`, top: `${pt.y}%` }}
aria-label={`Focal point, ${pt.x}% across and ${pt.y}% down. Arrow keys move it`}
onKeyDown={(e) => {
const s = e.shiftKey ? 10 : 2;
const d: Record<string, [number, number]> = { ArrowLeft: [-s, 0], ArrowRight: [s, 0], ArrowUp: [0, -s], ArrowDown: [0, s] };
const m = d[e.key];
if (m) {
e.preventDefault();
setPt((p) => ({ x: clamp(p.x + m[0]), y: clamp(p.y + m[1]) }));
}
}}
/>
</div>
<ul className="fp-crops" aria-label="How each format crops">
{CROPS.map((c) => (
<li key={c.id}>
<span className="fp-crop" style={{ aspectRatio: c.ratio }}>
<img src={`${S}/studio-wide.svg`} alt="" style={{ objectPosition: pos }} />
</span>
<span>{c.label}</span>
</li>
))}
</ul>
<p className="dm-hint">Drag the ring onto the subject; each crop keeps it</p>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ key: "ArrowLeft", on: ".fp-marker", times: 10, wait: 700 },
{ key: "ArrowUp", on: ".fp-marker", times: 8, wait: 700 },
{ key: "ArrowRight", on: ".fp-marker", times: 18, wait: 700 },
{ key: "ArrowDown", on: ".fp-marker", times: 8, wait: 700 },
{ key: "ArrowLeft", on: ".fp-marker", times: 8, wait: 500 },
];
export default function FocalPointEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Focal point for every crop" sample tour={TOUR}>
<FocalPointDemo />
</PieceStage>
);
}