'use client'; import { useCallback, useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; type CropModalProps = { /** The freshly picked image to crop. */ file: File; /** Dismiss without producing a crop — the upload is abandoned. */ onCancel: () => void; /** Hands back the cropped image as a new File. */ onCrop: (file: File) => void; }; // Square crop box, expressed in on-screen (displayed-image) pixels. type Box = { x: number; y: number; size: number }; type Handle = 'tl' | 'tr' | 'bl' | 'br'; const HANDLES: Handle[] = ['tl', 'tr', 'bl', 'br']; const MIN_FRACTION = 0.2; // crop can't shrink below 20% of the short edge const MAX_OUTPUT = 1800; // px — cap on the exported crop's edge const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v)); // A square-locked crop tool shown before an uploaded photo is accepted. // The crop is always 1:1 — the shape every Lumen & Gesso portrait is // painted on — so the customer frames their pet exactly as it will sit // on the canvas. Cropping happens entirely client-side via . export function CropModal({ file, onCancel, onCrop }: CropModalProps) { const stageRef = useRef(null); const imgRef = useRef(null); const natRef = useRef({ w: 0, h: 0 }); const dispRef = useRef({ w: 0, h: 0 }); const boxRef = useRef({ x: 0, y: 0, size: 0 }); const [url, setUrl] = useState(''); const [box, setBox] = useState(null); const [ready, setReady] = useState(false); const [failed, setFailed] = useState(false); // Object-URL lifecycle for the preview image. useEffect(() => { const u = URL.createObjectURL(file); setUrl(u); return () => URL.revokeObjectURL(u); }, [file]); // Lock background scroll while the modal is open. useEffect(() => { const prev = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = prev; }; }, []); // Esc cancels. useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') onCancel(); }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [onCancel]); const commitBox = (b: Box) => { boxRef.current = b; setBox(b); }; // Measure the displayed image. On first measure, drop a centered crop // box; on later measures (window resize) scale the box to keep it valid. const measure = useCallback(() => { const img = imgRef.current; if (!img) return; const w = img.clientWidth; const h = img.clientHeight; if (w === 0 || h === 0) return; const prev = dispRef.current; dispRef.current = { w, h }; if (prev.w === 0) { const size = Math.min(w, h) * 0.86; commitBox({ x: (w - size) / 2, y: (h - size) / 2, size }); setReady(true); } else if (prev.w !== w) { const r = w / prev.w; const b = boxRef.current; commitBox({ x: b.x * r, y: b.y * r, size: b.size * r }); } }, []); useEffect(() => { window.addEventListener('resize', measure); return () => window.removeEventListener('resize', measure); }, [measure]); const onImgLoad = () => { const img = imgRef.current; if (!img) return; natRef.current = { w: img.naturalWidth, h: img.naturalHeight }; measure(); }; // One handler drives both moving the box and resizing from any corner. const startDrag = (mode: 'move' | Handle) => (e: React.PointerEvent) => { e.preventDefault(); e.stopPropagation(); const stage = stageRef.current; if (!stage) return; const rect = stage.getBoundingClientRect(); const disp = dispRef.current; const start = boxRef.current; const minSize = Math.min(disp.w, disp.h) * MIN_FRACTION; // The corner opposite the dragged handle stays pinned during resize. const anchor = mode === 'tl' ? { x: start.x + start.size, y: start.y + start.size } : mode === 'tr' ? { x: start.x, y: start.y + start.size } : mode === 'bl' ? { x: start.x + start.size, y: start.y } : { x: start.x, y: start.y }; // 'br' / 'move' const grabX = e.clientX - rect.left - start.x; const grabY = e.clientY - rect.top - start.y; const onMove = (ev: PointerEvent) => { const px = clamp(ev.clientX - rect.left, 0, disp.w); const py = clamp(ev.clientY - rect.top, 0, disp.h); if (mode === 'move') { commitBox({ x: clamp(ev.clientX - rect.left - grabX, 0, disp.w - start.size), y: clamp(ev.clientY - rect.top - grabY, 0, disp.h - start.size), size: start.size, }); return; } let raw: number; let cap: number; if (mode === 'br') { raw = Math.max(px - anchor.x, py - anchor.y); cap = Math.min(disp.w - anchor.x, disp.h - anchor.y); } else if (mode === 'tl') { raw = Math.max(anchor.x - px, anchor.y - py); cap = Math.min(anchor.x, anchor.y); } else if (mode === 'tr') { raw = Math.max(px - anchor.x, anchor.y - py); cap = Math.min(disp.w - anchor.x, anchor.y); } else { raw = Math.max(anchor.x - px, py - anchor.y); // bl cap = Math.min(anchor.x, disp.h - anchor.y); } const size = clamp(raw, minSize, cap); commitBox({ x: mode === 'tl' || mode === 'bl' ? anchor.x - size : anchor.x, y: mode === 'tl' || mode === 'tr' ? anchor.y - size : anchor.y, size, }); }; const onUp = () => { window.removeEventListener('pointermove', onMove); window.removeEventListener('pointerup', onUp); }; window.addEventListener('pointermove', onMove); window.addEventListener('pointerup', onUp); }; // Paint the crop onto a canvas at source resolution and hand back a File. const applyCrop = () => { const img = imgRef.current; const disp = dispRef.current; const b = boxRef.current; if (!img || disp.w === 0) { onCrop(file); return; } const scale = natRef.current.w / disp.w; const sSize = b.size * scale; const out = Math.min(Math.round(sSize), MAX_OUTPUT); const canvas = document.createElement('canvas'); canvas.width = out; canvas.height = out; const ctx = canvas.getContext('2d'); if (!ctx) { onCrop(file); return; } ctx.drawImage(img, b.x * scale, b.y * scale, sSize, sSize, 0, 0, out, out); canvas.toBlob( (blob) => { if (!blob) { onCrop(file); return; } const base = file.name.replace(/\.[^.]+$/, '') || 'photo'; onCrop(new File([blob], `${base}-cropped.jpg`, { type: 'image/jpeg' })); }, 'image/jpeg', 0.92, ); }; if (!url) return null; return createPortal(

Crop your photo

{failed ? (
We couldn’t open {file.name} for cropping in the browser — some formats (like HEIC) can’t preview here. You can still use the photo as-is.
) : (
{/* eslint-disable-next-line @next/next/no-img-element */} setFailed(true)} /> {ready && box && (
{HANDLES.map((h) => ( ))}
)}
)}

{failed ? 'No problem — the studio will frame it for you when your proof is prepared.' : 'Drag the corner handles to resize, or drag inside the box to move it. The crop is locked to a square — the shape every portrait is painted on.'}

{failed ? ( ) : ( )}
, document.body, ); }