diff --git a/PLAN.md b/PLAN.md index c8a1c6e..cea6b31 100644 --- a/PLAN.md +++ b/PLAN.md @@ -124,7 +124,8 @@ sources/ build/ web/tiles/ gitignored | --- | --- | --- | | psd-tools fills stroke-only vector `shape` layers as solid colour | Customs rendered white over the whole playable area until `Map Borders` was excluded | Worked around via `excludeLayers`; documented as a check in the runbook | | Excluding `Map Borders` also removes the playable-area boundary outline | Customs no longer shows the dotted play-area edge; the out-of-bounds grey-out still marks it | Accepted. Revisit only if the boundary is missed | -| No retina / DPR-2 tiles | Slightly soft on high-DPI phones at 1:1 | Stage 7 | +| ~~No retina / DPR tiles~~ | ~~Soft on high-DPI screens~~ | **Fixed** — `DENSITY_BOOST` requests one level deeper; 0.56 → 1.33 source px per device px at DPR 1.5 | +| z5 is a hard detail ceiling | Upscaling past zoom 5 is browser-side and blurry | Inherent. The embedded terrain smart object is 7832 × 3720 placed 1:1, so there is no further source detail to extract | | `flyToBounds` depends on `requestAnimationFrame` | Picker doesn't move the view in a non-compositing tab | Cosmetic; not seen in a real browser | | Dorms has no scale bar | No px/m for that map | Fine — `pxPerMeter` is optional | @@ -197,7 +198,7 @@ sources/ build/ web/tiles/ gitignored - [x] Collapsible picker; zoom buttons hidden under 640 px in favour of pinch - [ ] **Test on a real iOS Safari and a real Android Chrome over LAN** — nothing here is proven until this happens - [ ] Check pinch, double-tap and inertial pan don't fight page scroll -- [ ] Decide on retina: one extra zoom level vs. `@2x` tiles vs. accept +- [x] Retina decided: `DENSITY_BOOST` requests one pyramid level deeper instead of shipping `@2x` tiles — no extra build output, fixes fractional-zoom softness too - [ ] First-paint budget on LAN (target ~2 s) #### Stage 8 — Third map by someone else diff --git a/README.md b/README.md index d58f1ee..86ff51b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,34 @@ up front (z3 is 49 tiles / 471 KB) — z2 is the sweet spot. | Customs | 7832 × 5016 | 7832 × 3015 | 503 (8.4 MB) | | Customs — Dorms | 4301 × 4904 | 4301 × 4904 | 469 (1.4 MB) | +## Tile resolution and sharpness + +`maxZoom` is derived from the largest map: `ceil(log2(7832 / 256)) = 5`. At z5 +one source pixel is one CSS pixel, and z5 *is* the full PSD — verified by +reading the embedded terrain smart object, which is exactly 7832 × 3720 placed +1:1. There is no hidden detail to recover, so z5 is a hard ceiling. + +Sharpness is therefore about **level selection**, not pyramid depth. Leaflet +picks its level with `Math.round(mapZoom)`, so at zoom 2.25 it takes level 2 and +stretches it 1.19×. On a 150 %-scaled Windows display (`devicePixelRatio` 1.5) +that compounds to a **1.78× upscale** — visibly soft. + +`DENSITY_BOOST` in `app.js` fixes it: halve the CSS size of a tile and request +one level deeper, so we downscale a denser level instead of upscaling a coarser +one. Measured at `devicePixelRatio` 1.5, in source pixels delivered per device +pixel (higher is sharper, 1.0 is pixel-perfect): + +| View zoom | Before | After | +| --- | --- | --- | +| 0–4 | 0.56 | **1.33** | +| 5 (1:1 source) | 0.67 | 0.67 | + +It costs ~4× the tiles in view (99 at the busiest level on a 1315 px viewport, +~17 KB each). Cached for a week and served by Caddy, that is a fair trade. + +At z5 and beyond you are past the source resolution and the browser upscales — +unavoidable, and `ZOOM_HEADROOM` deliberately allows two levels of it. + ## Serving Two options, both driven by `server/run.ps1`: diff --git a/j0s1bmig2kzd1.jpeg b/j0s1bmig2kzd1.jpeg new file mode 100644 index 0000000..6939677 Binary files /dev/null and b/j0s1bmig2kzd1.jpeg differ diff --git a/web/app.js b/web/app.js index 1d64c4a..5156628 100644 --- a/web/app.js +++ b/web/app.js @@ -9,15 +9,37 @@ */ const ZOOM_HEADROOM = 2; // how far past 1:1 the user may zoom +const ZOOM_SNAP = 0.25; const BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; +/* Pixel density. + * + * Leaflet picks its tile level with Math.round(mapZoom), so at zoom 2.25 it + * takes level 2 and stretches it 1.19x. Multiply that by a devicePixelRatio + * of 1.5 and each source pixel covers ~1.8 device pixels -- visibly soft. + * + * DENSITY_BOOST fixes it by halving the CSS size of a tile and asking for one + * level deeper, so we downscale a denser level instead of upscaling a coarser + * one. Costs ~4x the tiles in view, which at ~17 KB each is a fair trade. + * + * Worth it whenever the screen is not 1:1, or whenever fractional zoom can + * land us between levels. Both are true almost everywhere. + */ +const DENSITY_BOOST = + (window.devicePixelRatio > 1.05 || ZOOM_SNAP < 1) ? 1 : 0; + const OffsetTileLayer = L.TileLayer.extend({ getTileUrl(coords) { - const n = 2 ** coords.z; + // Tiles are numbered from each map's own top-left corner, so shift world + // tile coordinates into the map's local grid. A grid cell spans + // baseTileSize * 2**z pixels at this level; divided by the layer's own + // tile size that is always a whole number of tiles, boost or no boost. + const o = this.options; + const perCell = (o.baseTileSize * 2 ** coords.z) / o.tileSize; return L.Util.template(this._url, { - z: coords.z, - x: coords.x - this.options.cell[0] * n, - y: coords.y - this.options.cell[1] * n, + z: coords.z + o.zoomOffset, + x: coords.x - o.cell[0] * perCell, + y: coords.y - o.cell[1] * perCell, }); }, }); @@ -60,7 +82,7 @@ async function main() { crs: CRS, minZoom: 0, maxZoom: ZMAX + ZOOM_HEADROOM, - zoomSnap: 0.25, + zoomSnap: ZOOM_SNAP, zoomDelta: 0.5, wheelPxPerZoomLevel: 90, maxBounds: extent.pad(0.08), @@ -74,12 +96,18 @@ async function main() { const UNDERLAY_Z = Math.min(world.underlayZoom ?? 2, ZMAX); + // With the boost on, a level-N request is served by the level-(N+1) files, + // so the deepest level we may ask Leaflet for is one shallower. + const boost = Math.min(DENSITY_BOOST, ZMAX, UNDERLAY_Z); + for (const m of world.maps) { const url = `tiles/${m.id}/{z}/{x}/{y}.webp`; const common = { cell: m.cell, bounds: rectOf(m), - tileSize: world.tileSize, + baseTileSize: world.tileSize, + tileSize: world.tileSize / 2 ** boost, + zoomOffset: boost, minZoom: 0, maxZoom: ZMAX + ZOOM_HEADROOM, noWrap: true, @@ -93,9 +121,9 @@ async function main() { // blurry map instead of the page background. new OffsetTileLayer(url, { ...common, - minNativeZoom: UNDERLAY_Z, - maxNativeZoom: UNDERLAY_Z, - keepBuffer: 8, + minNativeZoom: UNDERLAY_Z - boost, + maxNativeZoom: UNDERLAY_Z - boost, + keepBuffer: 8 * 2 ** boost, updateWhenIdle: false, updateWhenZooming: false, zIndex: 1, @@ -104,8 +132,11 @@ async function main() { new OffsetTileLayer(url, { ...common, - maxNativeZoom: ZMAX, - keepBuffer: 3, + maxNativeZoom: ZMAX - boost, + // Small on purpose: boosted tiles are half-size, so a wide buffer costs + // 4x the requests, and the underlay already covers anything a pan + // outruns. + keepBuffer: 2, updateWhenIdle: false, zIndex: 2, }).addTo(map);