fix(capture): preserve PhotoStage preview aspect on first paint

Contain-fit waits for a real parent width, uses ResizeObserver, and shrinks
width when maxH would otherwise stretch the CSS box. Pure fitDisplay is
covered by node tests.
This commit is contained in:
2026-09-14 16:37:00 +00:00
parent 0e94b1fd2a
commit b781ffeb66
6 changed files with 568 additions and 21 deletions
+26
View File
@@ -0,0 +1,26 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { fitDisplay } from './fitDisplay.ts';
test('parentW=0 → null (no size until laid out)', () => {
assert.equal(fitDisplay(0, 1000, 750, 420), null);
assert.equal(fitDisplay(-5, 1000, 750, 420), null);
});
test('280 wide, 4:3 landscape → contain, under the height cap', () => {
const fit = fitDisplay(280, 1600, 1200, 420);
assert.ok(fit);
assert.equal(fit.fitW, 280);
assert.ok(Math.abs(fit.fitH - 210) < 1e-9);
assert.ok(fit.fitH <= 420);
});
test('portrait under maxH never stretches: height capped, width shrinks', () => {
// 1000 wide source, 3000 tall. Full-width fit would be 900 tall > 420.
const fit = fitDisplay(400, 1000, 3000, 420);
assert.ok(fit);
assert.equal(fit.fitH, 420);
assert.ok(Math.abs(fit.fitW - 140) < 1e-9);
// aspect preserved: fitW/fitH === imgW/imgH
assert.ok(Math.abs(fit.fitW / fit.fitH - 1000 / 3000) < 1e-9);
});