import {drawSymbol} from "./symbols.js"; import {createTerrain} from "./terrain.js"; const styles = [ ["units", "unit", "#6adcd2"], ["objects", "object", "#a9baf6"], ["contacts", "contact", "#f0bf6a"], ["interceptors", "interceptor", "#b2e68f"], ]; const clamp = (x, low, high) => Math.min(high, Math.max(low, x)); export function createMap(canvas, {onSelect, onView, onTerrain}) { const ctx = canvas.getContext("2d"); const view = {x: 0, z: 0, scale: .22}; const fingers = new Map(); let points = [], visible = [], selected, width = 1, height = 1, drawnFrame; const terrain = createTerrain(schedule); let mode = "map", scope, online, radarId; let moved = false, autoFit = true, queued = false; function paint() { queued = false; const ratio = Math.min(2, window.devicePixelRatio || 1); width = Math.max(1, canvas.clientWidth); height = Math.max(1, canvas.clientHeight); const w = Math.round(width * ratio), h = Math.round(height * ratio); if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h; } ctx.setTransform(ratio, 0, 0, ratio, 0, 0); const {x, z, scale} = view; const px = v => (v - x) * scale + width / 2; const py = v => (v - z) * scale + height / 2; const steps = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000]; const step = steps.find(s => s * scale >= 54) || steps.at(-1); ctx.fillStyle = mode === "radar" ? "#071718" : "#101c27"; ctx.fillRect(0, 0, width, height); ctx.lineWidth = 1; if (mode === "radar") { const station = drawnFrame?.units?.find(unit => String(unit.id) === radarId && unit.radarRange > 0); const cx = station ? px(station.x) : width / 2; const cy = station ? py(station.z) : height / 2; const spacing = step * scale; const min = Math.max(1, Math.floor(Math.hypot(Math.max(-cx, cx - width, 0), Math.max(-cy, cy - height, 0)) / spacing)); const max = Math.ceil(Math.hypot(Math.max(Math.abs(cx), Math.abs(cx - width)), Math.max(Math.abs(cy), Math.abs(cy - height))) / spacing); ctx.strokeStyle = "#345c54"; ctx.fillStyle = "#8ab5aa"; ctx.font = "11px system-ui, sans-serif"; for (let ring = min; ring <= max; ring++) { const radius = ring * spacing; ctx.beginPath(); ctx.arc(cx, cy, radius, 0, Math.PI * 2); ctx.stroke(); ctx.fillText(`${ring * step}`, cx + 5, cy - radius + 14); } ctx.beginPath(); ctx.moveTo(cx, 0); ctx.lineTo(cx, height); ctx.moveTo(0, cy); ctx.lineTo(width, cy); ctx.stroke(); if (station) { ctx.beginPath(); ctx.arc(cx, cy, station.radarRange * scale, 0, Math.PI * 2); ctx.strokeStyle = "#73dbc5"; ctx.setLineDash([6, 5]); ctx.stroke(); ctx.setLineDash([]); } onTerrain(station ? `${station.label} · номинальная дальность ${Math.round(station.radarRange)} бл.` : "Общая картина сети"); } else { ctx.imageSmoothingEnabled = false; const loaded = terrain.paint(ctx, view, width, height, scope, online); ctx.imageSmoothingEnabled = true; const left = x - width / (2 * scale), top = z - height / (2 * scale); ctx.strokeStyle = loaded ? "#d9eff429" : "#253745"; ctx.beginPath(); for (let a = Math.ceil(left / step) * step; px(a) < width; a += step) { const line = Math.round(px(a)) + .5; ctx.moveTo(line, 0); ctx.lineTo(line, height); } for (let a = Math.ceil(top / step) * step; py(a) < height; a += step) { const line = Math.round(py(a)) + .5; ctx.moveTo(0, line); ctx.lineTo(width, line); } ctx.stroke(); onTerrain(online ? loaded ? "Рельеф загруженных чанков" : "Ожидание данных местности" : loaded ? "Последний загруженный рельеф" : "Рельеф доступен в открытом мире"); } ctx.font = "12px system-ui, sans-serif"; ctx.fillStyle = "#b6c8d0"; ctx.fillText("↑ N", width - 36, 20); ctx.fillText(`X ${Math.round(x)} · Z ${Math.round(z)}`, 14, 20); const bar = steps.find(s => s * scale >= 40) || steps.at(-1); ctx.fillText(`${bar} бл.`, 14, height - 23); ctx.fillRect(14, height - 16, bar * scale, 2); visible = []; for (const point of points) { const {row, color, kind} = point; const x = px(row.x), y = py(row.z); if (x < -40 || y < -40 || x > width + 40 || y > height + 40) continue; visible.push({...point, x, y}); const heading = Math.hypot(row.vx, row.vz) > 0 ? Math.atan2(row.vx, -row.vz) : 0; const symbol = row.symbol || (kind === "contact" ? "unknown" : kind); drawSymbol(ctx, x, y, { kind, symbol, color, selected: point.key === selected, uncertain: symbol === "unknown", heading, }); if (point.key === selected && row.radius > 0) { ctx.beginPath(); ctx.arc(x, y, Math.min(380, row.radius * scale), 0, Math.PI * 2); ctx.setLineDash([5, 5]); ctx.strokeStyle = "#798f98"; ctx.stroke(); ctx.setLineDash([]); } } const focus = visible.find(item => item.key === selected); if (focus) { const links = [ focus.row.target != null ? `contact:${focus.row.target}` : null, focus.row.source != null ? `unit:${focus.row.source}` : null, ]; for (const key of links) { const other = visible.find(item => item.key === key); if (!other || other.key === focus.key) continue; ctx.strokeStyle = "#90bdc0"; ctx.setLineDash([5, 5]); ctx.beginPath(); ctx.moveTo(focus.x, focus.y); ctx.lineTo(other.x, other.y); ctx.stroke(); ctx.setLineDash([]); } ctx.font = "bold 13px system-ui, sans-serif"; const label = focus.row.label || focus.key; const length = Math.min(width - 22, ctx.measureText(label).width + 12); const left = clamp(focus.x + 12, 8, Math.max(8, width - length - 8)); const top = clamp(focus.y - 20, 6, Math.max(6, height - 27)); ctx.fillStyle = "#081017"; ctx.fillRect(left, top, length, 23); ctx.fillStyle = "#eaf7fa"; ctx.fillText(label, left + 6, top + 16, length - 12); } if (!drawnFrame) { ctx.fillStyle = "#9ab0bb"; ctx.textAlign = "center"; ctx.fillText("Нет снимка карты", width / 2, height / 2); ctx.textAlign = "left"; } } function schedule() { if (!queued) { queued = true; requestAnimationFrame(paint); } } function fit() { if (!points.length) return; let minX = Infinity, maxX = -Infinity, minZ = Infinity, maxZ = -Infinity; for (const {row} of points) { minX = Math.min(minX, row.x); maxX = Math.max(maxX, row.x); minZ = Math.min(minZ, row.z); maxZ = Math.max(maxZ, row.z); } view.x = (maxX + minX) / 2; view.z = (maxZ + minZ) / 2; view.scale = clamp(Math.min((canvas.clientWidth - 64) / Math.max(80, maxX - minX), (canvas.clientHeight - 64) / Math.max(80, maxZ - minZ)), .006, 5); autoFit = false; onView({...view}); schedule(); } function zoom(factor, x = width / 2, y = height / 2) { const before = view.scale; view.scale = clamp(before * factor, .006, 12); view.x += (x - width / 2) * (1 / before - 1 / view.scale); view.z += (y - height / 2) * (1 / before - 1 / view.scale); onView({...view}); schedule(); } function coords(event) { const rect = canvas.getBoundingClientRect(); return [event.clientX - rect.left, event.clientY - rect.top]; } canvas.addEventListener("wheel", event => { event.preventDefault(); zoom(Math.exp(clamp(-event.deltaY * .002, -.4, .4)), ...coords(event)); }, {passive: false}); canvas.addEventListener("pointerdown", event => { if (fingers.size === 0) moved = false; if (fingers.size > 0) moved = true; canvas.setPointerCapture(event.pointerId); const at = coords(event); fingers.set(event.pointerId, {at, start: at}); }); canvas.addEventListener("pointermove", event => { const finger = fingers.get(event.pointerId); if (!finger) return; const at = coords(event), before = finger.at; finger.at = at; const dx = at[0] - before[0], dy = at[1] - before[1]; if (fingers.size === 1) { if (Math.hypot(at[0] - finger.start[0], at[1] - finger.start[1]) > 5) moved = true; if (!moved) return; view.x -= dx / view.scale; view.z -= dy / view.scale; } else { const other = [...fingers.values()].find(value => value !== finger).at; const ratio = Math.hypot(at[0] - other[0], at[1] - other[1]) / Math.max(2, Math.hypot(before[0] - other[0], before[1] - other[1])); zoom(ratio, (at[0] + other[0]) / 2, (at[1] + other[1]) / 2); view.x -= dx / (2 * view.scale); view.z -= dy / (2 * view.scale); } onView({...view}); schedule(); }); canvas.addEventListener("pointerup", event => { fingers.delete(event.pointerId); if (moved) return; const [x, y] = coords(event); let hit, distance = 21; for (const p of visible) { const d = Math.hypot(x - p.x, y - p.y); if (d <= distance) [hit, distance] = [p, d]; } if (hit) onSelect(hit.key); }); canvas.addEventListener("pointercancel", event => fingers.delete(event.pointerId)); new ResizeObserver(schedule).observe(canvas); return { draw(frame, options) { drawnFrame = frame; selected = options.selected; mode = options.mode; scope = options.scope; online = options.online; radarId = options.radarId; points = styles.flatMap(([group, kind, color]) => options.layers[group] === false ? [] : (frame?.[group] || []) .filter(row => Number.isFinite(row.x) && Number.isFinite(row.z)) .map(row => ({row, color, kind, key: `${kind}:${row.id}`}))); const index = points.findIndex(point => point.key === selected); if (index >= 0) points.push(points.splice(index, 1)[0]); if (autoFit && canvas.clientWidth > 100) { if (points.length) { fit(); } else if (options.center?.every(Number.isFinite)) { view.x = options.center[0]; view.z = options.center[1]; view.scale = 1.5; autoFit = false; onView({...view}); } } schedule(); }, fit, zoom, clearTerrain: terrain.clear, centerAt(x, z, radius) { view.x = x; view.z = z; if (radius > 0) view.scale = clamp((Math.min(canvas.clientWidth, canvas.clientHeight) - 48) / (radius * 2), .006, 12); autoFit = false; onView({...view}); schedule(); }, restore(saved) { if (saved && [saved.x, saved.z, saved.scale].every(Number.isFinite)) { Object.assign(view, saved, {scale: clamp(saved.scale, .006, 12)}); autoFit = false; } else { autoFit = true; } schedule(); }, focus(id) { const item = points.find(point => point.key === id); if (!item) return; view.x = item.row.x; view.z = item.row.z; onView({...view}); schedule(); }, }; }