Files
led-controller/led-simulator/web/dmx-draw.js
T
jimmyandClaude Opus 5.5 35207a868a sim: fixture editor for DDP and Art-Net/DMX fixtures
The rig sim receives Art-Net (UDP 6454, answers ArtPoll) and DDP (each port the
room's DDP strips use) in sim_net.c and passes changed universes and pixels to
the page over /sim/ws, now with a kind byte in front of each binary frame.

Rooms gain fixture types: ddp strips, artnet pixel strips, and artnet cans and
movers that read named channels from the room's dmx_definitions (the qlcplus
stage map's format). Overlapping DMX channels are refused. Cans and movers are
decoded and drawn as in the qlcplus stage simulator (dmx.js, dmx-draw.js).

In Edit mode: pick a type to add, edit port/offset/universe/address/colour
order/definition, see a can's or mover's live channels, make a controller
output aimed at the sim and send its test. The room dialog lists DMX
definitions, with presets, a channel editor, and .qxf and stage map import.

GET /sim/api/dmx shows what the receiver has had. Tests: test/api/test_sim_net.py
and test/browser/dmx_decode.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:13:58 +13:00

344 lines
13 KiB
JavaScript

/* Drawing the simulated rig's DMX cans and movers: glow, beam, floor spot and body.
* Ported from the qlcplus stage simulator (technicalkiwi/qlcplus simulator/static/app.js), same look and timing.
* Headings are degrees clockwise from +y, y up the screen, as in both sims. The live view is RigDmx.viewFixture().
*
* RigDmxDraw.drawLight(ctx, view, fixture, live) beam and glow (call with globalCompositeOperation 'lighter')
* RigDmxDraw.drawBody(ctx, view, fixture, live, selected)
* RigDmxDraw.animated(live) whether it changes with time (strobe, chase, smoothing)
*
* view: {sx(x), sy(y), scale}: metres -> canvas px; fixture: {id, kind: 'can'|'mover', label, x, y, direction, zero}
*/
(function (g) {
'use strict';
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],
];
// Below this plan reach a mover is pointing near straight down.
const SPOT_M = 0.5;
// Throw to the floor for the spot under a mover.
const DROP_M = 2.5;
// A can's beam: fixed along its heading.
const CAN_BEAM = { beam: 2.2, zoom: 25, focus: 0.35 };
const aims = new Map();
const now = () => performance.now() / 1000;
function luma(r, gr, b) {
return 0.2126 * r + 0.7152 * gr + 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]];
return map[i % 6].map((c) => Math.round(c * 255));
}
function scaleColour(colour, dimmer) {
const level = (dimmer || 0) / 255;
return colour.map((c) => Math.round(c * level));
}
function paletteColour(live, gradient) {
const palette = live.palette && live.palette.length ? live.palette : PAR_PALETTE;
const phase = now() * (live.speed_hz || 0.35);
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((c, slot) => Math.round(c + (next[slot] - c) * 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 wave = 0.5 - 0.5 * Math.cos(now() * (live.speed_hz || 0.4) * Math.PI * 2);
const depth = 0.05 + 0.95 * wave;
return [live.r || 0, live.g || 0, live.b || 0].map((c) => Math.round(c * 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 = ((now() * 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((c) => Math.round(c * hit));
}
return [live.r || 0, live.g || 0, live.b || 0];
}
function effectPixels(live) {
const source = (live.pixels || []).map((p) => p.slice());
if (!live.dynamic || !source.length) {
return source;
}
const count = source.length;
const lit = source.find((p) => p[0] + p[1] + p[2] > 0);
const fallback = lit || scaleColour([255, 255, 255], live.dimmer || 0);
const t = now() * (live.speed_hz || 1);
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);
level = Math.round(Math.abs((t % (2 * span)) - span)) === 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((c) => Math.round(c * level));
});
}
function mixPixels(pixels) {
let red = 0, green = 0, blue = 0, weight = 0;
for (const p of pixels) {
const w = p[0] + p[1] + p[2];
red += p[0] * w;
green += p[1] * w;
blue += p[2] * w;
weight += w;
}
return weight <= 0 ? [0, 0, 0] : [Math.round(red / weight), Math.round(green / weight), Math.round(blue / weight)];
}
function shutter(live) {
if (!live.strobe_hz) {
return 1;
}
const t = now();
if (live.strobe_mode === 'random') {
const bucket = Math.floor(t * live.strobe_hz);
return (Math.abs(Math.sin(bucket * 12.9898) * 43758.5453) % 1) > 0.55 ? 1 : 0;
}
const period = 1 / live.strobe_hz;
return t % period < period * 0.42 ? 1 : 0;
}
// Pan and reach ease toward the target at the fixture's Pan/Tilt Speed
function aimed(id, live, zero) {
const targetPan = (zero || 0) + (live.pan_deg || 0);
const targetBeam = live.beam_m || 0;
const t = now();
let st = aims.get(id);
if (!st) {
st = { pan: targetPan, beam: targetBeam, t, settled: true };
aims.set(id, st);
return st;
}
const dt = Math.min(0.05, Math.max(0, t - st.t));
st.t = t;
const fast = live.move == null ? 1 : live.move;
if (fast > 0.97) {
st.pan = targetPan;
st.beam = targetBeam;
} else {
const k = 1 - Math.exp(-dt / (0.05 + (1 - fast) * 1.8));
st.pan += (targetPan - st.pan) * k;
st.beam += (targetBeam - st.beam) * k;
}
st.settled = Math.abs(targetPan - st.pan) < 0.05 && Math.abs(targetBeam - st.beam) < 0.002;
return st;
}
function appearance(fixture, live) {
const pixels = fixture.kind === 'mover' ? effectPixels(live) : null;
const [r, gr, 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(gr * gate),
b: Math.round(b * gate),
dimmer: live.dimmer || 0,
pixels: pixels && pixels.map((p) => p.map((c) => Math.round(c * gate))),
};
}
function drawGlow(ctx, v, fixture, color) {
const level = (color.r + color.g + color.b) / (255 * 3);
if (level <= 0.01) {
return;
}
const x = v.sx(fixture.x);
const y = v.sy(fixture.y);
const radius = (fixture.kind === 'mover' ? 70 : 54) * (0.45 + level) * bodyScale(v);
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 drawSpot(ctx, v, fixture, pan, reach, half, color, spread) {
const rad = (pan * Math.PI) / 180;
const cx = v.sx(fixture.x + Math.sin(rad) * reach);
const cy = v.sy(fixture.y + Math.cos(rad) * reach);
const radius = Math.max(4, DROP_M * Math.tan(half) * v.scale);
const gradient = ctx.createRadialGradient(cx, cy, 0, cx, cy, radius);
const alpha = 0.85 * (1 - spread * 0.35);
gradient.addColorStop(0, `rgba(${color.r},${color.g},${color.b},${alpha})`);
gradient.addColorStop(0.7, `rgba(${color.r},${color.g},${color.b},${alpha * 0.6})`);
gradient.addColorStop(1, `rgba(${color.r},${color.g},${color.b},0)`);
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.arc(cx, cy, radius, 0, Math.PI * 2);
ctx.fill();
}
function drawBeam(ctx, v, fixture, live, color, fixed) {
const level = (color.r + color.g + color.b) / 255;
if (live.patched === false || level <= 0.02) {
return;
}
const aim = fixed || aimed(fixture.id, live, fixture.zero);
// Mover reach is signed: below zero the head has tilted past straight down.
const reach = fixed ? fixed.beam : aim.beam;
// DMX focus 0 is a wide wash. Focus 255 holds the zoom angle. Fixed beams pass spread: 0 tight, 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 half = ((tight + (wide - tight) * spread) * Math.PI) / 180;
if (fixture.kind === 'mover' && Math.abs(reach) < SPOT_M) {
drawSpot(ctx, v, fixture, aim.pan, reach, half, color, spread);
return;
}
const length = Math.abs(reach);
if (!length) {
return;
}
const rad = ((aim.pan + (reach < 0 ? 180 : 0)) * Math.PI) / 180;
const at = (a) => [v.sx(fixture.x + Math.sin(a) * length), v.sy(fixture.y + Math.cos(a) * length)];
const tip = at(rad);
const left = at(rad - half);
const right = at(rad + half);
const x0 = v.sx(fixture.x);
const y0 = v.sy(fixture.y);
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(left[0], left[1]);
ctx.lineTo(right[0], right[1]);
ctx.closePath();
const gradient = ctx.createLinearGradient(x0, y0, tip[0], 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 (now() * live.spin_dps * Math.PI) / 180;
}
return ((live.lens_deg || 0) * Math.PI) / 180;
}
// Bodies are drawn at a fixed pixel size in qlcplus; here they follow the zoom a little so a big room stays legible
function bodyScale(v) {
return Math.max(0.6, Math.min(1.6, v.scale / 90));
}
/** Beam and glow. Call with ctx.globalCompositeOperation = 'lighter'. */
function drawLight(ctx, v, fixture, live) {
const color = appearance(fixture, live);
if (fixture.kind === 'mover') {
drawBeam(ctx, v, fixture, live, color, null);
} else {
drawBeam(ctx, v, fixture, live, color, { pan: fixture.direction || 0, ...CAN_BEAM });
}
drawGlow(ctx, v, fixture, color);
}
function drawBody(ctx, v, fixture, live, selected) {
const color = appearance(fixture, live);
const x = v.sx(fixture.x);
const y = v.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 k = bodyScale(v);
const size = (fixture.kind === 'mover' ? 15 : 13) * k;
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 = selected ? 2.5 : 1.5;
ctx.strokeStyle = selected ? '#5aa9ff' : live.patched === false ? '#5c5c66' : '#c8c8d0';
ctx.stroke();
if (fixture.kind === 'mover' && pixels.length > 1) {
const ring = pixels.slice(1);
const spin = lensRotation(live);
ring.forEach((p, index) => {
const angle = spin + (index / ring.length) * Math.PI * 2 - Math.PI / 2;
ctx.beginPath();
ctx.arc(x + Math.cos(angle) * (size + 7 * k), y + Math.sin(angle) * (size + 7 * k), 3.5 * k, 0, Math.PI * 2);
ctx.fillStyle = p[0] + p[1] + p[2] > 0 ? `rgb(${p[0]},${p[1]},${p[2]})` : '#333';
ctx.fill();
});
}
if (fixture.kind === 'can') {
// a tick on the rim shows the way the can points
const a = ((fixture.direction || 0) * Math.PI) / 180;
ctx.beginPath();
ctx.moveTo(x + Math.sin(a) * size, y - Math.cos(a) * size);
ctx.lineTo(x + Math.sin(a) * (size + 6 * k), y - Math.cos(a) * (size + 6 * k));
ctx.lineWidth = 2;
ctx.strokeStyle = selected ? '#5aa9ff' : '#c8c8d0';
ctx.stroke();
}
ctx.fillStyle = lit && luma(head[0], head[1], head[2]) > 140 ? '#16161a' : '#f2f2f4';
ctx.font = `600 ${Math.round(11 * k)}px system-ui, sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(fixture.label, x, y);
ctx.textBaseline = 'alphabetic';
return size;
}
/** Whether the fixture's look changes with time, so the canvas must keep redrawing. */
function animated(fixture, live) {
if (live.strobe_hz || live.dynamic || live.spin_dps) {
return true;
}
if (fixture.kind === 'can' && ['jump', 'gradient', 'pulse', 'sound'].includes(live.function)) {
return true;
}
const st = aims.get(fixture.id);
return fixture.kind === 'mover' && !!st && !st.settled;
}
g.RigDmxDraw = { drawLight, drawBody, animated, bodyScale };
})(typeof self !== 'undefined' ? self : globalThis);