Show the full Vanuatu board when zooming out

This commit is contained in:
coyotte508 2026-11-01 14:47:58 +01:00
commit 824f542a7e
5 changed files with 145 additions and 37 deletions

View file

@ -679,9 +679,13 @@ for (const width of [1440, 390]) {
path: `.local/qa/zoom-min-${width}.png`,
fullPage: true,
});
const minimumZoom = Number(
await page.locator(".map-scroll").getAttribute("data-zoom"),
);
for (let i = 0; i < 3; i++) await page.locator("[data-zoom-in]").click();
assert.ok(
Number(await page.locator(".map-scroll").getAttribute("data-zoom")) > 1,
Number(await page.locator(".map-scroll").getAttribute("data-zoom")) >
minimumZoom,
);
assert.equal(
await page.locator(".archipelago").getAttribute("viewBox"),

View file

@ -36,7 +36,49 @@ const browser = await chromium.launch({
});
const errors = [];
await mkdir(".local/qa", { recursive: true });
async function zoomOutFully(page) {
const out = page.locator("[data-zoom-out]");
let steps = 0;
while ((await out.getAttribute("aria-disabled")) !== "true") {
assert.ok(steps++ < 20, "Zoom out reaches its full-board limit");
await out.click();
}
const clipped = await page.locator(".map-scroll").evaluate((map) => {
const frame = map.getBoundingClientRect();
const css = getComputedStyle(map);
const svg = map.querySelector(".archipelago");
const bounds = svg.viewBox.baseVal;
return [...svg.querySelectorAll(".map-cell .cell-surface")].flatMap(
(path) => {
const tile = path.getBoundingClientRect();
const world = path.getBBox();
const inside =
tile.left >= frame.left + parseFloat(css.paddingLeft) - 1 &&
tile.top >= frame.top + parseFloat(css.paddingTop) - 1 &&
tile.right <=
frame.left + map.clientWidth - parseFloat(css.paddingRight) + 1 &&
tile.bottom <=
frame.top + map.clientHeight - parseFloat(css.paddingBottom) + 1 &&
world.x >= bounds.x &&
world.y >= bounds.y &&
world.x + world.width <= bounds.x + bounds.width &&
world.y + world.height <= bounds.y + bounds.height;
return inside ? [] : [path.closest("[data-cell]").dataset.cell];
},
);
});
assert.deepEqual(
clipped,
[],
"Full overview includes every complete tile space, even empty ones",
);
}
try {
const partial = E.init(3, [], {}, "map-fit");
partial.board["2,0"] = E.tileState("c1");
partial.board["2,1"] = E.tileState("a1");
partial.round = 2;
partial.phase = "plan";
const expanded = E.init(3, [], {}, "map-fit");
const remainingTiles = Object.keys(E.TILES).filter(
(id) => !Object.values(expanded.board).some((tile) => tile.id === id),
@ -55,7 +97,7 @@ try {
]) {
const page = await browser.newPage({ viewport: { width, height } });
page.on("pageerror", (error) => errors.push(error.message));
for (const state of [E.init(3, [], {}, "map-fit"), expanded]) {
for (const state of [E.init(3, [], {}, "map-fit"), partial, expanded]) {
await page.goto(base + "/?hotseat");
await page.evaluate(
(s) => localStorage.setItem("vanuatu-practice-v1", JSON.stringify(s)),
@ -78,7 +120,11 @@ try {
const fit = await page.locator(".map-scroll").evaluate((map) => {
const frame = map.getBoundingClientRect(),
css = getComputedStyle(map);
const svg = map.querySelector(".archipelago").getBoundingClientRect();
const svg = map.querySelector(".archipelago");
const [x, y, w, h] = svg.dataset.focusBox.split(" ").map(Number);
const matrix = svg.getScreenCTM();
const first = new DOMPoint(x, y).matrixTransform(matrix);
const last = new DOMPoint(x + w, y + h).matrixTransform(matrix);
const wide =
map.clientWidth -
parseFloat(css.paddingLeft) -
@ -92,12 +138,22 @@ try {
.querySelector(".turn-tray")
.getBoundingClientRect().height + 12
: parseFloat(css.paddingBottom);
const middleX = frame.left + map.clientWidth / 2;
const middleY =
frame.top +
parseFloat(css.paddingTop) +
(map.clientHeight -
parseFloat(css.paddingTop) -
parseFloat(css.paddingBottom)) /
2;
return (
svg.left >= frame.left + parseFloat(css.paddingLeft) - 1 &&
svg.right <=
Math.abs((first.x + last.x) / 2 - middleX) < 1 &&
Math.abs((first.y + last.y) / 2 - middleY) < 1 &&
first.x >= frame.left + parseFloat(css.paddingLeft) - 1 &&
last.x <=
frame.left + map.clientWidth - parseFloat(css.paddingRight) + 1 &&
svg.top >= frame.top + top - 1 &&
svg.bottom <= frame.top + map.clientHeight - bottom + 1
first.y >= frame.top + top - 1 &&
last.y <= frame.top + map.clientHeight - bottom + 1
);
});
assert.ok(fit, `${width}px: the whole active map fits after reload`);
@ -151,6 +207,21 @@ try {
[],
`${width}px: controls do not obscure tiles at the default zoom`,
);
if (state === partial)
await page.screenshot({
path: `.local/qa/map-partial-default-${width}.png`,
});
await zoomOutFully(page);
if (state !== expanded)
assert.ok(
Number(await page.locator(".map-scroll").getAttribute("data-zoom")) <
1,
"An incomplete map can zoom out beyond the active tiles",
);
if (state === partial)
await page.screenshot({
path: `.local/qa/map-partial-overview-${width}.png`,
});
}
await page.screenshot({ path: `.local/qa/map-fit-expanded-${width}.png` });
await page.close();
@ -337,12 +408,8 @@ try {
);
// Zoom out reaches a complete overview; the limit button also permits a
// vertical swipe to scroll the page, without changing the game state.
while (
(await page.locator("[data-zoom-out]").getAttribute("aria-disabled")) !==
"true"
)
await page.locator("[data-zoom-out]").click();
assert.equal(await zoom(), 1);
await zoomOutFully(page);
assert.ok((await zoom()) < 1);
const button = await page.locator("[data-zoom-out]").boundingBox();
const beforeY = await page.evaluate(() => scrollY);
await cdp.send("Input.dispatchTouchEvent", {

View file

@ -40,6 +40,16 @@ function tilePath(x, y) {
return path + "Z";
}
const defaultSymbols = ["●", "◆", "▲", "■", "✦"];
function boardBounds(cells) {
const x = Math.min(...cells.map((c) => c.x)) - 120;
const y = Math.min(...cells.map((c) => c.y)) - 120;
return [
x,
y,
Math.max(...cells.map((c) => c.x)) + 120 - x,
Math.max(...cells.map((c) => c.y)) + 120 - y,
].join(" ");
}
function tileImage(spec, c, ghost = false, prefix = "board") {
const clip = `url(#${prefix}-cell-${c.q + 2}-${c.r})`;
const ocean = oceanSurface(c, prefix);
@ -90,20 +100,19 @@ export function boardSvg(
const all = boardCells(s)
.filter((c) => !preview || s.board[c.id])
.map((c) => ({ ...c, ...center(c.id) }));
const visible = overview
const active = overview
? all
: all.filter((c) => s.board[c.id] || targetSet.has(c.id));
const x0 = Math.min(...visible.map((c) => c.x)) - 120,
x1 = Math.max(...visible.map((c) => c.x)) + 120,
y0 = Math.min(...visible.map((c) => c.y)) - 120,
y1 = Math.max(...visible.map((c) => c.y)) + 120;
// Frame the active tiles on arrival, but retain the complete map so empty
// spaces are not cut off and the zoom controls can reveal the whole board.
const focusBox = boardBounds(active.length ? active : all);
const defs = all
.map(
(c) =>
`<clipPath id="${prefix}-cell-${c.q + 2}-${c.r}"><path d="${tilePath(c.x, c.y)}"/></clipPath>`,
)
.join("");
let svg = `<svg class="${preview ? "tile-art" : "archipelago"}${overview ? " overview" : ""}" viewBox="${x0} ${y0} ${x1 - x0} ${y1 - y0}" role="group" aria-label="${preview ? esc(t(s.board["0,0"].type === "island" ? "island" : "sea")) : "Vanuatu"}"><defs>${defs}${oceanDefs(prefix)}<filter id="${prefix}-boat-shadow" x="-25%" y="-25%" width="150%" height="150%"><feDropShadow dx="0" dy="0" stdDeviation=".9" flood-color="#fff9df" flood-opacity="1"/><feDropShadow dx="0" dy="2" stdDeviation="1.5" flood-opacity=".45"/></filter></defs>`;
let svg = `<svg class="${preview ? "tile-art" : "archipelago"}${overview ? " overview" : ""}" viewBox="${boardBounds(all)}" data-focus-box="${focusBox}" role="group" aria-label="${preview ? esc(t(s.board["0,0"].type === "island" ? "island" : "sea")) : "Vanuatu"}"><defs>${defs}${oceanDefs(prefix)}<filter id="${prefix}-boat-shadow" x="-25%" y="-25%" width="150%" height="150%"><feDropShadow dx="0" dy="0" stdDeviation=".9" flood-color="#fff9df" flood-opacity="1"/><feDropShadow dx="0" dy="2" stdDeviation="1.5" flood-opacity=".45"/></filter></defs>`;
let dikes = "";
for (const c of all) {
const legal = targetSet.has(c.id),

View file

@ -1,4 +1,4 @@
const MIN_ZOOM = 1;
const DEFAULT_ZOOM = 1;
const MAX_ZOOM = 4;
const STEP = 1.25;
const READABLE_TILE_SCALE = 0.5;
@ -6,6 +6,7 @@ const clamp = (n, min, max) => Math.max(min, Math.min(max, n));
export function mountMapZoom(root) {
let zoom,
minZoom = 1,
observed,
pinch,
mapEvents,
@ -65,18 +66,22 @@ export function mountMapZoom(root) {
const { map, canvas, svg } = el,
f = frame(map),
box = svg.viewBox.baseVal;
const [x, y, w, h] = svg.dataset.focusBox.split(" ").map(Number);
const active = { x, y, width: w, height: h };
if (f.width <= 0 || f.height <= 0) return;
const initial = zoom === undefined;
const fitScale = Math.min(f.width / box.width, f.height / box.height);
const fitScale = Math.min(f.width / active.width, f.height / active.height);
const overviewScale = Math.min(f.width / box.width, f.height / box.height);
minZoom = Math.min(DEFAULT_ZOOM, overviewScale / fitScale);
if (autoFit) {
// Fit desktop panels even when narrow; on phones keep 176-unit tiles
// at least 88px wide so their resource badges remain readable.
zoom = matchMedia("(max-width: 700px)").matches
? clamp(READABLE_TILE_SCALE / fitScale, MIN_ZOOM, MAX_ZOOM)
: MIN_ZOOM;
? clamp(READABLE_TILE_SCALE / fitScale, DEFAULT_ZOOM, MAX_ZOOM)
: DEFAULT_ZOOM;
if (f.width >= 900) {
// Wide panels leave room between the corner controls. Use the spare
// vertical padding for a closer view, while keeping the entire SVG
// vertical padding for a closer view, while keeping the active tiles
// above the action tray and at least 12px from the viewport edges.
const trayHeight =
root.querySelector(".turn-tray")?.getBoundingClientRect().height ?? 0;
@ -85,19 +90,40 @@ export function mountMapZoom(root) {
Math.min(f.top - 12, f.bottom - trayHeight - 12),
);
const closeScale = Math.min(
f.width / box.width,
(f.height + 2 * extra) / box.height,
f.width / active.width,
(f.height + 2 * extra) / active.height,
);
zoom = clamp(closeScale / fitScale, MIN_ZOOM, STEP);
zoom = clamp(closeScale / fitScale, DEFAULT_ZOOM, STEP);
}
}
} else zoom = clamp(zoom, minZoom, MAX_ZOOM);
const scale = fitScale * zoom;
const width = box.width * scale,
height = box.height * scale;
// Leave enough room to center an off-center cluster of active tiles, even
// when the full map fits along one axis. At overview, remove that room so
// the whole board fits without leaving an unnecessary scrollable area.
const focus = zoom <= minZoom + 0.000001 ? box : active;
const canvasSize = (size, viewport, middle) =>
Math.max(
viewport,
size + 2 * Math.max(0, viewport / 2 - Math.min(middle, size - middle)),
);
let resized = false;
for (const [node, property, value] of [
[canvas, "width", Math.max(f.width, width)],
[canvas, "height", Math.max(f.height, height)],
[
canvas,
"width",
canvasSize(width, f.width, (focus.x + focus.width / 2 - box.x) * scale),
],
[
canvas,
"height",
canvasSize(
height,
f.height,
(focus.y + focus.height / 2 - box.y) * scale,
),
],
[svg, "width", width],
[svg, "height", height],
]) {
@ -116,6 +142,7 @@ export function mountMapZoom(root) {
box.y,
box.width,
box.height,
svg.dataset.focusBox,
f.width,
f.height,
zoom,
@ -127,12 +154,16 @@ export function mountMapZoom(root) {
map.scrollLeft += actual.x - client.x;
map.scrollTop += actual.y - client.y;
} else if (initial || (autoFit && layoutKey !== nextLayout)) {
map.scrollLeft = (canvas.clientWidth - f.width) / 2;
map.scrollTop = (canvas.clientHeight - f.height) / 2;
const middle = new DOMPoint(
active.x + active.width / 2,
active.y + active.height / 2,
).matrixTransform(svg.getScreenCTM());
map.scrollLeft += middle.x - (f.x + f.width / 2);
map.scrollTop += middle.y - (f.y + f.height / 2);
}
layoutKey = nextLayout;
for (const [selector, disabled] of [
["[data-zoom-out]", zoom <= MIN_ZOOM],
["[data-zoom-out]", zoom <= minZoom + 0.000001],
["[data-zoom-in]", zoom >= MAX_ZOOM],
]) {
const button = root.querySelector(selector);
@ -173,7 +204,7 @@ export function mountMapZoom(root) {
}
function setZoom(value, anchor = capture()) {
autoFit = false;
zoom = clamp(value, MIN_ZOOM, MAX_ZOOM);
zoom = clamp(value, minZoom, MAX_ZOOM);
update(anchor);
}
const geometry = (touches) => {

View file

@ -1606,9 +1606,6 @@ body:has(.vanuatu) {
scroll-behavior: auto !important;
}
}
.vanuatu .archipelago {
overflow: hidden;
}
.vanuatu .map-scroll {
padding-bottom: 145px;
}