- Schakelaar (role=switch) Actief / Niet actief bij de knoppen, gedimde rij; 'Verberg voor schermlezers'; veld 'Beschrijving' binnen een groep 'Slide N'
- Alle foto's zonder complete set tonen 'Wordt verwerkt' (voortgang bij de foto die bezig is, 'in de wachtrij' bij de rest); totaal bovenaan telt ook nog niet opgeslagen foto's
- Per foto 'Verwerking afgerond' (zelfde weergave voor een hergebruikte foto), foto direct bruikbaar; niet-opgeslagen foto's worden zelf ververst (GET /hero/images/{id}), ook in een achtergrondtab
- Focusvenster toont de hele foto (full-preview in elke set, achtergrond-aanvulling voor oude sets) in plaats van de uitgesneden wide-variant (vervorming)
- Prullenbak en 'Wijzigingen weggooien' in de destructieve stijl
- upload-respons: reused (intern)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
108 lines
5.4 KiB
TypeScript
108 lines
5.4 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { CENTRE, TALL, WIDE, cropFor, normalizeFocus, roundHalfUp } from './focusMath';
|
|
|
|
// The same vectors as src/SlpModularCms.Core.Tests/Media/MediaPureFunctionTests.cs (FocusMathTests.KnownVectors).
|
|
const vectors: [number, number, 'wide' | 'tall', number, number, number, [number, number, number, number]][] = [
|
|
[1600, 1067, 'wide', 0.5, 0.5, 1.0, [0, 0, 1600, 1067]],
|
|
[1600, 1067, 'tall', 0.5, 0.5, 1.0, [374, 1, 853, 1066]],
|
|
[1600, 1067, 'tall', 0.0, 0.5, 1.0, [0, 1, 853, 1066]],
|
|
[1600, 1067, 'tall', 1.0, 0.5, 1.0, [747, 1, 853, 1066]],
|
|
[1600, 1067, 'wide', 0.45, 0.35, 2.0, [320, 107, 800, 533]],
|
|
[2560, 1702, 'tall', 0.55, 0.6, 1.0, [728, 1, 1361, 1701]],
|
|
[1000, 2000, 'wide', 0.5, 0.5, 1.0, [0, 667, 1000, 667]],
|
|
];
|
|
|
|
describe('cropFor', () => {
|
|
it.each(vectors)('%i x %i %s at (%d, %d) zoom %d', (w, h, ratio, x, y, zoom, [cx, cy, cw, ch]) => {
|
|
expect(cropFor(w, h, ratio === 'tall' ? TALL : WIDE, { x, y, zoom })).toEqual({ x: cx, y: cy, width: cw, height: ch });
|
|
});
|
|
|
|
it('always stays inside the image and keeps the focus point inside the crop', () => {
|
|
for (let i = 0; i < 2000; i++) {
|
|
const w = 1 + Math.floor(Math.random() * 6000);
|
|
const h = 1 + Math.floor(Math.random() * 6000);
|
|
const focus = { x: Math.random(), y: Math.random(), zoom: 1 + Math.random() * 3 };
|
|
for (const ratio of [WIDE, TALL]) {
|
|
const c = cropFor(w, h, ratio, focus);
|
|
expect(c.x).toBeGreaterThanOrEqual(0);
|
|
expect(c.y).toBeGreaterThanOrEqual(0);
|
|
expect(c.x + c.width).toBeLessThanOrEqual(w);
|
|
expect(c.y + c.height).toBeLessThanOrEqual(h);
|
|
expect(focus.x * w).toBeGreaterThanOrEqual(c.x - 1);
|
|
expect(focus.x * w).toBeLessThanOrEqual(c.x + c.width + 1);
|
|
expect(focus.y * h).toBeGreaterThanOrEqual(c.y - 1);
|
|
expect(focus.y * h).toBeLessThanOrEqual(c.y + c.height + 1);
|
|
}
|
|
}
|
|
});
|
|
|
|
it('survives values that are out of range or not numbers', () => {
|
|
const c = cropFor(1600, 1067, TALL, { x: Number.NaN, y: Infinity, zoom: -Infinity });
|
|
expect(c.x).toBeGreaterThanOrEqual(0);
|
|
expect(c.x + c.width).toBeLessThanOrEqual(1600);
|
|
});
|
|
|
|
it('rejects an image without a size', () => {
|
|
expect(() => cropFor(0, 100, WIDE, CENTRE)).toThrow(RangeError);
|
|
});
|
|
});
|
|
|
|
describe('normalizeFocus and roundHalfUp', () => {
|
|
it('clamps and defaults', () => {
|
|
expect(normalizeFocus({ x: -1, y: 2, zoom: 9 })).toEqual({ x: 0, y: 1, zoom: 4 });
|
|
expect(normalizeFocus({ x: Number.NaN, y: Number.NaN, zoom: Number.NaN })).toEqual({ x: 0.5, y: 0.5, zoom: 1 });
|
|
});
|
|
|
|
it('rounds .5 up like the host', () => {
|
|
expect(roundHalfUp(373.5)).toBe(374);
|
|
expect(roundHalfUp(0.5)).toBe(1);
|
|
expect(roundHalfUp(2.4)).toBe(2);
|
|
});
|
|
});
|
|
|
|
|
|
describe('the crop of both shapes for every kind of picture', () => {
|
|
// landscape 3:2, the two common cameras, portrait, square, a panorama and a very tall strip
|
|
const sources: [string, number, number][] = [
|
|
['landscape 3:2', 3000, 2000],
|
|
['landscape 4:3', 2048, 1536],
|
|
['portrait 2:3', 2000, 3000],
|
|
['square', 1500, 1500],
|
|
['panorama 3:1', 6000, 2000],
|
|
['tall strip 1:3', 800, 2400],
|
|
];
|
|
const points = [CENTRE, { x: 0, y: 0, zoom: 1 }, { x: 1, y: 1, zoom: 1 }, { x: 0.2, y: 0.8, zoom: 2 }, { x: 0.5, y: 0.5, zoom: 4 }];
|
|
|
|
it.each(sources)('%s: both crops have exactly their shape (to the pixel), lie inside the picture and keep the point', (_name, width, height) => {
|
|
for (const ratio of [WIDE, TALL]) {
|
|
for (const focus of points) {
|
|
const crop = cropFor(width, height, ratio, focus);
|
|
// One pixel of rounding at most: the crop is never stretched to another shape.
|
|
expect(Math.abs(crop.width * ratio.denominator - crop.height * ratio.numerator)).toBeLessThanOrEqual(Math.max(ratio.numerator, ratio.denominator));
|
|
expect(crop.x).toBeGreaterThanOrEqual(0);
|
|
expect(crop.y).toBeGreaterThanOrEqual(0);
|
|
expect(crop.x + crop.width).toBeLessThanOrEqual(width);
|
|
expect(crop.y + crop.height).toBeLessThanOrEqual(height);
|
|
}
|
|
}
|
|
});
|
|
|
|
it.each(sources)('%s: the preview box shows the crop at its own shape (no stretching)', (_name, width, height) => {
|
|
// The preview places the WHOLE picture inside a box of the crop shape: scale = box width / crop width, so the
|
|
// picture keeps its own shape when its height follows from its width.
|
|
for (const ratio of [WIDE, TALL]) {
|
|
const crop = cropFor(width, height, ratio, CENTRE);
|
|
const boxWidth = 100;
|
|
const scale = boxWidth / crop.width;
|
|
const pictureWidth = width * scale;
|
|
const pictureHeight = pictureWidth * (height / width);
|
|
const boxHeight = (boxWidth * ratio.denominator) / ratio.numerator;
|
|
// What is visible of the picture in the box is exactly the crop rectangle (same shape as the box).
|
|
const visibleWidth = boxWidth / (pictureWidth / width);
|
|
const visibleHeight = boxHeight / (pictureHeight / height);
|
|
expect(visibleWidth).toBeCloseTo(crop.width, 6);
|
|
expect(visibleHeight / visibleWidth).toBeCloseTo(ratio.denominator / ratio.numerator, 2);
|
|
}
|
|
});
|
|
});
|