From 824f542a7e20f729d8cd320f38ab184ee6627001 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Sun, 1 Nov 2026 14:47:58 +0100 Subject: [PATCH] Show the full Vanuatu board when zooming out --- test/browser.mjs | 6 ++- test/map-zoom-browser.mjs | 91 +++++++++++++++++++++++++++++++++------ viewer/board.js | 21 ++++++--- viewer/map-zoom.js | 61 +++++++++++++++++++------- viewer/style.css | 3 -- 5 files changed, 145 insertions(+), 37 deletions(-) diff --git a/test/browser.mjs b/test/browser.mjs index 9ba04dd..c5e18d8 100644 --- a/test/browser.mjs +++ b/test/browser.mjs @@ -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"), diff --git a/test/map-zoom-browser.mjs b/test/map-zoom-browser.mjs index 9789471..23f8260 100644 --- a/test/map-zoom-browser.mjs +++ b/test/map-zoom-browser.mjs @@ -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", { diff --git a/viewer/board.js b/viewer/board.js index 81716d5..1edd33a 100644 --- a/viewer/board.js +++ b/viewer/board.js @@ -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) => ``, ) .join(""); - let svg = `${defs}${oceanDefs(prefix)}`; + let svg = `${defs}${oceanDefs(prefix)}`; let dikes = ""; for (const c of all) { const legal = targetSet.has(c.id), diff --git a/viewer/map-zoom.js b/viewer/map-zoom.js index 556f304..125cf07 100644 --- a/viewer/map-zoom.js +++ b/viewer/map-zoom.js @@ -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) => { diff --git a/viewer/style.css b/viewer/style.css index 3b07501..84b047d 100644 --- a/viewer/style.css +++ b/viewer/style.css @@ -1606,9 +1606,6 @@ body:has(.vanuatu) { scroll-behavior: auto !important; } } -.vanuatu .archipelago { - overflow: hidden; -} .vanuatu .map-scroll { padding-bottom: 145px; }