Each map now renders twice. The lower layer pins minNativeZoom and maxNativeZoom to underlayZoom (2), so Leaflet serves that single level at every view zoom, scaled to fit. The entire world at z2 is 17 tiles / 134 KB, so it paints in ~120ms and stays resident behind a generous keepBuffer. Detail tiles draw over it at zIndex 2, so any gap while they load shows blurry map instead of the page background. Verified zoom 0-7: zero broken tiles, no console errors. Co-Authored-By: Claude Opus 5 <[email protected]>
156 lines
4.8 KiB
JavaScript
156 lines
4.8 KiB
JavaScript
/* Tarkov Maps -- every map on one shared canvas.
|
|
*
|
|
* Coordinates: LatLng is (y, x) in *world pixels* at native resolution.
|
|
* A map placed in grid cell (gx, gy) occupies the world rectangle
|
|
* [gx*cellSize, gy*cellSize] .. [+width, +height]. Because cellSize is
|
|
* tileSize * 2**maxZoom, the tile-index shift for a cell is exactly
|
|
* cell * 2**z at every zoom -- so each map keeps its own locally numbered
|
|
* pyramid and we translate on the way out in getTileUrl().
|
|
*/
|
|
|
|
const ZOOM_HEADROOM = 2; // how far past 1:1 the user may zoom
|
|
const BLANK = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';
|
|
|
|
const OffsetTileLayer = L.TileLayer.extend({
|
|
getTileUrl(coords) {
|
|
const n = 2 ** coords.z;
|
|
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,
|
|
});
|
|
},
|
|
});
|
|
|
|
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;
|
|
|
|
// CRS where one LatLng unit == one source pixel at zoom ZMAX, y pointing
|
|
// down so world coordinates read the same way as image coordinates.
|
|
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 = world.maps.reduce(
|
|
(acc, m) => (acc ? acc.extend(rectOf(m)) : rectOf(m)), null,
|
|
);
|
|
|
|
const map = L.map('map', {
|
|
crs: CRS,
|
|
minZoom: 0,
|
|
maxZoom: ZMAX + ZOOM_HEADROOM,
|
|
zoomSnap: 0.25,
|
|
zoomDelta: 0.5,
|
|
wheelPxPerZoomLevel: 90,
|
|
maxBounds: extent.pad(0.08),
|
|
maxBoundsViscosity: 0.75,
|
|
attributionControl: false,
|
|
});
|
|
|
|
L.control.attribution({ prefix: false })
|
|
.addAttribution(world.attribution)
|
|
.addTo(map);
|
|
|
|
const UNDERLAY_Z = Math.min(world.underlayZoom ?? 2, ZMAX);
|
|
|
|
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,
|
|
minZoom: 0,
|
|
maxZoom: ZMAX + ZOOM_HEADROOM,
|
|
noWrap: true,
|
|
errorTileUrl: BLANK,
|
|
};
|
|
|
|
// Backdrop. Pinning min and max native zoom to the same low level makes
|
|
// Leaflet serve that level at every view zoom, scaled to fit -- the whole
|
|
// world is a handful of tiles, so it lands almost immediately and then
|
|
// stays put. Detail tiles draw over it, and any gap between them shows
|
|
// blurry map instead of the page background.
|
|
new OffsetTileLayer(url, {
|
|
...common,
|
|
minNativeZoom: UNDERLAY_Z,
|
|
maxNativeZoom: UNDERLAY_Z,
|
|
keepBuffer: 8,
|
|
updateWhenIdle: false,
|
|
updateWhenZooming: false,
|
|
zIndex: 1,
|
|
className: 'tile-underlay',
|
|
}).addTo(map);
|
|
|
|
new OffsetTileLayer(url, {
|
|
...common,
|
|
maxNativeZoom: ZMAX,
|
|
keepBuffer: 3,
|
|
updateWhenIdle: false,
|
|
zIndex: 2,
|
|
}).addTo(map);
|
|
}
|
|
|
|
// --- picker -----------------------------------------------------------
|
|
const list = document.getElementById('maplist');
|
|
const buttons = world.maps.map((m) => {
|
|
const b = document.createElement('button');
|
|
b.textContent = m.name;
|
|
b.dataset.id = m.id;
|
|
b.addEventListener('click', () => {
|
|
map.flyToBounds(rectOf(m), { padding: [20, 20], duration: 0.6 });
|
|
if (window.matchMedia('(max-width: 640px)').matches) {
|
|
document.getElementById('panel').open = false;
|
|
}
|
|
});
|
|
list.appendChild(b);
|
|
return b;
|
|
});
|
|
|
|
const syncUI = () => {
|
|
const c = map.getCenter();
|
|
const active = world.maps.find((m) => rectOf(m).contains(c));
|
|
buttons.forEach((b) => b.setAttribute('aria-current', String(active?.id === b.dataset.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);
|
|
|
|
// --- initial view -----------------------------------------------------
|
|
const h = readHash();
|
|
if (h) map.setView([h.y, h.x], h.z);
|
|
else map.fitBounds(rectOf(world.maps[0]), { padding: [20, 20] });
|
|
|
|
syncUI();
|
|
document.getElementById('boot').remove();
|
|
|
|
window.tarkovMap = map; // handy from the console when checking a new map
|
|
window.tarkovWorld = world;
|
|
}
|
|
|
|
main().catch((err) => {
|
|
document.getElementById('boot').textContent = `Failed to load: ${err.message}`;
|
|
console.error(err);
|
|
});
|