/** * TacticLayer - Zeichenebene fuer Taktiken ueber der Karte. * * Ein eigenes Canvas ueber dem Karten-Canvas: die Zeichnungen lassen sich so bei jeder * Mausbewegung neu zeichnen, ohne Kacheln und tausende Marker mitzuzeichnen. Alle Punkte * liegen in Weltkoordinaten (0..4096, wie die Marker) und werden hier ueber die Kamera * von MapView in Bildschirmpixel umgerechnet. */ class TacticLayer { constructor(mapView) { this.view = mapView; this.canvas = document.createElement('canvas'); this.canvas.id = 'tactic-canvas'; mapView.canvas.insertAdjacentElement('afterend', this.canvas); this.ctx = this.canvas.getContext('2d'); // Was gezeichnet wird - setzt der TacticController. this.shapes = []; // Zeichnungen des aktuellen Schritts this.ghostShapes = []; // Zeichnungen der vorherigen Schritte (blass) this.draft = null; // Form, die gerade gezeichnet wird this.hoverId = null; // Form unter dem Radierer this.resize(); } resize() { this.canvas.width = this.view.canvas.width; this.canvas.height = this.view.canvas.height; } /** Bildschirmpixel pro Welteinheit bei der aktuellen Zoomstufe. */ scale() { return this.view.worldToScreen(1, 0).x - this.view.worldToScreen(0, 0).x; } toScreen(point) { return this.view.worldToScreen(point[0], point[1]); } draw() { const ctx = this.ctx; ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); ctx.save(); ctx.globalAlpha = 0.3; this.ghostShapes.forEach(shape => this.drawShape(shape)); ctx.restore(); this.shapes.forEach(shape => this.drawShape(shape, shape.id === this.hoverId)); if (this.draft) { this.drawShape(this.draft); } } drawShape(shape, highlighted = false) { const ctx = this.ctx; const pts = shape.points.map(p => this.toScreen(p)); if (pts.length === 0) return; const width = Math.max(1, shape.width || 4); ctx.save(); ctx.strokeStyle = shape.color; ctx.fillStyle = shape.color; ctx.lineWidth = width; ctx.lineCap = 'round'; ctx.lineJoin = 'round'; // Dunkler Schatten, damit helle Farben auch auf heller Karte lesbar bleiben. ctx.shadowColor = 'rgba(0, 0, 0, 0.7)'; ctx.shadowBlur = 3; if (highlighted) { ctx.shadowColor = '#ffffff'; ctx.shadowBlur = 12; } switch (shape.type) { case 'pen': case 'line': this.polyline(pts); ctx.stroke(); break; case 'arrow': this.drawArrow(pts[0], pts[pts.length - 1], width); break; case 'area': this.polyline(pts); ctx.closePath(); ctx.save(); ctx.globalAlpha *= 0.25; ctx.fill(); ctx.restore(); ctx.stroke(); this.drawLabel(shape.label, this.centroid(pts), shape.color, 13); break; case 'circle': { const radius = Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y); ctx.beginPath(); ctx.arc(pts[0].x, pts[0].y, radius, 0, Math.PI * 2); ctx.save(); ctx.globalAlpha *= 0.2; ctx.fill(); ctx.restore(); ctx.stroke(); this.drawLabel(shape.label, pts[0], shape.color, 13); break; } case 'point': { const r = 6 + width / 2; ctx.beginPath(); ctx.arc(pts[0].x, pts[0].y, r, 0, Math.PI * 2); ctx.fill(); ctx.shadowBlur = 0; ctx.lineWidth = 2; ctx.strokeStyle = '#ffffff'; ctx.stroke(); if (shape.label) { this.drawLabel(shape.label, { x: pts[0].x + r + 4, y: pts[0].y }, '#ffffff', 13, 'left'); } break; } case 'text': this.drawLabel(shape.label, pts[0], shape.color, 12 + width * 2); break; } ctx.restore(); } polyline(pts) { const ctx = this.ctx; ctx.beginPath(); ctx.moveTo(pts[0].x, pts[0].y); for (let i = 1; i < pts.length; i++) { ctx.lineTo(pts[i].x, pts[i].y); } } drawArrow(from, to, width) { const ctx = this.ctx; const angle = Math.atan2(to.y - from.y, to.x - from.x); const head = Math.max(12, width * 3.5); const length = Math.hypot(to.x - from.x, to.y - from.y); // Die Linie endet vor der Spitze, sonst schaut sie bei dicken Strichen vorne heraus. const shaft = Math.max(0, length - head * 0.8); ctx.beginPath(); ctx.moveTo(from.x, from.y); ctx.lineTo(from.x + Math.cos(angle) * shaft, from.y + Math.sin(angle) * shaft); ctx.stroke(); ctx.beginPath(); ctx.moveTo(to.x, to.y); ctx.lineTo(to.x - head * Math.cos(angle - Math.PI / 7), to.y - head * Math.sin(angle - Math.PI / 7)); ctx.lineTo(to.x - head * Math.cos(angle + Math.PI / 7), to.y - head * Math.sin(angle + Math.PI / 7)); ctx.closePath(); ctx.fill(); } drawLabel(text, at, color, size, align = 'center') { if (!text) return; const ctx = this.ctx; ctx.save(); ctx.shadowBlur = 0; ctx.font = `bold ${size}px 'Segoe UI', sans-serif`; ctx.textAlign = align; ctx.textBaseline = 'middle'; ctx.lineWidth = Math.max(3, size / 4); ctx.strokeStyle = 'rgba(0, 0, 0, 0.85)'; ctx.lineJoin = 'round'; ctx.strokeText(text, at.x, at.y); ctx.fillStyle = color; ctx.fillText(text, at.x, at.y); ctx.restore(); } centroid(pts) { const sum = pts.reduce((acc, p) => ({ x: acc.x + p.x, y: acc.y + p.y }), { x: 0, y: 0 }); return { x: sum.x / pts.length, y: sum.y / pts.length }; } /** * Die oberste Form des aktuellen Schritts an einer Bildschirmposition (fuer den * Radierer), oder null. */ hitTest(screenX, screenY) { for (let i = this.shapes.length - 1; i >= 0; i--) { if (this.hitsShape(this.shapes[i], screenX, screenY)) { return this.shapes[i]; } } return null; } hitsShape(shape, x, y) { const pts = shape.points.map(p => this.toScreen(p)); const tolerance = 8 + (shape.width || 4) / 2; switch (shape.type) { case 'pen': case 'line': case 'arrow': return this.distanceToPolyline(pts, x, y) <= tolerance; case 'area': return this.insidePolygon(pts, x, y) || this.distanceToPolyline([...pts, pts[0]], x, y) <= tolerance; case 'circle': return Math.hypot(x - pts[0].x, y - pts[0].y) <= Math.hypot(pts[1].x - pts[0].x, pts[1].y - pts[0].y) + tolerance; case 'point': return Math.hypot(x - pts[0].x, y - pts[0].y) <= 6 + (shape.width || 4) / 2 + tolerance; case 'text': { const size = 12 + (shape.width || 4) * 2; this.ctx.font = `bold ${size}px 'Segoe UI', sans-serif`; const half = this.ctx.measureText(shape.label || '').width / 2; return Math.abs(x - pts[0].x) <= half + 4 && Math.abs(y - pts[0].y) <= size / 2 + 4; } } return false; } distanceToPolyline(pts, x, y) { let best = Infinity; for (let i = 0; i < pts.length - 1; i++) { best = Math.min(best, TacticLayer.distanceToSegment(pts[i], pts[i + 1], x, y)); } return pts.length === 1 ? Math.hypot(x - pts[0].x, y - pts[0].y) : best; } static distanceToSegment(a, b, x, y) { const dx = b.x - a.x; const dy = b.y - a.y; const lengthSq = dx * dx + dy * dy; const t = lengthSq === 0 ? 0 : Math.max(0, Math.min(1, ((x - a.x) * dx + (y - a.y) * dy) / lengthSq)); return Math.hypot(x - (a.x + t * dx), y - (a.y + t * dy)); } insidePolygon(pts, x, y) { let inside = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { if ((pts[i].y > y) !== (pts[j].y > y) && x < (pts[j].x - pts[i].x) * (y - pts[i].y) / (pts[j].y - pts[i].y) + pts[i].x) { inside = !inside; } } return inside; } /** * Vereinfacht eine Freihandlinie (Ramer-Douglas-Peucker), damit nicht jeder * Mausschritt als eigener Punkt gespeichert wird. tolerance in Welteinheiten. */ static simplify(points, tolerance) { if (points.length <= 2) return points; const toXY = p => ({ x: p[0], y: p[1] }); let maxDist = 0; let index = 0; const first = toXY(points[0]); const last = toXY(points[points.length - 1]); for (let i = 1; i < points.length - 1; i++) { const p = toXY(points[i]); const dist = TacticLayer.distanceToSegment(first, last, p.x, p.y); if (dist > maxDist) { maxDist = dist; index = i; } } if (maxDist <= tolerance) { return [points[0], points[points.length - 1]]; } const left = TacticLayer.simplify(points.slice(0, index + 1), tolerance); const right = TacticLayer.simplify(points.slice(index), tolerance); return left.slice(0, -1).concat(right); } }