Files
airdef/tools/web_remote/static/map.js
T

251 lines
9.0 KiB
JavaScript

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}) {
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;
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);
ctx.fillStyle = "#101c24";
ctx.fillRect(0, 0, width, height);
const {x, z, scale} = view;
const px = v => (v - x) * scale + width / 2;
const py = v => (v - z) * scale + height / 2;
const left = x - width / (2 * scale), top = z - height / (2 * scale);
const steps = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
const step = steps.find(s => s * scale >= 48) || steps.at(-1);
ctx.strokeStyle = "#233947";
ctx.lineWidth = 1;
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();
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 r = point.key === selected ? 8 : 5;
ctx.beginPath();
if (kind === "unit") {
ctx.rect(x - r, y - r, 2 * r, 2 * r);
} else if (kind === "contact") {
ctx.arc(x, y, r, 0, Math.PI * 2);
} else {
ctx.moveTo(x, y - r);
ctx.lineTo(x + r, kind === "object" ? y : y + r);
if (kind === "object") ctx.lineTo(x, y + r);
ctx.lineTo(x - r, kind === "object" ? y : y + r);
ctx.closePath();
}
ctx.strokeStyle = ctx.fillStyle = color;
ctx.lineWidth = 2;
if (kind !== "contact") ctx.fill();
if (row.status?.startsWith("Оценка")) ctx.setLineDash([3, 2]);
ctx.stroke();
ctx.setLineDash([]);
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([]);
}
if (Number.isFinite(row.vx) && Number.isFinite(row.vz)) {
const n = Math.hypot(row.vx, row.vz) || 1;
const length = clamp(n * scale / 3, 5, 28);
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + row.vx / n * length, y + row.vz / n * length);
ctx.stroke();
}
}
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 (!points.length) {
ctx.fillStyle = "#9ab0bb";
ctx.textAlign = "center";
ctx.fillText(drawnFrame ? "На выбранных слоях нет объектов" : "Нет снимка карты", 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;
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}`})));
if (autoFit && canvas.clientWidth > 100 && points.length) fit();
schedule();
},
fit,
zoom,
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();
},
};
}