Files
TarkovMaps/web/app.js
T
Claude f49a00bc98 Version tile URLs so rebuilt maps cannot serve a previous build
Tile paths are stable across rebuilds but their contents are not: moving a
map's `place` makes tiles/<id>/6/17/33.webp different imagery at the same
URL. Combined with the immutable week-long cache added earlier, a browser
that had seen an older build blended the two and drew duplicated, offset
terrain -- visible as a second copy of Lighthouse.

Each map now carries a `version` in world.json, hashed from its geometry,
zoom range and source file stamp, and appended to every tile URL as ?v=.
Re-tiled maps get fresh URLs; identical rebuilds keep the same version so
nothing is re-fetched needlessly.

Also corrects the runbook's base-layer table, which still listed the
pre-correction layers for Woods, Reserve, Interchange and Streets.

Verified: 733 tiles across seven map views, all carrying ?v=, zero broken,
and the focused map topmost in every case.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-08 14:59:41 -07:00

180 lines
6.4 KiB
JavaScript

/* Tarkov Maps -- every map on one shared canvas.
*
* The base layer is the overall satellite map, rotated so north is up. Every
* detail map is scaled and positioned onto it, so zooming into Customs on the
* world map lands you in the full-resolution re3mr Customs map.
*
* Coordinates: LatLng is (y, x) in *world pixels* at native resolution, where
* one world pixel is 1/overallScale of an overall-map pixel. Each map's tiles
* are numbered from its own padded cell origin -- see tools/common.py.
*/
const ZOOM_HEADROOM = 1; // how far past a layer's native detail we allow
const ZOOM_SNAP = 0.25;
const BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
/* Leaflet picks its tile level with Math.round(mapZoom), so at zoom 2.25 it
* takes level 2 and stretches it 1.19x; on a 1.5x display that compounds to a
* visibly soft 1.78x upscale. Halving a tile's CSS size and asking for one
* level deeper makes us downscale a denser level instead. */
const DENSITY_BOOST = (window.devicePixelRatio > 1.05 || ZOOM_SNAP < 1) ? 1 : 0;
const OffsetTileLayer = L.TileLayer.extend({
getTileUrl(coords) {
const o = this.options;
const perCell = (o.baseTileSize * 2 ** coords.z) / o.tileSize;
// ?v= busts the week-long immutable cache when a map is re-tiled; the same
// URL would otherwise serve a previous build's imagery.
return L.Util.template(this._url, {
z: coords.z + o.zoomOffset,
x: coords.x - o.cell[0] * perCell,
y: coords.y - o.cell[1] * perCell,
}) + (o.version ? `?v=${o.version}` : '');
},
});
const rectOf = (m) => L.latLngBounds(
[m.origin[1], m.origin[0]],
[m.origin[1] + m.size[1], m.origin[0] + m.size[0]],
);
function readHash() {
const p = location.hash.replace(/^#/, '').split('/');
if (p.length !== 3) return null;
const [z, y, x] = p.map(Number);
return [z, y, x].every(Number.isFinite) ? { z, y, x } : null;
}
async function main() {
const world = await fetch('data/world.json', { cache: 'no-cache' }).then((r) => {
if (!r.ok) throw new Error(`world.json: ${r.status}`);
return r.json();
});
if (!world.maps.length) throw new Error('world.json contains no maps');
const ZMAX = world.maxZoom;
const base = world.maps.find((m) => m.base) ?? world.maps[0];
const CRS = L.extend({}, L.CRS.Simple, {
transformation: new L.Transformation(1, 0, 1, 0),
scale: (z) => 2 ** (z - ZMAX),
zoom: (s) => Math.log(s) / Math.LN2 + ZMAX,
});
const extent = L.latLngBounds([0, 0], [world.worldSize[1], world.worldSize[0]]);
const map = L.map('map', {
crs: CRS,
minZoom: 0,
maxZoom: ZMAX + ZOOM_HEADROOM,
zoomSnap: ZOOM_SNAP,
zoomDelta: 0.5,
wheelPxPerZoomLevel: 90,
maxBounds: extent.pad(0.05),
maxBoundsViscosity: 0.75,
attributionControl: false,
});
L.control.attribution({ prefix: false })
.addAttribution(world.attribution)
.addTo(map);
const boost = DENSITY_BOOST;
/* Stacking. Placements are approximate, so map rectangles overlap even when
* the real locations don't (Reserve clips Customs, Lighthouse clips
* Shoreline). Baseline order puts the finer map on top: sort by
* maxNativeZoom, then by smaller footprint. `focus()` then lifts whichever
* map you are actually looking at above the rest, so an overlapping
* neighbour can never hide the map you came to see. */
const detail = world.maps.filter((m) => !m.base);
const stackOrder = [...detail].sort(
(a, b) => (a.maxNativeZoom - b.maxNativeZoom)
|| (b.size[0] * b.size[1] - a.size[0] * a.size[1]),
);
const BASE_Z = 1;
const baseZ = new Map(stackOrder.map((m, i) => [m.id, 2 + i]));
const TOP_Z = 2 + stackOrder.length;
const layers = new Map();
const focus = (id) => {
for (const [mid, layer] of layers) {
if (mid === base.id) continue;
layer.setZIndex(mid === id ? TOP_Z : baseZ.get(mid));
}
};
for (const m of world.maps) {
// The base map is the backdrop: it is a handful of tiles, always loaded,
// and Leaflet upscales it past its native level -- so a gap while detail
// tiles load shows blurry terrain rather than the page background.
const isBase = !!m.base;
const layer = new OffsetTileLayer(`tiles/${m.id}/{z}/{x}/{y}.webp`, {
cell: m.cell,
bounds: rectOf(m),
version: m.version,
baseTileSize: world.tileSize,
tileSize: world.tileSize / 2 ** boost,
zoomOffset: boost,
minZoom: isBase ? 0 : m.minZoom,
maxZoom: ZMAX + ZOOM_HEADROOM,
maxNativeZoom: Math.max(0, m.maxNativeZoom - boost),
minNativeZoom: 0,
noWrap: true,
keepBuffer: isBase ? 6 : 2,
updateWhenIdle: false,
errorTileUrl: BLANK,
zIndex: isBase ? BASE_Z : baseZ.get(m.id),
className: isBase ? 'tile-base' : undefined,
}).addTo(map);
layers.set(m.id, layer);
}
// --- picker -----------------------------------------------------------
const list = document.getElementById('maplist');
const targets = [base, ...world.maps.filter((m) => !m.base)];
const buttons = targets.map((m) => {
const b = document.createElement('button');
b.textContent = m.base ? 'Overview' : m.name;
b.dataset.id = m.id;
b.addEventListener('click', () => {
map.flyToBounds(rectOf(m), { padding: [24, 24], duration: 0.7 });
if (window.matchMedia('(max-width: 640px)').matches) {
document.getElementById('panel').open = false;
}
});
list.appendChild(b);
return b;
});
const syncUI = () => {
const c = map.getCenter();
// Smallest non-base map containing the centre wins -- Dorms beats Customs.
const hit = world.maps
.filter((m) => !m.base && rectOf(m).contains(c) && map.getZoom() >= m.minZoom)
.sort((a, b) => a.size[0] * a.size[1] - b.size[0] * b.size[1])[0];
buttons.forEach((b) => b.setAttribute(
'aria-current', String(hit ? hit.id === b.dataset.id : b.dataset.id === base.id)));
focus(hit?.id);
const z = Math.round(map.getZoom() * 4) / 4;
history.replaceState(null, '', `#${z}/${Math.round(c.lat)}/${Math.round(c.lng)}`);
};
map.on('moveend zoomend', syncUI);
const h = readHash();
if (h) map.setView([h.y, h.x], h.z);
else map.fitBounds(rectOf(base), { padding: [10, 10] });
syncUI();
document.getElementById('boot').remove();
window.tarkovMap = map;
window.tarkovWorld = world;
}
main().catch((err) => {
document.getElementById('boot').textContent = `Failed to load: ${err.message}`;
console.error(err);
});