LuminaWebsite/assets/map/js/views/MapView.js
Maaxxs c5213b5736 Karte: replace the tactical map with the interactive map viewer
The Karte page now embeds a standalone viewer (assets/map/) covering all ten
regions - Verteron, Altgard, Elthen, Morheim, Poeta, Ishalgen and the
Reshanta maps - with markers and category filters from interactivemap.app.
map.php loads it in an iframe because its stylesheet restyles body, button
and * globally and would otherwise break the site layout.

The tile, marker and icon data (~230 MB, ~39,000 files) is kept out of Git
and uploaded by FTP; the README explains where it comes from.

Removed with it: own markers and categories, tactic layers, the in-browser
map builder, the tile/stitch/viewer tools, the bundled Leaflet, the four map
tables in schema.sql and the map.manage / map.tactics permissions. Existing
databases keep their old map tables untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 06:54:17 +02:00

334 lines
No EOL
11 KiB
JavaScript

/**
* MapView - Handles all canvas rendering for the map viewer
*/
class MapView {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
// View state
this.zoom = CONFIG.MIN_ZOOM + 1;
this.centerWorldX = (CONFIG.WORLD_MIN_X + CONFIG.WORLD_MAX_X) / 2;
this.centerWorldY = (CONFIG.WORLD_MIN_Y + CONFIG.WORLD_MAX_Y) / 2;
// Icon cache
this.iconCache = {};
this.loadingIcons = new Set();
// Interaction state
this.isDragging = false;
this.lastMouseX = 0;
this.lastMouseY = 0;
this.renderRequested = false;
}
/**
* Resize canvas to fill window
*/
resize() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
/**
* Convert world coordinates to screen coordinates
*/
worldToScreen(worldX, worldY) {
const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X;
const zoomScale = Math.pow(2, this.zoom - CONFIG.MIN_ZOOM);
const visibleWidth = worldWidth / zoomScale;
const scale = this.canvas.width / visibleWidth;
const camOffsetX = -this.centerWorldX * scale + this.canvas.width / 2;
const camOffsetY = -this.centerWorldY * scale + this.canvas.height / 2;
return {
x: worldX * scale + camOffsetX,
y: worldY * scale + camOffsetY
};
}
/**
* Convert screen coordinates to world coordinates
*/
screenToWorld(screenX, screenY) {
const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X;
const zoomScale = Math.pow(2, this.zoom - CONFIG.MIN_ZOOM);
const visibleWidth = worldWidth / zoomScale;
const scale = this.canvas.width / visibleWidth;
const camOffsetX = -this.centerWorldX * scale + this.canvas.width / 2;
const camOffsetY = -this.centerWorldY * scale + this.canvas.height / 2;
return {
x: (screenX - camOffsetX) / scale,
y: (screenY - camOffsetY) / scale
};
}
/**
* Get viewport bounds in world coordinates
*/
getViewBounds() {
const topLeft = this.screenToWorld(0, 0);
const bottomRight = this.screenToWorld(this.canvas.width, this.canvas.height);
return {
minX: Math.min(topLeft.x, bottomRight.x),
maxX: Math.max(topLeft.x, bottomRight.x),
minY: Math.min(topLeft.y, bottomRight.y),
maxY: Math.max(topLeft.y, bottomRight.y)
};
}
/**
* Render the map with tiles and markers
*/
async render(tileService, mapData) {
const ctx = this.ctx;
const width = this.canvas.width;
const height = this.canvas.height;
// Clear canvas
ctx.fillStyle = '#0a0a1e';
ctx.fillRect(0, 0, width, height);
if (!mapData.currentMap) return;
const slug = mapData.currentMap.slug;
const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X;
// Calculate scale and camera offset
const zoomScale = Math.pow(2, this.zoom - CONFIG.MIN_ZOOM);
const visibleWidth = worldWidth / zoomScale;
const scale = width / visibleWidth;
const camOffsetX = -this.centerWorldX * scale + width / 2;
const camOffsetY = -this.centerWorldY * scale + height / 2;
// Draw tiles at current zoom level
const tilesPerSide = Math.pow(2, this.zoom);
const tileWorldSize = worldWidth / tilesPerSide;
const tileScreenSize = tileWorldSize * scale;
for (let tx = 0; tx < tilesPerSide; tx++) {
for (let ty = 0; ty < tilesPerSide; ty++) {
// World position of this tile's top-left corner
const tileWorldX = CONFIG.WORLD_MIN_X + tx * tileWorldSize;
const tileWorldY = CONFIG.WORLD_MIN_Y + ty * tileWorldSize;
// Screen position after camera transform
const screenX = tileWorldX * scale + camOffsetX;
const screenY = tileWorldY * scale + camOffsetY;
// Skip tiles completely outside viewport (with margin)
if (screenX + tileScreenSize < -tileScreenSize ||
screenX > width + tileScreenSize ||
screenY + tileScreenSize < -tileScreenSize ||
screenY > height + tileScreenSize) {
continue;
}
const tileImg = await tileService.loadTile(slug, this.zoom, tx, ty);
if (tileImg) {
ctx.drawImage(tileImg, screenX, screenY, tileScreenSize, tileScreenSize);
} else {
// Placeholder for missing/loading tiles
ctx.fillStyle = '#1a1a3e';
ctx.fillRect(screenX, screenY, tileScreenSize, tileScreenSize);
}
}
}
// Draw markers
this.drawMarkers(mapData);
// Preload adjacent zoom levels in background
const viewBounds = this.getViewBounds();
tileService.preloadAdjacentZoomLevels(slug, this.zoom, viewBounds);
}
/**
* Load an icon image from local data directory
*/
async loadIcon(iconPath) {
if (!iconPath) return null;
// Extract filename from path
const filename = iconPath.split('/').pop();
if (this.iconCache[filename]) {
return this.iconCache[filename];
}
if (this.loadingIcons.has(filename)) {
await new Promise(r => setTimeout(r, 10));
return this.iconCache[filename] || null;
}
this.loadingIcons.add(filename);
try {
const img = new Image();
img.src = `${CONFIG.ICONS_DIR}/${filename}`;
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = () => reject(new Error('Load failed'));
setTimeout(() => reject(new Error('Timeout')), 3000);
});
this.iconCache[filename] = img;
} catch (e) {
// Silently fail for missing icons
} finally {
this.loadingIcons.delete(filename);
}
return this.iconCache[filename] || null;
}
/**
* Draw all visible markers using icon images
*/
async drawMarkers(mapData) {
const ctx = this.ctx;
for (const marker of mapData.markers) {
const catKey = `${marker.group_name}/${marker.category_name}`;
if (!mapData.visibleCategories.has(catKey)) continue;
// Marker size based on zoom
let iconSize = 12;
if (this.zoom >= 4) iconSize = 16;
if (this.zoom >= 5) iconSize = 20;
if (this.zoom >= 6) iconSize = 24;
const screen = this.worldToScreen(marker.worldX, marker.worldY);
// Try to load and draw icon image
let iconImg = null;
if (marker.iconImage) {
iconImg = await this.loadIcon(marker.iconImage);
}
if (iconImg) {
// Draw icon image centered at marker position
ctx.drawImage(iconImg,
screen.x - iconSize / 2,
screen.y - iconSize / 2,
iconSize,
iconSize);
} else {
// Fallback to emoji if no icon available
const symbol = CONFIG.CATEGORY_SYMBOLS[marker.category_name] || '📍';
let fontSize = 8;
if (this.zoom >= 5) fontSize = 12;
if (this.zoom >= 6) fontSize = 16;
ctx.font = `${fontSize}px sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(symbol, screen.x, screen.y);
}
}
}
/**
* Zoom in
*/
zoomIn() {
if (this.zoom < CONFIG.MAX_ZOOM) {
this.zoom++;
return true;
}
return false;
}
/**
* Zoom out
*/
zoomOut() {
if (this.zoom > CONFIG.MIN_ZOOM) {
this.zoom--;
return true;
}
return false;
}
/**
* Reset view to default
*/
resetView() {
this.zoom = CONFIG.MIN_ZOOM + 1;
this.centerWorldX = (CONFIG.WORLD_MIN_X + CONFIG.WORLD_MAX_X) / 2;
this.centerWorldY = (CONFIG.WORLD_MIN_Y + CONFIG.WORLD_MAX_Y) / 2;
}
/**
* Pan by screen delta
*/
panBy(dx, dy) {
const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X;
const zoomScale = Math.pow(2, this.zoom - CONFIG.MIN_ZOOM);
const visibleWidth = worldWidth / zoomScale;
const scale = this.canvas.width / visibleWidth;
// Each screen pixel represents 1/scale world units
const worldDx = -(dx / scale);
const worldDy = -(dy / scale);
this.centerWorldX += worldDx;
this.centerWorldY += worldDy;
// Clamp to world bounds
const margin = 0.1;
this.centerWorldX = Math.max(
CONFIG.WORLD_MIN_X + worldWidth * margin,
Math.min(CONFIG.WORLD_MAX_X - worldWidth * margin, this.centerWorldX)
);
this.centerWorldY = Math.max(
CONFIG.WORLD_MIN_Y + (CONFIG.WORLD_MAX_Y - CONFIG.WORLD_MIN_Y) * margin,
Math.min(CONFIG.WORLD_MAX_Y - (CONFIG.WORLD_MAX_Y - CONFIG.WORLD_MIN_Y) * margin, this.centerWorldY)
);
}
/**
* Request a render frame
*/
requestRender(tileService, mapData) {
if (!this.renderRequested) {
this.renderRequested = true;
requestAnimationFrame(() => {
this.render(tileService, mapData);
this.renderRequested = false;
});
}
}
/**
* Find nearest marker to screen position
*/
findNearestMarker(mapData, screenX, screenY, threshold = 20) {
let nearest = null;
let nearestDist = threshold;
for (const marker of mapData.markers) {
const catKey = `${marker.group_name}/${marker.category_name}`;
if (!mapData.visibleCategories.has(catKey)) continue;
const screen = this.worldToScreen(marker.worldX, marker.worldY);
const dx = screenX - screen.x;
const dy = screenY - screen.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < nearestDist) {
nearestDist = dist;
nearest = marker;
}
}
return nearest;
}
}