feat(client): map tiles, grid and pan/zoom (#69)

T6 of spec #63. Tiles are requested only for the viewport plus one tile of
margin, in viewport order, at most 8 at a time (TileScheduler); decoded
textures are cached by URL so a reconnect refetches nothing; a 403/404 is
remembered as denied and never retried. The browser image loader is the hot
path, with one status fetch only to classify an error.

Camera: drag to pan, wheel/pinch to zoom around the pointer/midpoint; players
clamp at 0.5 (ADR-0013) and the GM has no player floor; both stop at 4.0.
Grid is one TilingSprite toggled by #grid-toggle. Camera is local UI state,
not a reducer slice, so hello/reconnect does not move the view.

Tests: viewport/tile unit tests (clamps, margin window, 8-wide queue, denial,
cache) and client/e2e/map.test.mjs (mobile viewport fill gate (a), grid,
pinch, cache across reconnect, GM zoom, 403 no-retry) against the real
server through TLS/h2. Tile-size gate (a) documented in client/README.md.
This commit is contained in:
2026-11-21 01:11:12 +01:00
parent 0323fbd2ae
commit 4534ed6426
9 changed files with 1469 additions and 0 deletions
+38
View File
@@ -100,6 +100,44 @@ change them only if a slice needs a new *core* event, and say so in the PR.
network with `context.setOffline`, asserts the badge transitions and the full resync frames, and
runs two pages on one key.
## Map tiles, grid and pan/zoom (T6, #69)
- **Camera** (`src/viewport.ts`, pure): the world container is `screen = world * zoom + (x, y)`.
Players clamp at `0.5` and the GM has no player floor (a `1/64` sanity floor only keeps the
transform non-singular); both clamp at `4.0` (ADR-0013, spec #63 decision 7). Wheel and pinch zoom
around the pointer/midpoint, one-finger drag pans. Input is hand-rolled DOM pointer/wheel events
on the canvas (`src/render/modules/camera.ts`, DESIGN 7.2: no Pixi federated events), and the
canvas gets `touch-action: none`.
- **Camera is not a reducer slice.** It is local UI state; a `hello` resets every slice by design,
and a reconnect must not move the player's view. `ViewportModel` is owned by the render layer and
reset only when a new stage is created. The canvas carries the e2e facts `data-zoom`, `data-grid`
and `data-view` (viewport px).
- **Tiles** (`src/tiles.ts` scheduler + `src/render/modules/tiles.ts` layer): the wanted set is
`visibleTiles()` — every tile intersecting the viewport plus exactly one tile of margin, clamped
to the scene, in viewport order (row-major). The scheduler keeps at most `TILE_CONCURRENCY` (8)
loads in flight, drops queued tiles that leave the window, never asks twice for a tile, and
remembers a `403`/`404` as `denied` forever: the gate has said "not yours", so it is never retried
in a loop. A transient failure is retried only on a new window or after a reconnect (`hello`).
The decoded-texture cache is module-level and keyed by URL, so a reconnect keeps every tile and
the page makes no new tile request. `data-tiles` is `ready` once every wanted tile has settled
(`ok` or `denied`); `data-tile-requests` counts image attempts (plus the single status fetch used
to classify an image error).
- **Why `<img>` and not `fetch` + `createImageBitmap`:** the browser's image loader does the
fetch/decode off the JS hot path; one `onload` per tile is the cheapest way to put the ~480 tiles
of a phone viewport on the GPU. A failed image is classified once with `fetch`, which is where the
`403` distinction comes from.
- **Grid** (`src/render/modules/grid.ts`): one `TilingSprite` over a `grid_size` cell texture, drawn
once, toggled by `#grid-toggle` (story 18). No per-frame redraw.
- **Tile-size gate (a)** (`client/e2e/map.test.mjs`): mobile emulation 412×869 CSS px, DPR 2.625,
touch; real server through the TLS/HTTP2 nginx; viewport **412×869 at zoom 1**. The measurement is
from the canvas becoming visible to `data-tiles=ready`, i.e. the first viewport filling; the budget
is **< 1 s** (DESIGN §13). Measured on this dev host: **~0.4–0.8 s** for the 480-tile window
(marginal under parallel test load). The same test then applies CDP's 4× CPU throttle for the grid,
pinch and reconnect checks. Note: under the 4× throttle the *fill itself* takes ~3.2 s, because CDP
multiplies each of the ~480 image-load callbacks; that measures the throttler rather than the app,
so the gate is measured at real CPU and gate (b), the real-phone run (#77), remains authoritative
for phone speed.
## Tokens, labels, statuses and the two lists (T8, #71)
- **State**: `src/slices/tokens.ts` (`token` upsert, `token_gone`, `hello` reset; `tokensOf`),