/* Rig geometry for the canvas: strips along their rows, pixel 0 at the * strip's start end (next to the centre line for start: "center"). * Strip placement (row, col, reversed) comes from the server (layout.py). */ (function (g) { 'use strict'; /** * @param {object} layout {rows, columns, leds_per_strip} * @param {Array} strips [{row, col, leds, reversed}] * @returns {object} {ledPitch, ledW, ledH, strips:[{x0, y, w, labelX, labelY, labelAlign}], centreLines} */ function computeGeometry(layout, strips, width, height, opts = {}) { const margin = opts.margin ?? 16; const colGap = opts.colGap ?? 32; const labelH = opts.labelH ?? 16; const rows = layout.rows; const cols = layout.columns; const n = layout.leds_per_strip; const usableW = Math.max(cols * n, width - 2 * margin - (cols - 1) * colGap); const colW = usableW / cols; const pitch = colW / n; const rowH = Math.max(labelH + 6, (height - 2 * margin) / rows); const ledH = Math.max(6, Math.min(rowH - labelH - 10, 18)); const gap = pitch >= 3 ? 1 : 0; const ledW = Math.max(1, pitch - gap); const out = strips.map((s) => { const x0 = margin + s.col * (colW + colGap); const yTop = margin + s.row * rowH; // strip sits just under its label; leftover row space goes below it const y = yTop + labelH + 4; return { x0, y, w: colW, h: ledH, top: yTop, rowH, reversed: Boolean(s.reversed), labelX: s.reversed ? x0 + colW : x0, labelY: yTop + labelH / 2 + 2, labelAlign: s.reversed ? 'right' : 'left', }; }); const centreLines = []; for (let c = 1; c < cols; c += 1) { centreLines.push(margin + c * (colW + colGap) - colGap / 2); } return { ledPitch: pitch, ledW, ledH, strips: out, centreLines }; } /** * Geometry from each strip's rig place [x0, y0, x1, y1] (y up, centre 0, as the controller sends it), * fitted to the canvas with one scale. Used once every strip has a place. * @returns {object} {placed: true, ledH, strips: [{ax, ay, bx, by, ang, len, pitch, labelX, labelY}], centre} */ function computePlaced(strips, width, height, opts = {}) { const margin = opts.margin ?? 28; const ledH = opts.ledH ?? 12; let lo = [Infinity, Infinity]; let hi = [-Infinity, -Infinity]; for (const s of strips) { const p = s.place; for (let k = 0; k < 4; k += 2) { lo = [Math.min(lo[0], p[k]), Math.min(lo[1], p[k + 1])]; hi = [Math.max(hi[0], p[k]), Math.max(hi[1], p[k + 1])]; } } const spanX = Math.max(hi[0] - lo[0], 1e-3); const spanY = Math.max(hi[1] - lo[1], 1e-3); const scale = Math.min((width - 2 * margin) / spanX, (height - 2 * margin) / spanY); const ox = width / 2 - ((lo[0] + hi[0]) / 2) * scale; const oy = height / 2 + ((lo[1] + hi[1]) / 2) * scale; const tx = (x) => ox + x * scale; const ty = (y) => oy - y * scale; const out = strips.map((s) => { const [x0, y0, x1, y1] = s.place; const ax = tx(x0); const ay = ty(y0); const bx = tx(x1); const by = ty(y1); const len = Math.hypot(bx - ax, by - ay); const n = s.leds; return { ax, ay, bx, by, len, ang: Math.atan2(by - ay, bx - ax), pitch: len / n, labelX: (ax + bx) / 2, labelY: (ay + by) / 2 - ledH / 2 - 9, }; }); return { placed: true, ledH, strips: out, centre: { x: tx(0), y: ty(0) } }; } /** x of pixel i of a strip (pixel 0 at the start end). */ function pixelX(geo, sg, i, leds) { const pos = sg.reversed ? leds - 1 - i : i; return sg.x0 + pos * geo.ledPitch; } g.RigLayout = { computeGeometry, computePlaced, pixelX }; })(typeof self !== 'undefined' ? self : globalThis);