/** Crop region in natural image pixels. */ export interface CropRect { x: number y: number width: number height: number } export interface DisplayLayout { offsetX: number offsetY: number width: number height: number } const MIN_CROP_PX = 48 export function clampCropRect( crop: CropRect, naturalW: number, naturalH: number, ): CropRect { const minW = Math.min(MIN_CROP_PX, naturalW) const minH = Math.min(MIN_CROP_PX, naturalH) let { x, y, width, height } = crop width = Math.max(minW, Math.min(width, naturalW)) height = Math.max(minH, Math.min(height, naturalH)) x = Math.max(0, Math.min(x, naturalW - width)) y = Math.max(0, Math.min(y, naturalH - height)) return { x, y, width, height } } export function defaultCropRect(naturalW: number, naturalH: number): CropRect { const marginX = naturalW * 0.08 const marginY = naturalH * 0.08 return clampCropRect( { x: marginX, y: marginY, width: naturalW - marginX * 2, height: naturalH - marginY * 2, }, naturalW, naturalH, ) } export function fullCropRect(naturalW: number, naturalH: number): CropRect { return { x: 0, y: 0, width: naturalW, height: naturalH } } /** object-contain layout of an image inside a container. */ export function computeDisplayLayout( containerW: number, containerH: number, naturalW: number, naturalH: number, ): DisplayLayout { if (containerW <= 0 || containerH <= 0 || naturalW <= 0 || naturalH <= 0) { return { offsetX: 0, offsetY: 0, width: 0, height: 0 } } const scale = Math.min(containerW / naturalW, containerH / naturalH) const width = naturalW * scale const height = naturalH * scale return { offsetX: (containerW - width) / 2, offsetY: (containerH - height) / 2, width, height, } } export function cropToDisplay( crop: CropRect, layout: DisplayLayout, naturalW: number, naturalH: number, ) { const sx = layout.width / naturalW const sy = layout.height / naturalH return { x: layout.offsetX + crop.x * sx, y: layout.offsetY + crop.y * sy, width: crop.width * sx, height: crop.height * sy, } } export function displayToCrop( display: { x: number; y: number; width: number; height: number }, layout: DisplayLayout, naturalW: number, naturalH: number, ): CropRect { const sx = naturalW / layout.width const sy = naturalH / layout.height return clampCropRect( { x: (display.x - layout.offsetX) * sx, y: (display.y - layout.offsetY) * sy, width: display.width * sx, height: display.height * sy, }, naturalW, naturalH, ) } export async function applyCrop( source: HTMLImageElement, crop: CropRect, ): Promise<{ image: HTMLImageElement; file: File; url: string }> { const rect = clampCropRect(crop, source.naturalWidth, source.naturalHeight) const canvas = document.createElement('canvas') canvas.width = Math.round(rect.width) canvas.height = Math.round(rect.height) const ctx = canvas.getContext('2d') if (!ctx) throw new Error('Canvas not supported') ctx.drawImage( source, rect.x, rect.y, rect.width, rect.height, 0, 0, canvas.width, canvas.height, ) const blob = await new Promise((resolve, reject) => { canvas.toBlob( (b) => (b ? resolve(b) : reject(new Error('Failed to encode crop'))), 'image/jpeg', 0.92, ) }) const file = new File([blob], 'crop.jpg', { type: 'image/jpeg' }) const url = URL.createObjectURL(blob) const image = await new Promise((resolve, reject) => { const img = new Image() img.onload = () => resolve(img) img.onerror = () => reject(new Error('Failed to load cropped image')) img.src = url }) return { image, file, url } }