- Map links (/map.php?karte=…&x=…&y=…&zoom=…&ort=…) open the region at that spot with a pin
- "Share location" button on the map: click a spot, optional name, copy the link
- Calendar entries: "Pick location on the map" opens the map in a window, "Use this location" stores the meeting point
- Map links in comments, captions and descriptions render as "📍 Region – Location" and do not count as external links for moderation
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
356 lines
No EOL
12 KiB
JavaScript
356 lines
No EOL
12 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);
|
|
|
|
// Zeichenebene der Taktiken sofort mitziehen, nicht erst nach dem Laden der Kacheln.
|
|
if (this.onViewChange) this.onViewChange();
|
|
// Pin eines geteilten Ortes mitziehen (LocationController)
|
|
if (this.onPinUpdate) this.onPinUpdate();
|
|
|
|
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;
|
|
|
|
const iconSize = this.markerSize();
|
|
|
|
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] || '📍';
|
|
const fontSize = Math.round(iconSize * 0.75);
|
|
|
|
ctx.font = `${fontSize}px sans-serif`;
|
|
ctx.textAlign = 'center';
|
|
ctx.textBaseline = 'middle';
|
|
ctx.fillText(symbol, screen.x, screen.y);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Groesse der Marker-Icons in Pixeln - waechst mit der Zoomstufe, damit man sie auch
|
|
* herausgezoomt noch erkennt.
|
|
*/
|
|
markerSize() {
|
|
const sizes = { 2: 20, 3: 24, 4: 28, 5: 32, 6: 38 };
|
|
return sizes[this.zoom] || (this.zoom < 2 ? 18 : 38);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Eine Stufe rein (+1) oder raus (-1) zoomen, wobei der Weltpunkt unter
|
|
* screenX/screenY an seiner Stelle bleibt (z.B. zwischen zwei Fingern).
|
|
*/
|
|
zoomAt(step, screenX, screenY) {
|
|
const before = this.screenToWorld(screenX, screenY);
|
|
const changed = step > 0 ? this.zoomIn() : this.zoomOut();
|
|
if (!changed) return false;
|
|
const after = this.screenToWorld(screenX, screenY);
|
|
const scale = this.worldToScreen(1, 0).x - this.worldToScreen(0, 0).x;
|
|
this.panBy((after.x - before.x) * scale, (after.y - before.y) * scale);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 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 = this.markerSize() * 0.7) {
|
|
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;
|
|
}
|
|
} |