The map's zoom was capped at the deepest level that has tiles, which leaves almost no room on a shallow pyramid: a 1024px map only has levels 0 to 2, so fitBounds already landed at the maximum and the zoom-in button was disabled from the moment the page loaded. Reproduced against a copy of the live map (1024x1024, max_zoom 2): zoom-in disabled on load, clicking it did nothing. The tile layer now declares maxNativeZoom as the deepest level that really exists, and the map may zoom two levels beyond it - Leaflet scales those tiles up instead of requesting levels that would 404. Verified across the range: level 1 at zoom 1.25, native level 2 at zoom 2, and the same level 2 tiles upscaled to 362/512/724/1024px at zoom 2.5 through 4, with no request for the non-existent level 3. This also removes detectRetina, which made the problem worse rather than better: it costs a zoom level of magnification, not just resolution, so on a high-density display a three-level map lost a third of its already minimal range. tools/tile_viewer.html had the same constraint one level up and now allows two levels past native as well. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
760 lines
25 KiB
HTML
760 lines
25 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="de">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||
<title>Kachel-Viewer</title>
|
||
<style>
|
||
:root {
|
||
--bg: #0e1013;
|
||
--gap: #1b2029;
|
||
--panel: rgba(18, 20, 25, 0.86);
|
||
--border: #333a45;
|
||
--text: #e6e8ec;
|
||
--muted: #8d95a3;
|
||
--accent: #c9a227;
|
||
}
|
||
|
||
* { box-sizing: border-box; }
|
||
|
||
html, body {
|
||
margin: 0;
|
||
height: 100%;
|
||
overflow: hidden;
|
||
background: var(--bg);
|
||
color: var(--text);
|
||
font-family: "Segoe UI", Roboto, Arial, sans-serif;
|
||
}
|
||
|
||
/* touch-action: none, damit Pointer-Events das Pannen/Zoomen selbst steuern und
|
||
der Browser nicht dazwischenfunkt (Scrollen, Pull-to-refresh, Doppeltipp-Zoom). */
|
||
#map {
|
||
display: block;
|
||
width: 100%;
|
||
height: 100%;
|
||
touch-action: none;
|
||
cursor: grab;
|
||
}
|
||
|
||
#map.dragging { cursor: grabbing; }
|
||
|
||
#hud {
|
||
position: fixed;
|
||
top: 12px;
|
||
left: 12px;
|
||
padding: 10px 12px;
|
||
background: var(--panel);
|
||
backdrop-filter: blur(6px);
|
||
-webkit-backdrop-filter: blur(6px);
|
||
border: 1px solid var(--border);
|
||
border-radius: 8px;
|
||
font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
|
||
font-size: 12px;
|
||
line-height: 1.55;
|
||
color: var(--muted);
|
||
pointer-events: none;
|
||
min-width: 210px;
|
||
}
|
||
|
||
#hud b { color: var(--text); font-weight: 600; }
|
||
#hud .row { display: flex; justify-content: space-between; gap: 14px; }
|
||
#hud .sep { height: 1px; background: var(--border); margin: 7px 0; }
|
||
|
||
#hud .buttons {
|
||
display: flex;
|
||
gap: 6px;
|
||
margin-top: 8px;
|
||
pointer-events: auto;
|
||
}
|
||
|
||
#hud button {
|
||
flex: 1;
|
||
font: inherit;
|
||
padding: 4px 8px;
|
||
color: var(--text);
|
||
background: #202632;
|
||
border: 1px solid var(--border);
|
||
border-radius: 5px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
#hud button:hover { border-color: var(--accent); color: var(--accent); }
|
||
|
||
#help {
|
||
position: fixed;
|
||
left: 12px;
|
||
bottom: 12px;
|
||
padding: 7px 10px;
|
||
background: var(--panel);
|
||
border: 1px solid var(--border);
|
||
border-radius: 8px;
|
||
font-family: ui-monospace, Consolas, monospace;
|
||
font-size: 11px;
|
||
color: var(--muted);
|
||
pointer-events: none;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<canvas id="map"></canvas>
|
||
|
||
<div id="hud">
|
||
<div class="row"><span>Maßstab</span><b id="hud-scale">–</b></div>
|
||
<div class="row"><span>Ebene z</span><b id="hud-level">–</b></div>
|
||
<div class="row"><span>Kachel</span><b id="hud-tile">–</b></div>
|
||
<div class="row"><span>Welt</span><b id="hud-world">–</b></div>
|
||
<div class="sep"></div>
|
||
<div class="row"><span>geladen</span><b id="hud-loaded">0</b></div>
|
||
<div class="row"><span>fehlend</span><b id="hud-absent">0</b></div>
|
||
<div class="row"><span>Lücken/Bild</span><b id="hud-gaps">0</b></div>
|
||
<div class="row"><span>Cache</span><b id="hud-cache">0</b></div>
|
||
<div class="buttons">
|
||
<button id="btn-reset" type="button" title="Ansicht zurücksetzen (R)">Reset</button>
|
||
<button id="btn-png" type="button" title="Aktuelle Ansicht als PNG speichern">PNG</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="help">Ziehen = verschieben · Rad/Pinch = zoomen · Pfeile · +/− · R</div>
|
||
|
||
<script>
|
||
'use strict';
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Konfiguration
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Alles Wesentliche laesst sich ueber die Adresszeile einstellen, damit dieselbe Datei
|
||
* fuer verschiedene Kartensaetze taugt:
|
||
*
|
||
* ?tiles=<muster> URL-Muster der Kacheln mit den Platzhaltern {z}, {x}, {y}
|
||
* ?zmin=&zmax= vorhandene Zoomstufen
|
||
* ?x=&y= Startpunkt in Weltpixeln
|
||
*
|
||
* Voreingestellt ist das Ablageschema von tools/make_map_tiles.php aus dem
|
||
* Website-Projekt (<schluessel>/<z>/<x>_<y>.jpg). Ein flacher Ordner mit
|
||
* {z}_{x}_{y}.jpg wird so angesprochen:
|
||
*
|
||
* index.html?tiles=elysea_tiles/{z}_{x}_{y}.jpg&zmin=2&zmax=6
|
||
*/
|
||
const params = new URLSearchParams(location.search);
|
||
|
||
function param(name, fallback) {
|
||
const value = params.get(name);
|
||
return value === null || value === '' ? fallback : value;
|
||
}
|
||
|
||
function numParam(name, fallback) {
|
||
const value = Number(params.get(name));
|
||
return Number.isFinite(value) && params.get(name) !== null ? value : fallback;
|
||
}
|
||
|
||
/** URL-Muster der Kacheln. {z}, {x}, {y} werden ersetzt. */
|
||
const TILE_URL = param('tiles', 'tiles/{z}/{x}_{y}.jpg');
|
||
|
||
const ZMIN = numParam('zmin', 2);
|
||
const ZMAX = numParam('zmax', 6);
|
||
const TILE_PX = numParam('tilesize', 256);
|
||
|
||
/**
|
||
* Feste "Welt" auf der hoechsten Ebene: Weltkoordinaten sind schlichte Pixel in
|
||
* [0, WORLD)^2. Jede Ebene ist damit dieselbe Pyramide - vier (z+1)-Kacheln decken
|
||
* exakt eine z-Kachel ab.
|
||
*/
|
||
const WORLD = TILE_PX * (1 << ZMAX); // 16384
|
||
|
||
/** Startansicht in Weltpixeln. Ohne Angabe die Mitte der Karte. */
|
||
const START_X = numParam('x', WORLD / 2);
|
||
const START_Y = numParam('y', WORLD / 2);
|
||
|
||
/** Fuellfarbe, wenn zu einem Bereich auf keiner Ebene Material existiert. */
|
||
const GAP_COLOR = '#1b2029';
|
||
|
||
const MAX_INFLIGHT = 8; // gleichzeitige Netzwerk-Anfragen
|
||
const CACHE_LIMIT = 512; // dekodierte Bitmaps im LRU-Cache
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Kachel-Geometrie
|
||
//
|
||
// Diese beiden Funktionen sind die EINZIGE Quelle fuer "welche Kachel deckt welchen
|
||
// Weltbereich ab". Renderer und HUD benutzen beide - dadurch koennen Anzeige und
|
||
// Daten nicht auseinanderlaufen.
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/** Weltgroesse einer Kachel auf Ebene z (Kantenlaenge in Weltpixeln). */
|
||
function tileSpan(z) {
|
||
return TILE_PX * (1 << (ZMAX - z));
|
||
}
|
||
|
||
/** Weltrechteck der Kachel (z, x, y). */
|
||
function tileWorldRect(z, x, y) {
|
||
const size = tileSpan(z);
|
||
return { x: x * size, y: y * size, size: size };
|
||
}
|
||
|
||
/** Kachelkoordinate, die den Weltpunkt (wx, wy) auf Ebene z enthaelt. */
|
||
function tileAtWorld(z, wx, wy) {
|
||
const size = tileSpan(z);
|
||
return { x: Math.floor(wx / size), y: Math.floor(wy / size) };
|
||
}
|
||
|
||
/** Anzahl Kacheln je Achse auf Ebene z. */
|
||
function tileCount(z) {
|
||
return 1 << z;
|
||
}
|
||
|
||
function tileKey(z, x, y) {
|
||
return z + '_' + x + '_' + y;
|
||
}
|
||
|
||
function tileUrl(z, x, y) {
|
||
return TILE_URL.replace('{z}', z).replace('{x}', x).replace('{y}', y);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Kamera
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const canvas = document.getElementById('map');
|
||
const ctx = canvas.getContext('2d', { alpha: false });
|
||
|
||
let viewW = 0, viewH = 0, dpr = 1;
|
||
let camX = START_X, camY = START_Y;
|
||
let k = 1; // Bildschirmpixel je Weltpixel
|
||
|
||
function worldToScreenX(wx) { return (wx - camX) * k + viewW / 2; }
|
||
function worldToScreenY(wy) { return (wy - camY) * k + viewH / 2; }
|
||
function screenToWorldX(sx) { return (sx - viewW / 2) / k + camX; }
|
||
function screenToWorldY(sy) { return (sy - viewH / 2) / k + camY; }
|
||
|
||
/**
|
||
* Grenzen fuer k.
|
||
*
|
||
* Nativ ist eine Ebene, wenn ein Kachel-Bildpixel genau einem Geraetepixel entspricht.
|
||
* Auf Ebene z deckt ein Bildpixel 2^(ZMAX-z) Weltpixel ab, nativ gilt also
|
||
* k * dpr * 2^(ZMAX-z) = 1. Fuer die hoechste Ebene (z = ZMAX) heisst das k = 1/dpr.
|
||
*
|
||
* Nach oben sind zwei Ebenen ueber nativ erlaubt (vierfache Vergroesserung), nach unten
|
||
* eine Halbierung unter "ganze Welt passt ins Fenster". Zwei statt einer Ebene deshalb,
|
||
* weil flache Pyramiden sonst kaum Spielraum lassen: eine 1024er-Karte hat nur die
|
||
* Stufen 0 bis 2, da waere die Startansicht schon fast am Anschlag.
|
||
*/
|
||
function scaleBounds() {
|
||
const fit = Math.min(viewW, viewH) / WORLD;
|
||
return { min: fit / 2, max: 4 / dpr };
|
||
}
|
||
|
||
function clampScale(value) {
|
||
const b = scaleBounds();
|
||
return Math.min(b.max, Math.max(b.min, value));
|
||
}
|
||
|
||
/** Kameramittelpunkt innerhalb der Welt halten. */
|
||
function clampCamera() {
|
||
camX = Math.min(WORLD, Math.max(0, camX));
|
||
camY = Math.min(WORLD, Math.max(0, camY));
|
||
}
|
||
|
||
/**
|
||
* Aktive Kachelebene: diejenige, deren Darstellung der nativen Aufloesung am naechsten
|
||
* kommt. Aus k * dpr * 2^(ZMAX-z) = 1 folgt z = ZMAX + log2(k * dpr).
|
||
*/
|
||
function activeLevel() {
|
||
const z = Math.round(ZMAX + Math.log2(k * dpr));
|
||
return Math.min(ZMAX, Math.max(ZMIN, z));
|
||
}
|
||
|
||
function resetView() {
|
||
camX = START_X;
|
||
camY = START_Y;
|
||
// Startmassstab: rund vier Level-6-Kacheln fuellen die Fensterbreite.
|
||
k = clampScale(viewW / (4 * TILE_PX));
|
||
requestRender();
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Kachel-Speicher: LRU-Cache, Ladewarteschlange, Fehlliste
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/** Map haelt die Einfuegereihenfolge - der aelteste Eintrag ist der erste. */
|
||
const cache = new Map();
|
||
const absent = new Set(); // dauerhaft nicht vorhanden (404 o.ae.)
|
||
const pending = new Set(); // laeuft gerade
|
||
const queue = []; // wartet auf einen freien Slot
|
||
let inflight = 0;
|
||
let loadedCount = 0;
|
||
|
||
const stats = { gaps: 0 };
|
||
|
||
function cacheGet(key) {
|
||
const bitmap = cache.get(key);
|
||
if (bitmap === undefined) return null;
|
||
// Zugriff macht den Eintrag wieder jung: loeschen und hinten neu einfuegen.
|
||
cache.delete(key);
|
||
cache.set(key, bitmap);
|
||
return bitmap;
|
||
}
|
||
|
||
function cachePut(key, bitmap) {
|
||
cache.set(key, bitmap);
|
||
while (cache.size > CACHE_LIMIT) {
|
||
const oldestKey = cache.keys().next().value;
|
||
const oldest = cache.get(oldestKey);
|
||
cache.delete(oldestKey);
|
||
// ImageBitmap haelt Speicher ausserhalb des JS-Heaps - explizit freigeben.
|
||
if (oldest && typeof oldest.close === 'function') oldest.close();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Stellt eine Kachel in die Warteschlange. Pro Koordinate wird genau ein Versuch
|
||
* unternommen - kein Wiederholen, keine Schleife.
|
||
*/
|
||
function request(z, x, y, priority) {
|
||
if (z < ZMIN || z > ZMAX) return;
|
||
const count = tileCount(z);
|
||
if (x < 0 || y < 0 || x >= count || y >= count) return;
|
||
|
||
const key = tileKey(z, x, y);
|
||
if (cache.has(key) || absent.has(key) || pending.has(key)) return;
|
||
|
||
pending.add(key);
|
||
if (priority) queue.unshift({ key, z, x, y });
|
||
else queue.push({ key, z, x, y });
|
||
pump();
|
||
}
|
||
|
||
function pump() {
|
||
while (inflight < MAX_INFLIGHT && queue.length > 0) {
|
||
const job = queue.shift();
|
||
inflight++;
|
||
loadTile(job).finally(() => {
|
||
inflight--;
|
||
pending.delete(job.key);
|
||
pump();
|
||
});
|
||
}
|
||
}
|
||
|
||
async function loadTile(job) {
|
||
try {
|
||
const response = await fetch(tileUrl(job.z, job.x, job.y), { cache: 'force-cache' });
|
||
if (!response.ok) throw new Error('HTTP ' + response.status);
|
||
|
||
const blob = await response.blob();
|
||
let bitmap;
|
||
if (typeof createImageBitmap === 'function') {
|
||
bitmap = await createImageBitmap(blob);
|
||
} else {
|
||
bitmap = await blobToImage(blob);
|
||
}
|
||
|
||
cachePut(job.key, bitmap);
|
||
loadedCount++;
|
||
} catch (err) {
|
||
// Fehlende Kacheln sind erwartbar (Raster ragt ueber das Kartengebiet hinaus).
|
||
absent.add(job.key);
|
||
}
|
||
requestRender();
|
||
}
|
||
|
||
/** Rueckfallweg fuer Browser ohne createImageBitmap. */
|
||
function blobToImage(blob) {
|
||
return new Promise((resolve, reject) => {
|
||
const url = URL.createObjectURL(blob);
|
||
const img = new Image();
|
||
img.onload = () => { URL.revokeObjectURL(url); resolve(img); };
|
||
img.onerror = () => { URL.revokeObjectURL(url); reject(new Error('decode')); };
|
||
img.src = url;
|
||
});
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Zeichnen
|
||
// ---------------------------------------------------------------------------
|
||
|
||
let renderPending = false;
|
||
|
||
function requestRender() {
|
||
if (renderPending) return;
|
||
renderPending = true;
|
||
requestAnimationFrame(render);
|
||
}
|
||
|
||
function resize() {
|
||
dpr = Math.min(3, window.devicePixelRatio || 1);
|
||
viewW = canvas.clientWidth;
|
||
viewH = canvas.clientHeight;
|
||
canvas.width = Math.max(1, Math.round(viewW * dpr));
|
||
canvas.height = Math.max(1, Math.round(viewH * dpr));
|
||
}
|
||
|
||
/**
|
||
* Zeichnet den Weltbereich rect aus dem Bild der Kachel (iz, ix, iy).
|
||
*
|
||
* Die Bildschirmkanten werden einzeln gerundet und die Breite daraus abgeleitet
|
||
* (rechts minus links). Dadurch benutzt eine Nachbarkachel fuer ihre linke Kante
|
||
* exakt denselben gerundeten Wert - es entstehen weder Luecken noch Ueberlappungen,
|
||
* auch nicht beim Uebergang zwischen Ebenen.
|
||
*/
|
||
function blit(image, iz, ix, iy, rect) {
|
||
const tile = tileWorldRect(iz, ix, iy);
|
||
const imgPerWorld = TILE_PX / tile.size;
|
||
|
||
const sx = (rect.x - tile.x) * imgPerWorld;
|
||
const sy = (rect.y - tile.y) * imgPerWorld;
|
||
const sw = rect.size * imgPerWorld;
|
||
|
||
const left = Math.round(worldToScreenX(rect.x) * dpr);
|
||
const top = Math.round(worldToScreenY(rect.y) * dpr);
|
||
const right = Math.round(worldToScreenX(rect.x + rect.size) * dpr);
|
||
const bottom = Math.round(worldToScreenY(rect.y + rect.size) * dpr);
|
||
|
||
const dw = right - left;
|
||
const dh = bottom - top;
|
||
if (dw <= 0 || dh <= 0) return;
|
||
|
||
// Glaetten nur beim Hochskalieren; bei 1:1 oder Verkleinerung bleiben Kanten scharf.
|
||
ctx.imageSmoothingEnabled = dw > sw + 0.5;
|
||
ctx.drawImage(image, sx, sy, sw, sw, left, top, dw, dh);
|
||
}
|
||
|
||
function fillGap(rect) {
|
||
const left = Math.round(worldToScreenX(rect.x) * dpr);
|
||
const top = Math.round(worldToScreenY(rect.y) * dpr);
|
||
const right = Math.round(worldToScreenX(rect.x + rect.size) * dpr);
|
||
const bottom = Math.round(worldToScreenY(rect.y + rect.size) * dpr);
|
||
ctx.fillStyle = GAP_COLOR;
|
||
ctx.fillRect(left, top, right - left, bottom - top);
|
||
}
|
||
|
||
/**
|
||
* Malt die Kachel (z, x, y) - notfalls aus Ersatzmaterial.
|
||
*
|
||
* Reihenfolge laut Vorgabe: erst das Original, dann mehr Detail (alle vier Kinder auf
|
||
* z+1, aber nur wenn alle vier da sind), dann grobere Elternkacheln nach oben bis ZMIN,
|
||
* zuletzt eine neutrale Fuellfarbe.
|
||
*/
|
||
function paintTile(z, x, y) {
|
||
const rect = tileWorldRect(z, x, y);
|
||
const key = tileKey(z, x, y);
|
||
|
||
const image = cacheGet(key);
|
||
if (image) {
|
||
blit(image, z, x, y, rect);
|
||
return;
|
||
}
|
||
|
||
if (!absent.has(key)) {
|
||
request(z, x, y, true);
|
||
// Das Elternteil gleich mitbestellen: es ist der Ersatz, falls diese Kachel
|
||
// fehlt oder noch laedt - ohne das waere die Rueckfallregel unten wirkungslos,
|
||
// weil auf der aktiven Ebene nur diese eine Ebene im Cache liegt.
|
||
if (z > ZMIN) request(z - 1, x >> 1, y >> 1, false);
|
||
}
|
||
|
||
// 1) Mehr Detail: vier Kinder, aber nur vollstaendig (sonst entstuenden Loecher).
|
||
if (z < ZMAX) {
|
||
const cz = z + 1;
|
||
const kids = [[2 * x, 2 * y], [2 * x + 1, 2 * y], [2 * x, 2 * y + 1], [2 * x + 1, 2 * y + 1]];
|
||
const images = kids.map(([kx, ky]) => cacheGet(tileKey(cz, kx, ky)));
|
||
if (images.every(Boolean)) {
|
||
kids.forEach(([kx, ky], i) => blit(images[i], cz, kx, ky, tileWorldRect(cz, kx, ky)));
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 2) Weniger Detail: naechstbeste Elternkachel, hochskaliert.
|
||
for (let pz = z - 1; pz >= ZMIN; pz--) {
|
||
const shift = z - pz;
|
||
const px = x >> shift;
|
||
const py = y >> shift;
|
||
const parent = cacheGet(tileKey(pz, px, py));
|
||
if (parent) {
|
||
blit(parent, pz, px, py, rect);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 3) Nichts vorhanden.
|
||
fillGap(rect);
|
||
stats.gaps++;
|
||
}
|
||
|
||
/** Laedt den Ring um den sichtbaren Bereich vor (aktuelle und naechsthoehere Ebene). */
|
||
function prefetch(z, tx0, ty0, tx1, ty1) {
|
||
const pad = Math.max(1, Math.ceil((tx1 - tx0 + 1) / 2));
|
||
for (let x = tx0 - pad; x <= tx1 + pad; x++) {
|
||
for (let y = ty0 - pad; y <= ty1 + pad; y++) {
|
||
if (x >= tx0 && x <= tx1 && y >= ty0 && y <= ty1) continue; // sichtbar, laeuft schon
|
||
request(z, x, y, false);
|
||
}
|
||
}
|
||
if (z < ZMAX) {
|
||
for (let x = 2 * tx0; x <= 2 * tx1 + 1; x++) {
|
||
for (let y = 2 * ty0; y <= 2 * ty1 + 1; y++) {
|
||
request(z + 1, x, y, false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
function render() {
|
||
renderPending = false;
|
||
resize();
|
||
|
||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||
ctx.fillStyle = '#0e1013';
|
||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||
|
||
const z = activeLevel();
|
||
const span = tileSpan(z);
|
||
const count = tileCount(z);
|
||
|
||
const topLeft = tileAtWorld(z, screenToWorldX(0), screenToWorldY(0));
|
||
const bottomRight = tileAtWorld(z, screenToWorldX(viewW), screenToWorldY(viewH));
|
||
|
||
const tx0 = Math.max(0, topLeft.x);
|
||
const ty0 = Math.max(0, topLeft.y);
|
||
const tx1 = Math.min(count - 1, bottomRight.x);
|
||
const ty1 = Math.min(count - 1, bottomRight.y);
|
||
|
||
stats.gaps = 0;
|
||
for (let x = tx0; x <= tx1; x++) {
|
||
for (let y = ty0; y <= ty1; y++) {
|
||
paintTile(z, x, y);
|
||
}
|
||
}
|
||
|
||
prefetch(z, tx0, ty0, tx1, ty1);
|
||
updateHud(z, span);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// HUD
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const hud = {
|
||
scale: document.getElementById('hud-scale'),
|
||
level: document.getElementById('hud-level'),
|
||
tile: document.getElementById('hud-tile'),
|
||
world: document.getElementById('hud-world'),
|
||
loaded: document.getElementById('hud-loaded'),
|
||
ab: document.getElementById('hud-absent'),
|
||
gaps: document.getElementById('hud-gaps'),
|
||
cache: document.getElementById('hud-cache'),
|
||
};
|
||
|
||
let pointerScreenX = null, pointerScreenY = null;
|
||
|
||
function updateHud(z) {
|
||
hud.scale.textContent = k.toFixed(3) + ' px/wp';
|
||
hud.level.textContent = z;
|
||
hud.loaded.textContent = loadedCount;
|
||
hud.ab.textContent = absent.size;
|
||
hud.gaps.textContent = stats.gaps;
|
||
hud.cache.textContent = cache.size + '/' + CACHE_LIMIT;
|
||
|
||
if (pointerScreenX === null) {
|
||
hud.tile.textContent = '–';
|
||
hud.world.textContent = '–';
|
||
return;
|
||
}
|
||
|
||
const wx = screenToWorldX(pointerScreenX);
|
||
const wy = screenToWorldY(pointerScreenY);
|
||
// Dieselbe Funktion wie im Renderer - Anzeige und Daten koennen nicht abweichen.
|
||
const t = tileAtWorld(z, wx, wy);
|
||
hud.tile.textContent = z + '_' + t.x + '_' + t.y;
|
||
hud.world.textContent = Math.round(wx) + ', ' + Math.round(wy);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Bedienung
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/** Zoomt so, dass der Weltpunkt unter (sx, sy) genau dort stehen bleibt. */
|
||
function zoomAt(sx, sy, factor) {
|
||
const before = clampScale(k);
|
||
const after = clampScale(before * factor);
|
||
if (after === before) return;
|
||
|
||
const wx = screenToWorldX(sx);
|
||
const wy = screenToWorldY(sy);
|
||
|
||
k = after;
|
||
// Kamera so nachziehen, dass (wx, wy) wieder auf (sx, sy) landet.
|
||
camX = wx - (sx - viewW / 2) / k;
|
||
camY = wy - (sy - viewH / 2) / k;
|
||
|
||
clampCamera();
|
||
requestRender();
|
||
}
|
||
|
||
const activePointers = new Map();
|
||
let dragLast = null;
|
||
let pinchStart = null;
|
||
|
||
canvas.addEventListener('pointerdown', (e) => {
|
||
canvas.setPointerCapture(e.pointerId);
|
||
activePointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
||
|
||
if (activePointers.size === 1) {
|
||
dragLast = { x: e.clientX, y: e.clientY };
|
||
canvas.classList.add('dragging');
|
||
} else if (activePointers.size === 2) {
|
||
const [a, b] = [...activePointers.values()];
|
||
pinchStart = { distance: Math.hypot(a.x - b.x, a.y - b.y), k: k };
|
||
dragLast = null;
|
||
}
|
||
});
|
||
|
||
canvas.addEventListener('pointermove', (e) => {
|
||
const rect = canvas.getBoundingClientRect();
|
||
pointerScreenX = e.clientX - rect.left;
|
||
pointerScreenY = e.clientY - rect.top;
|
||
|
||
if (activePointers.has(e.pointerId)) {
|
||
activePointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
||
}
|
||
|
||
if (activePointers.size === 2 && pinchStart) {
|
||
const [a, b] = [...activePointers.values()];
|
||
const distance = Math.hypot(a.x - b.x, a.y - b.y);
|
||
if (distance > 0 && pinchStart.distance > 0) {
|
||
const midX = (a.x + b.x) / 2 - rect.left;
|
||
const midY = (a.y + b.y) / 2 - rect.top;
|
||
const target = clampScale(pinchStart.k * (distance / pinchStart.distance));
|
||
zoomAt(midX, midY, target / k);
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (dragLast && activePointers.size === 1) {
|
||
camX -= (e.clientX - dragLast.x) / k;
|
||
camY -= (e.clientY - dragLast.y) / k;
|
||
dragLast = { x: e.clientX, y: e.clientY };
|
||
clampCamera();
|
||
}
|
||
|
||
requestRender();
|
||
});
|
||
|
||
function endPointer(e) {
|
||
activePointers.delete(e.pointerId);
|
||
if (activePointers.size < 2) pinchStart = null;
|
||
if (activePointers.size === 0) {
|
||
dragLast = null;
|
||
canvas.classList.remove('dragging');
|
||
} else {
|
||
const first = [...activePointers.values()][0];
|
||
dragLast = { x: first.x, y: first.y };
|
||
}
|
||
}
|
||
|
||
canvas.addEventListener('pointerup', endPointer);
|
||
canvas.addEventListener('pointercancel', endPointer);
|
||
canvas.addEventListener('pointerleave', () => {
|
||
pointerScreenX = pointerScreenY = null;
|
||
requestRender();
|
||
});
|
||
|
||
canvas.addEventListener('wheel', (e) => {
|
||
e.preventDefault();
|
||
const rect = canvas.getBoundingClientRect();
|
||
// deltaMode 1 = Zeilen, 2 = Seiten - auf Pixel normieren, sonst zoomt Firefox anders.
|
||
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? viewH : 1;
|
||
const factor = Math.exp(-e.deltaY * unit * 0.0015);
|
||
zoomAt(e.clientX - rect.left, e.clientY - rect.top, factor);
|
||
}, { passive: false });
|
||
|
||
window.addEventListener('keydown', (e) => {
|
||
const step = 80 / k;
|
||
switch (e.key) {
|
||
case 'ArrowLeft': camX -= step; break;
|
||
case 'ArrowRight': camX += step; break;
|
||
case 'ArrowUp': camY -= step; break;
|
||
case 'ArrowDown': camY += step; break;
|
||
case '+': case '=': zoomAt(viewW / 2, viewH / 2, 2); return;
|
||
case '-': case '_': zoomAt(viewW / 2, viewH / 2, 0.5); return;
|
||
case 'r': case 'R': resetView(); return;
|
||
default: return;
|
||
}
|
||
e.preventDefault();
|
||
clampCamera();
|
||
requestRender();
|
||
});
|
||
|
||
/**
|
||
* Einziger Weg, auf Groessenaenderungen zu reagieren - auch auf die allererste.
|
||
*
|
||
* Beim Parsen kann das Canvas noch 0x0 gross sein; wuerde die Startansicht dann schon
|
||
* berechnet, kaeme k = 0 heraus und die Ansicht bliebe dauerhaft leer. Deshalb wird die
|
||
* Startansicht erst gesetzt, sobald echte Masse vorliegen.
|
||
*/
|
||
let viewInitialised = false;
|
||
|
||
function handleViewportChange() {
|
||
resize();
|
||
if (viewW === 0 || viewH === 0) return;
|
||
|
||
if (!viewInitialised) {
|
||
viewInitialised = true;
|
||
resetView();
|
||
// Das allererste Bild direkt zeichnen statt ueber requestAnimationFrame: in einem
|
||
// Hintergrund-Tab wird rAF gedrosselt, dann bliebe die Seite leer und es wuerde
|
||
// auch keine Kachel angefordert (die Anforderungen entstehen beim Zeichnen).
|
||
render();
|
||
return;
|
||
}
|
||
|
||
k = clampScale(k);
|
||
clampCamera();
|
||
requestRender();
|
||
}
|
||
|
||
if (typeof ResizeObserver === 'function') {
|
||
new ResizeObserver(handleViewportChange).observe(canvas);
|
||
}
|
||
window.addEventListener('resize', handleViewportChange);
|
||
|
||
document.getElementById('btn-reset').addEventListener('click', resetView);
|
||
|
||
document.getElementById('btn-png').addEventListener('click', () => {
|
||
// Die Kacheln kommen per fetch von derselben Herkunft, das Canvas ist also nicht
|
||
// "tainted" und laesst sich exportieren.
|
||
canvas.toBlob((blob) => {
|
||
if (!blob) return;
|
||
const url = URL.createObjectURL(blob);
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = 'elysea_' + Math.round(camX) + '_' + Math.round(camY) + '_z' + activeLevel() + '.png';
|
||
a.click();
|
||
setTimeout(() => URL.revokeObjectURL(url), 5000);
|
||
}, 'image/png');
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Start
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Die beiden groebsten Ebenen einmalig komplett laden (16 + 64 Kacheln, fast alle nur
|
||
* wenige hundert Byte). Damit existiert immer eine Grundschicht, auf die die
|
||
* Rueckfallregel ausweichen kann - sichtbare Luecken gibt es dadurch praktisch nie,
|
||
* auch nicht waehrend des Nachladens.
|
||
*/
|
||
function primeCoarseLevels() {
|
||
for (let z = ZMIN; z <= Math.min(ZMIN + 1, ZMAX); z++) {
|
||
const count = tileCount(z);
|
||
for (let x = 0; x < count; x++) {
|
||
for (let y = 0; y < count; y++) request(z, x, y, false);
|
||
}
|
||
}
|
||
}
|
||
|
||
handleViewportChange();
|
||
primeCoarseLevels();
|
||
</script>
|
||
</body>
|
||
</html>
|