/** Вписать прямоугольник целевого аспекта в бокс (как object-fit: contain). */ export function fitAspectRect( boxW: number, boxH: number, aspectW: number, aspectH: number, ): { x: number; y: number; w: number; h: number } { const bw = Math.max(1, boxW); const bh = Math.max(1, boxH); const aw = Math.max(1, aspectW); const ah = Math.max(1, aspectH); const boxAspect = bw / bh; const targetAspect = aw / ah; if (boxAspect > targetAspect) { const h = bh; const w = h * targetAspect; return { x: (bw - w) / 2, y: 0, w, h }; } const w = bw; const h = w / targetAspect; return { x: 0, y: (bh - h) / 2, w, h }; }