Pick a PNG/JPEG/WebP file, crop it to a square on a canvas (drag, wheel, slider or keyboard), export a 256x256 PNG and send it to Hub. Adds remove photo, error states, RU/EN copy and privacy policy text about the stored photo and 30-day backups. Claude-Session: https://claude.ai/code/session_01GDjxLvBcnJqniFQGUGb1V4
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { test } from "bun:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
MAX_ZOOM,
|
|
clampCrop,
|
|
initialCrop,
|
|
panBy,
|
|
sourceRect,
|
|
zoomTo,
|
|
} from "../src/crop";
|
|
|
|
test("the first frame is centred and fills the square", () => {
|
|
const rect = sourceRect(initialCrop(400, 200));
|
|
assert.deepEqual(rect, { sx: 100, sy: 0, side: 200 });
|
|
});
|
|
test("panning never leaves empty edges and never stretches", () => {
|
|
let state = initialCrop(400, 200);
|
|
state = panBy(state, -10_000, 10_000, 280);
|
|
let rect = sourceRect(state);
|
|
assert.equal(rect.sx + rect.side, 400);
|
|
assert.equal(rect.sy, 0);
|
|
state = zoomTo(state, 2);
|
|
state = panBy(state, 10_000, -10_000, 280);
|
|
rect = sourceRect(state);
|
|
assert.equal(rect.side, 100);
|
|
assert.equal(rect.sx, 0);
|
|
assert.equal(rect.sy, 100);
|
|
});
|
|
test("zoom is clamped and keeps the area inside the image", () => {
|
|
const state = clampCrop({
|
|
...initialCrop(300, 300),
|
|
zoom: 99,
|
|
x: -5,
|
|
y: 900,
|
|
});
|
|
assert.equal(state.zoom, MAX_ZOOM);
|
|
const { sx, sy, side } = sourceRect(state);
|
|
assert.ok(sx >= 0 && sy >= 0 && sx + side <= 300 && sy + side <= 300);
|
|
assert.equal(zoomTo(initialCrop(300, 300), 0.2).zoom, 1);
|
|
});
|