646 lines
22 KiB
JavaScript
646 lines
22 KiB
JavaScript
const canvas = document.getElementById("stage");
|
|
const ctx = canvas.getContext("2d");
|
|
const statusEl = document.getElementById("status");
|
|
const originEl = document.getElementById("origin");
|
|
const nameEl = document.getElementById("stage-name");
|
|
const patchEl = document.getElementById("patch");
|
|
const listEl = document.getElementById("fixtures");
|
|
const detailEl = document.getElementById("detail");
|
|
const detailTitle = document.getElementById("detail-title");
|
|
const detailMeta = document.getElementById("detail-meta");
|
|
const channelsEl = document.getElementById("channels");
|
|
const stageFileEl = document.getElementById("stage-file");
|
|
|
|
let stage = null;
|
|
let state = { fixtures: {}, age: null, rate: 0, universe: null, packets: 0 };
|
|
let selected = null;
|
|
let drag = null;
|
|
const rows = new Map();
|
|
|
|
function luma(r, g, b) {
|
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
}
|
|
|
|
function hsv(hue, value) {
|
|
const h = ((hue % 1) + 1) % 1;
|
|
const i = Math.floor(h * 6);
|
|
const f = h * 6 - i;
|
|
const q = value * (1 - f);
|
|
const t = value * f;
|
|
const map = [
|
|
[value, t, 0],
|
|
[q, value, 0],
|
|
[0, value, t],
|
|
[0, q, value],
|
|
[t, 0, value],
|
|
[value, 0, q],
|
|
];
|
|
const [r, g, b] = map[i % 6];
|
|
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
|
|
}
|
|
|
|
const PAR_PALETTE = [
|
|
[255, 0, 0],
|
|
[255, 96, 0],
|
|
[255, 200, 0],
|
|
[255, 255, 0],
|
|
[0, 255, 0],
|
|
[0, 255, 160],
|
|
[0, 64, 255],
|
|
[140, 0, 255],
|
|
[255, 0, 160],
|
|
[255, 255, 255],
|
|
];
|
|
|
|
const aims = new Map();
|
|
|
|
function scaleColour(colour, dimmer) {
|
|
const level = (dimmer || 0) / 255;
|
|
return colour.map((channel) => Math.round(channel * level));
|
|
}
|
|
|
|
function paletteColour(live, gradient) {
|
|
const palette = live.palette && live.palette.length ? live.palette : PAR_PALETTE;
|
|
const speed = live.speed_hz || 0.35;
|
|
const phase = performance.now() / 1000 * speed;
|
|
const index = Math.floor(phase) % palette.length;
|
|
if (!gradient) return palette[index];
|
|
const next = palette[(index + 1) % palette.length];
|
|
const blend = phase - Math.floor(phase);
|
|
return palette[index].map((channel, slot) => Math.round(channel + (next[slot] - channel) * blend));
|
|
}
|
|
|
|
function canColour(live) {
|
|
const mode = live.function || "mix";
|
|
const dimmer = live.dimmer || 0;
|
|
if (mode === "jump" || mode === "gradient") return scaleColour(paletteColour(live, mode === "gradient"), dimmer);
|
|
if (mode === "pulse") {
|
|
const speed = live.speed_hz || 0.4;
|
|
const wave = 0.5 - 0.5 * Math.cos((performance.now() / 1000) * speed * Math.PI * 2);
|
|
const depth = 0.05 + 0.95 * wave;
|
|
return [live.r || 0, live.g || 0, live.b || 0].map((channel) => Math.round(channel * depth));
|
|
}
|
|
if (mode === "sound") {
|
|
const speed = live.speed_hz || 1;
|
|
const variant = live.sound === 2 ? 2 : 1;
|
|
const bpm = (variant === 2 ? 156 : 104) * (0.55 + Math.min(speed, 8) / 10);
|
|
const beat = ((performance.now() / 1000) * bpm / 60) % 1;
|
|
let hit = beat < 0.08 ? 1 : 0.04;
|
|
if (variant === 2 && beat > 0.5 && beat < 0.58) hit = 1;
|
|
const colour = (live.palette && live.palette[variant === 2 ? 4 : 0]) || PAR_PALETTE[variant === 2 ? 4 : 0];
|
|
return scaleColour(colour, dimmer).map((channel) => Math.round(channel * hit));
|
|
}
|
|
return [live.r || 0, live.g || 0, live.b || 0];
|
|
}
|
|
|
|
function effectPixels(live) {
|
|
const source = (live.pixels || []).map((pixel) => pixel.slice());
|
|
if (!live.dynamic || !source.length) return source;
|
|
const count = source.length;
|
|
const lit = source.find((pixel) => pixel[0] + pixel[1] + pixel[2] > 0);
|
|
const fallback = lit || scaleColour([255, 255, 255], live.dimmer || 0);
|
|
const speed = live.speed_hz || 1;
|
|
const t = (performance.now() / 1000) * speed;
|
|
return source.map((pixel, index) => {
|
|
const colour = pixel[0] + pixel[1] + pixel[2] > 0 ? pixel : fallback;
|
|
if (live.dynamic === "rainbow") {
|
|
return hsv((t * 0.2 + index / count) % 1, (live.dimmer || 0) / 255);
|
|
}
|
|
let level = 1;
|
|
if (live.dynamic === "chase") level = Math.floor(t) % count === index ? 1 : 0.08;
|
|
else if (live.dynamic === "reverse") level = Math.floor(t) % count === (count - 1 - index) ? 1 : 0.08;
|
|
else if (live.dynamic === "bounce") {
|
|
const span = Math.max(1, count - 1);
|
|
const tri = Math.abs((t % (2 * span)) - span);
|
|
level = Math.round(tri) === index ? 1 : 0.08;
|
|
} else if (live.dynamic === "pulse") level = 0.15 + 0.85 * (0.5 + 0.5 * Math.sin(t * Math.PI * 2));
|
|
else if (live.dynamic === "pairs") level = Math.floor(t) % 2 === index % 2 ? 1 : 0.08;
|
|
return colour.map((channel) => Math.round(channel * level));
|
|
});
|
|
}
|
|
|
|
function mixPixels(pixels) {
|
|
let red = 0;
|
|
let green = 0;
|
|
let blue = 0;
|
|
let weight = 0;
|
|
for (const pixel of pixels) {
|
|
const w = pixel[0] + pixel[1] + pixel[2];
|
|
red += pixel[0] * w;
|
|
green += pixel[1] * w;
|
|
blue += pixel[2] * w;
|
|
weight += w;
|
|
}
|
|
if (weight <= 0) return [0, 0, 0];
|
|
return [Math.round(red / weight), Math.round(green / weight), Math.round(blue / weight)];
|
|
}
|
|
|
|
function shutter(live) {
|
|
if (!live.strobe_hz) return 1;
|
|
const t = performance.now() / 1000;
|
|
if (live.strobe_mode === "random") {
|
|
const bucket = Math.floor(t * live.strobe_hz);
|
|
const noise = Math.abs(Math.sin(bucket * 12.9898) * 43758.5453) % 1;
|
|
return noise > 0.55 ? 1 : 0;
|
|
}
|
|
const period = 1 / live.strobe_hz;
|
|
return t % period < period * 0.42 ? 1 : 0;
|
|
}
|
|
|
|
function aimed(id, live, zero) {
|
|
const targetPan = (zero || 0) + (live.pan_deg || 0);
|
|
const targetBeam = live.beam_m || 0;
|
|
const now = performance.now() / 1000;
|
|
let state = aims.get(id);
|
|
if (!state) {
|
|
state = { pan: targetPan, beam: targetBeam, t: now };
|
|
aims.set(id, state);
|
|
return state;
|
|
}
|
|
const dt = Math.min(0.05, Math.max(0, now - state.t));
|
|
state.t = now;
|
|
const fast = live.move == null ? 1 : live.move;
|
|
if (fast > 0.97) {
|
|
state.pan = targetPan;
|
|
state.beam = targetBeam;
|
|
return state;
|
|
}
|
|
const tau = 0.05 + (1 - fast) * 1.8;
|
|
const k = 1 - Math.exp(-dt / tau);
|
|
state.pan += (targetPan - state.pan) * k;
|
|
state.beam += (targetBeam - state.beam) * k;
|
|
return state;
|
|
}
|
|
|
|
function appearance(fixture, live) {
|
|
const pixels = fixture.kind === "mover" ? effectPixels(live) : null;
|
|
let [r, g, b] = fixture.kind === "can" ? canColour(live) : pixels && live.dynamic ? mixPixels(pixels) : [live.r || 0, live.g || 0, live.b || 0];
|
|
const gate = shutter(live);
|
|
return {
|
|
r: Math.round(r * gate),
|
|
g: Math.round(g * gate),
|
|
b: Math.round(b * gate),
|
|
dimmer: live.dimmer || 0,
|
|
pixels: pixels && pixels.map((pixel) => pixel.map((channel) => Math.round(channel * gate))),
|
|
};
|
|
}
|
|
|
|
function layout(width, height) {
|
|
const ys = stage.fixtures.map((fixture) => fixture.y);
|
|
const backY = Math.min(0, ...ys);
|
|
const depth = Math.max(2.4, ...ys);
|
|
const padX = 0.85;
|
|
const padY = 0.7;
|
|
const x0 = -stage.building_width / 2 - padX;
|
|
const x1 = stage.building_width / 2 + padX;
|
|
const y0 = backY - padY;
|
|
const y1 = depth + padY;
|
|
const worldW = x1 - x0;
|
|
const worldH = y1 - y0;
|
|
const scale = Math.min(width / worldW, height / worldH);
|
|
const ox = (width - worldW * scale) / 2;
|
|
const oy = (height - worldH * scale) / 2;
|
|
return {
|
|
depth,
|
|
backY,
|
|
sx: (x) => ox + (x - x0) * scale,
|
|
sy: (y) => oy + (y1 - y) * scale,
|
|
mx: (px) => x0 + (px - ox) / scale,
|
|
my: (py) => y1 - (py - oy) / scale,
|
|
};
|
|
}
|
|
|
|
function draw() {
|
|
if (!stage) return;
|
|
const dpr = window.devicePixelRatio || 1;
|
|
const width = canvas.width;
|
|
const height = canvas.height;
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
const cssW = width / dpr;
|
|
const cssH = height / dpr;
|
|
const view = layout(cssW, cssH);
|
|
const { sx, sy, depth, backY } = view;
|
|
|
|
ctx.clearRect(0, 0, cssW, cssH);
|
|
ctx.fillStyle = "#121216";
|
|
ctx.fillRect(0, 0, cssW, cssH);
|
|
|
|
const left = sx(-stage.building_width / 2);
|
|
const right = sx(stage.building_width / 2);
|
|
const front = sy(depth);
|
|
const back = sy(backY);
|
|
ctx.strokeStyle = "#5c5c66";
|
|
ctx.lineWidth = 2;
|
|
ctx.strokeRect(left, front, right - left, back - front);
|
|
|
|
ctx.strokeStyle = "#3a3a44";
|
|
ctx.lineWidth = 1;
|
|
ctx.setLineDash([4, 6]);
|
|
ctx.beginPath();
|
|
ctx.moveTo(sx(0), sy(backY - 0.2));
|
|
ctx.lineTo(sx(0), sy(depth + 0.25));
|
|
ctx.stroke();
|
|
ctx.setLineDash([]);
|
|
|
|
ctx.fillStyle = "#9a9aa4";
|
|
ctx.font = "600 13px system-ui, sans-serif";
|
|
ctx.textAlign = "center";
|
|
ctx.fillText("AUDIENCE", sx(0), sy(depth + 0.38));
|
|
ctx.font = "12px system-ui, sans-serif";
|
|
ctx.fillStyle = "#8d8d96";
|
|
ctx.fillText("row of 4", sx(0), sy(-0.28));
|
|
ctx.font = "11px system-ui, sans-serif";
|
|
ctx.fillText(`${stage.building_width.toFixed(1)} m`, sx(0), front - 8);
|
|
|
|
ctx.globalCompositeOperation = "lighter";
|
|
for (const fixture of stage.fixtures) {
|
|
const live = state.fixtures[fixture.id] || {};
|
|
const color = appearance(fixture, live);
|
|
const dragging = drag && drag.moved && drag.id === fixture.id;
|
|
const beamColor = dragging && color.r + color.g + color.b < 8 ? { ...color, r: 190, g: 190, b: 200 } : color;
|
|
if (fixture.kind === "mover") {
|
|
const fixed = dragging
|
|
? { pan: (fixture.zero || 0) + (live.pan_deg || 0), beam: Math.max(live.beam_m || 0, 1.6), zoom: live.zoom_deg || 18, focus: 1 - (live.focus || 0) }
|
|
: null;
|
|
drawBeam(sx, sy, fixture, live, beamColor, fixed);
|
|
} else {
|
|
drawBeam(sx, sy, fixture, live, beamColor, { pan: fixture.direction || 0, beam: 2.2, zoom: 25, focus: 0.35 });
|
|
}
|
|
drawGlow(sx, sy, fixture, color);
|
|
}
|
|
ctx.globalCompositeOperation = "source-over";
|
|
|
|
for (const fixture of stage.fixtures) {
|
|
const live = state.fixtures[fixture.id] || {};
|
|
drawBody(sx, sy, fixture, live, appearance(fixture, live));
|
|
}
|
|
}
|
|
|
|
function drawGlow(sx, sy, fixture, color) {
|
|
const level = (color.r + color.g + color.b) / (255 * 3);
|
|
if (level <= 0.01) return;
|
|
const x = sx(fixture.x);
|
|
const y = sy(fixture.y);
|
|
const radius = (fixture.kind === "mover" ? 70 : 54) * (0.45 + level);
|
|
const gradient = ctx.createRadialGradient(x, y, 2, x, y, radius);
|
|
gradient.addColorStop(0, `rgba(${color.r},${color.g},${color.b},${0.55 * level + 0.15})`);
|
|
gradient.addColorStop(1, `rgba(${color.r},${color.g},${color.b},0)`);
|
|
ctx.fillStyle = gradient;
|
|
ctx.beginPath();
|
|
ctx.arc(x, y, radius, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
}
|
|
|
|
function drawBeam(sx, sy, fixture, live, color, fixed) {
|
|
const level = (color.r + color.g + color.b) / 255;
|
|
const length = fixed ? fixed.beam : Math.max(live.beam_m || 0, fixture.kind === "mover" ? 2.2 : 0);
|
|
if (live.patched === false || level <= 0.02 || !length) return;
|
|
const aim = fixed || aimed(fixture.id, live, fixture.zero);
|
|
// DMX focus 0 is a wide wash. Focus 255 holds the zoom angle.
|
|
// Fixed beams pass spread directly, 0 tight and 1 wide.
|
|
const spread = fixed ? fixed.focus : 1 - (live.focus || 0);
|
|
const zoom = fixed ? fixed.zoom : live.zoom_deg || 8;
|
|
const tight = zoom / 2;
|
|
const wide = Math.max(tight, 40);
|
|
const rad = aim.pan * Math.PI / 180;
|
|
const half = (tight + (wide - tight) * spread) * Math.PI / 180;
|
|
const tip = [fixture.x + Math.sin(rad) * length, fixture.y + Math.cos(rad) * length];
|
|
const left = [fixture.x + Math.sin(rad - half) * length, fixture.y + Math.cos(rad - half) * length];
|
|
const right = [fixture.x + Math.sin(rad + half) * length, fixture.y + Math.cos(rad + half) * length];
|
|
ctx.beginPath();
|
|
ctx.moveTo(sx(fixture.x), sy(fixture.y));
|
|
ctx.lineTo(sx(left[0]), sy(left[1]));
|
|
ctx.lineTo(sx(right[0]), sy(right[1]));
|
|
ctx.closePath();
|
|
const gradient = ctx.createLinearGradient(sx(fixture.x), sy(fixture.y), sx(tip[0]), sy(tip[1]));
|
|
const alpha = 0.85 * (1 - spread * 0.35);
|
|
gradient.addColorStop(0, `rgba(${color.r},${color.g},${color.b},${alpha})`);
|
|
gradient.addColorStop(1, `rgba(${color.r},${color.g},${color.b},0)`);
|
|
ctx.fillStyle = gradient;
|
|
ctx.fill();
|
|
}
|
|
|
|
function lensRotation(live) {
|
|
if (live.spin_dps) return ((performance.now() / 1000) * live.spin_dps * Math.PI) / 180;
|
|
return ((live.lens_deg || 0) * Math.PI) / 180;
|
|
}
|
|
|
|
function drawBody(sx, sy, fixture, live, color) {
|
|
const x = sx(fixture.x);
|
|
const y = sy(fixture.y);
|
|
const pixels = color.pixels || [];
|
|
const head = fixture.kind === "mover" && pixels.length ? pixels[0] : [color.r, color.g, color.b];
|
|
const lit = head[0] + head[1] + head[2] > 8;
|
|
const size = fixture.kind === "mover" ? 15 : 13;
|
|
ctx.beginPath();
|
|
if (fixture.kind === "mover") {
|
|
ctx.rect(x - size, y - size, size * 2, size * 2);
|
|
} else {
|
|
ctx.arc(x, y, size, 0, Math.PI * 2);
|
|
}
|
|
ctx.fillStyle = lit ? `rgb(${head[0]},${head[1]},${head[2]})` : "#2a2a30";
|
|
ctx.fill();
|
|
ctx.lineWidth = fixture.id === selected ? 2.5 : 1.5;
|
|
ctx.strokeStyle = fixture.id === selected ? "#fff" : "#c8c8d0";
|
|
ctx.stroke();
|
|
|
|
if (fixture.kind === "mover" && pixels.length > 1) {
|
|
const ring = pixels.slice(1);
|
|
const spin = lensRotation(live);
|
|
ring.forEach((pixel, index) => {
|
|
const angle = spin + (index / ring.length) * Math.PI * 2 - Math.PI / 2;
|
|
const px = x + Math.cos(angle) * (size + 7);
|
|
const py = y + Math.sin(angle) * (size + 7);
|
|
ctx.beginPath();
|
|
ctx.arc(px, py, 3.5, 0, Math.PI * 2);
|
|
const on = pixel[0] + pixel[1] + pixel[2] > 0;
|
|
ctx.fillStyle = on ? `rgb(${pixel[0]},${pixel[1]},${pixel[2]})` : "#333";
|
|
ctx.fill();
|
|
});
|
|
}
|
|
|
|
ctx.fillStyle = lit && luma(head[0], head[1], head[2]) > 140 ? "#16161a" : "#f2f2f4";
|
|
ctx.font = "600 11px system-ui, sans-serif";
|
|
ctx.textAlign = "center";
|
|
ctx.textBaseline = "middle";
|
|
ctx.fillText(fixture.label, x, y);
|
|
ctx.textBaseline = "alphabetic";
|
|
}
|
|
|
|
function resize() {
|
|
const rect = canvas.getBoundingClientRect();
|
|
const dpr = window.devicePixelRatio || 1;
|
|
canvas.width = Math.max(1, Math.floor(rect.width * dpr));
|
|
canvas.height = Math.max(1, Math.floor(rect.height * dpr));
|
|
draw();
|
|
}
|
|
|
|
function swatchColor(fixture, live) {
|
|
const color = appearance(fixture, live || {});
|
|
const head = fixture.kind === "mover" && color.pixels && color.pixels[0] ? color.pixels[0] : [color.r, color.g, color.b];
|
|
if (head[0] + head[1] + head[2] < 8) return "#2a2a30";
|
|
return `rgb(${head[0]},${head[1]},${head[2]})`;
|
|
}
|
|
|
|
function buildList() {
|
|
listEl.replaceChildren();
|
|
rows.clear();
|
|
for (const fixture of stage.fixtures) {
|
|
const li = document.createElement("li");
|
|
li.id = `fx-${fixture.id}`;
|
|
li.className = fixture.kind;
|
|
li.dataset.id = fixture.id;
|
|
li.title = fixture.label;
|
|
const swatch = document.createElement("span");
|
|
swatch.className = "swatch";
|
|
const name = document.createElement("span");
|
|
name.className = "name";
|
|
name.textContent = fixture.label;
|
|
const addr = document.createElement("span");
|
|
addr.className = "addr";
|
|
addr.textContent = fixture.dmx == null ? "no address" : `DMX ${fixture.dmx}`;
|
|
li.append(swatch, name, addr);
|
|
li.addEventListener("click", () => {
|
|
selected = fixture.id;
|
|
updateDetail();
|
|
draw();
|
|
});
|
|
listEl.append(li);
|
|
rows.set(fixture.id, { li, swatch, addr });
|
|
}
|
|
}
|
|
|
|
function updateList() {
|
|
for (const fixture of stage.fixtures) {
|
|
const row = rows.get(fixture.id);
|
|
const live = state.fixtures[fixture.id] || {};
|
|
row.swatch.style.background = swatchColor(fixture, live);
|
|
row.li.classList.toggle("selected", fixture.id === selected);
|
|
if (!live.patched) row.addr.textContent = "no address";
|
|
else if (live.function && live.function !== "mix" && live.function !== "pixels") {
|
|
row.addr.textContent = `DMX ${fixture.dmx} · ${live.function_label || live.function}`;
|
|
} else row.addr.textContent = `DMX ${fixture.dmx}`;
|
|
}
|
|
}
|
|
|
|
function updateDetail() {
|
|
const fixture = stage.fixtures.find((item) => item.id === selected);
|
|
if (!fixture) {
|
|
detailEl.hidden = true;
|
|
return;
|
|
}
|
|
const live = state.fixtures[fixture.id] || {};
|
|
detailEl.hidden = false;
|
|
detailTitle.textContent = fixture.name || fixture.label;
|
|
const bits = [];
|
|
if (fixture.dmx != null) bits.push(`DMX ${fixture.dmx}`);
|
|
if (fixture.def) bits.push(fixture.def);
|
|
else if (fixture.mode) bits.push(fixture.mode);
|
|
if (fixture.kind === "can") bits.push(`direction ${fixture.direction || 0}°`);
|
|
if (fixture.kind === "mover") bits.push(`zero ${fixture.zero || 0}°`);
|
|
if (live.function_label && live.function !== "mix" && live.function !== "pixels") bits.push(live.function_label);
|
|
if (live.speed_hz) bits.push(`${live.speed_hz.toFixed(1)} Hz`);
|
|
if (live.reset) bits.push("reset");
|
|
if (fixture.kind === "mover" && live.pan_deg != null) {
|
|
bits.push(`pan ${live.pan_deg.toFixed(0)}°`);
|
|
bits.push(`zoom ${(live.zoom_deg || 0).toFixed(0)}°`);
|
|
if (live.spin_dps) bits.push(`lens ${live.spin_dps > 0 ? "CW" : "CCW"}`);
|
|
else if (live.lens_deg) bits.push(`lens ${live.lens_deg.toFixed(0)}°`);
|
|
}
|
|
detailMeta.textContent = bits.join(" · ");
|
|
const channels = live.channels || [];
|
|
channelsEl.replaceChildren();
|
|
channels.forEach((value, index) => {
|
|
const row = document.createElement("div");
|
|
row.className = "channel";
|
|
const label = document.createElement("span");
|
|
label.textContent = String(index + 1);
|
|
const title = document.createElement("span");
|
|
title.className = "ch-name";
|
|
title.textContent = (fixture.channel_names && fixture.channel_names[index]) || "";
|
|
const bar = document.createElement("div");
|
|
bar.className = "bar";
|
|
const fill = document.createElement("span");
|
|
fill.style.width = `${(value / 255) * 100}%`;
|
|
bar.append(fill);
|
|
const num = document.createElement("span");
|
|
num.textContent = String(value);
|
|
row.append(label, title, bar, num);
|
|
channelsEl.append(row);
|
|
});
|
|
}
|
|
|
|
function updateStatus() {
|
|
const age = state.age;
|
|
const live = age != null && age < 1.5;
|
|
statusEl.classList.toggle("live", live);
|
|
if (!live) {
|
|
statusEl.textContent = state.packets ? "Art-Net idle" : "waiting for Art-Net";
|
|
return;
|
|
}
|
|
const universe = state.universe == null ? "" : ` · universe ${state.universe}`;
|
|
statusEl.textContent = `Art-Net ${state.rate} fps${universe}`;
|
|
}
|
|
|
|
async function poll() {
|
|
try {
|
|
state = await fetch("/api/state").then((response) => response.json());
|
|
updateStatus();
|
|
updateList();
|
|
if (selected) updateDetail();
|
|
} catch {
|
|
statusEl.classList.remove("live");
|
|
statusEl.textContent = "view disconnected";
|
|
}
|
|
}
|
|
|
|
function frame() {
|
|
draw();
|
|
if (stage) updateList();
|
|
requestAnimationFrame(frame);
|
|
}
|
|
|
|
function pointerMetres(event) {
|
|
const rect = canvas.getBoundingClientRect();
|
|
const view = layout(rect.width, rect.height);
|
|
return {
|
|
x: view.mx(event.clientX - rect.left),
|
|
y: view.my(event.clientY - rect.top),
|
|
};
|
|
}
|
|
|
|
function fixtureAt(x, y) {
|
|
let best = null;
|
|
let bestDist = 0.55;
|
|
for (const fixture of stage.fixtures) {
|
|
const dist = Math.hypot(fixture.x - x, fixture.y - y);
|
|
if (dist < bestDist) {
|
|
best = fixture;
|
|
bestDist = dist;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
function wrapDegrees(degrees) {
|
|
return ((Math.round(degrees) + 180) % 360 + 360) % 360 - 180;
|
|
}
|
|
|
|
function aimDegrees(fixture, x, y) {
|
|
return wrapDegrees(Math.atan2(x - fixture.x, y - fixture.y) * 180 / Math.PI);
|
|
}
|
|
|
|
function saveHeading(fixture) {
|
|
const body = fixture.kind === "can"
|
|
? { id: fixture.id, direction: fixture.direction }
|
|
: { id: fixture.id, zero: fixture.zero };
|
|
fetch("/api/heading", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
}).catch(() => {});
|
|
}
|
|
|
|
canvas.addEventListener("pointerdown", (event) => {
|
|
if (!stage || event.button !== 0) return;
|
|
const { x, y } = pointerMetres(event);
|
|
const fixture = fixtureAt(x, y);
|
|
selected = fixture ? fixture.id : null;
|
|
drag = fixture ? { id: fixture.id, moved: false } : null;
|
|
if (drag) canvas.setPointerCapture(event.pointerId);
|
|
canvas.style.cursor = fixture ? "grabbing" : "default";
|
|
updateList();
|
|
updateDetail();
|
|
});
|
|
|
|
canvas.addEventListener("pointermove", (event) => {
|
|
if (!stage) return;
|
|
const { x, y } = pointerMetres(event);
|
|
if (!drag) {
|
|
canvas.style.cursor = fixtureAt(x, y) ? "grab" : "default";
|
|
return;
|
|
}
|
|
const fixture = stage.fixtures.find((item) => item.id === drag.id);
|
|
if (!fixture) return;
|
|
if (!drag.moved && Math.hypot(x - fixture.x, y - fixture.y) < 0.2) return;
|
|
drag.moved = true;
|
|
const heading = aimDegrees(fixture, x, y);
|
|
if (fixture.kind === "can") fixture.direction = heading;
|
|
else {
|
|
const live = state.fixtures[fixture.id] || {};
|
|
fixture.zero = wrapDegrees(heading - (live.pan_deg || 0));
|
|
}
|
|
updateDetail();
|
|
});
|
|
|
|
canvas.addEventListener("pointerup", () => {
|
|
if (!drag) return;
|
|
const fixture = stage.fixtures.find((item) => item.id === drag.id);
|
|
const moved = drag.moved;
|
|
drag = null;
|
|
canvas.style.cursor = "grab";
|
|
if (fixture && moved) {
|
|
aims.delete(fixture.id);
|
|
saveHeading(fixture);
|
|
}
|
|
updateDetail();
|
|
});
|
|
|
|
canvas.addEventListener("pointercancel", () => {
|
|
drag = null;
|
|
canvas.style.cursor = "default";
|
|
});
|
|
|
|
window.addEventListener("resize", resize);
|
|
|
|
function showStage(loaded) {
|
|
stage = loaded;
|
|
selected = null;
|
|
detailEl.hidden = true;
|
|
nameEl.textContent = stage.name || "xchc";
|
|
document.title = stage.name || "xchc";
|
|
originEl.textContent = stage.origin || "";
|
|
const show = stage.show ? stage.show : "addresses from the stage map";
|
|
const target = stage.output_ip ? ` · QLC+ output ${stage.output_ip}` : "";
|
|
patchEl.textContent = `${show}${target}`;
|
|
buildList();
|
|
resize();
|
|
}
|
|
|
|
async function loadStageList() {
|
|
const { current, stages } = await fetch("/api/stages").then((response) => response.json());
|
|
stageFileEl.replaceChildren(
|
|
...stages.map((name) => new Option(name.replace(/\.json$/, ""), name, false, name === current)),
|
|
);
|
|
stageFileEl.disabled = stages.length < 2;
|
|
}
|
|
|
|
stageFileEl.addEventListener("change", async () => {
|
|
stageFileEl.disabled = true;
|
|
try {
|
|
const response = await fetch("/api/stage", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ file: stageFileEl.value }),
|
|
});
|
|
if (!response.ok) throw new Error("bad stage");
|
|
showStage(await response.json());
|
|
} catch {
|
|
statusEl.textContent = "could not load that stage";
|
|
}
|
|
await loadStageList().catch(() => {});
|
|
});
|
|
|
|
fetch("/api/stage")
|
|
.then((response) => response.json())
|
|
.then((loaded) => {
|
|
showStage(loaded);
|
|
loadStageList().catch(() => {});
|
|
poll();
|
|
setInterval(poll, 50);
|
|
requestAnimationFrame(frame);
|
|
})
|
|
.catch(() => {
|
|
statusEl.textContent = "could not load the stage";
|
|
});
|