feat: sequence window, finer timeline, zone brightness to Wi-Fi drivers

- A sequence's length is a fixed window (window_beats / window_seconds), not its presets.
  The timeline is exactly the window long; clips past it are cut on save.
- The ruler adds finer ticks when zoomed in; Snap has Auto (follows the zoom), 0.25 s and 0.05 s.
  Steps and the window go down to 50 ms.
- Ctrl+wheel zooms around the pointer; the zoom buttons zoom around the last pointer position.
- Sequence start and the zone brightness slider reach Wi-Fi drivers (matched by hostname or MAC).
- A preset select with brightness also sends the brightness to Wi-Fi drivers, as ESP-NOW does.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-16 21:22:17 +13:00
1 parent 581b751904
commit a1c8ce853b
21 files changed
+419 -318

No files matched your search

+91 -32
View File
@@ -20,6 +20,7 @@ const seqTl = {
sel: null, // {clip: id} or {track: uid}
ppb: 40, // pixels per beat (or second)
snap: 1,
snapAuto: true, // the Snap menu's Auto: follows the zoom
loopEnd: 0,
loopEndSet: false, // the user moved the end marker
brush: '', // the browser item double-click adds
@@ -28,6 +29,7 @@ const seqTl = {
pollTimer: null,
raf: 0,
drag: null,
hoverX: null, // the pointer's last x over the timeline: where the zoom buttons zoom around
};
/* ------------------------------------------------------------------ basics */
@@ -52,12 +54,12 @@ function tlBpm() {
}
function tlMin() {
return tlTimed() ? 0.1 : 0.25;
return tlTimed() ? 0.05 : 0.25;
}
function tlSnapOptions() {
return tlTimed()
? [['1', '1 s'], ['0.5', '½ s'], ['0.1', '0.1 s']]
? [['1', '1 s'], ['0.5', '½ s'], ['0.25', '¼ s'], ['0.1', '0.1 s'], ['0.05', '0.05 s']]
: [['4', 'Bar'], ['1', 'Beat'], ['0.5', '½ beat'], ['0.25', '¼ beat']];
}
@@ -84,15 +86,30 @@ function tlEnd() {
return seqTl.clips.reduce((m, c) => Math.max(m, c.start + c.len), 0);
}
/** The sequence's window: its length, set by the end marker. Clips never change it; the ones past it are cut on save. */
function tlLoopLen() {
const end = tlEnd();
if (!seqTl.loopEndSet) seqTl.loopEnd = Math.max(TL_BAR, Math.ceil(end / TL_BAR - TL_EPS) * TL_BAR);
return Math.max(seqTl.loopEnd, end);
return Math.max(tlMin(), seqTl.loopEnd);
}
/** The window the record has, in the editor's units; 0 for records saved before windows. */
function tlWindowOf(doc) {
if (!doc) return 0;
const v = tlTimed() ? doc.window_seconds : doc.window_beats;
return typeof v === 'number' && v > 0 ? tlR(v) : 0;
}
/** The window in both units, as saved: the player and older clients read whichever they use. */
function tlWindowFields() {
const w = tlLoopLen();
const beatS = 60 / tlBpm();
return tlTimed()
? { window_beats: tlR(w / beatS), window_seconds: tlR(w) }
: { window_beats: tlR(w), window_seconds: tlR(w * beatS) };
}
/** True when [start, start+len) is free on the track (ignoring one clip, the one being moved). */
function tlFits(uid, start, len, ignoreId) {
if (start < -TL_EPS || len < tlMin() - TL_EPS) return false;
if (start < -TL_EPS || len < tlMin() - TL_EPS || start + len > tlLoopLen() + TL_EPS) return false;
return !seqTl.clips.some(
(c) => c.uid === uid && c.id !== ignoreId && start < c.start + c.len - TL_EPS && c.start < start + len - TL_EPS,
);
@@ -132,6 +149,8 @@ function tlFill(info) {
/** How long a sequence runs once, in the editor's current units. */
function tlSeqLen(doc) {
if (!doc) return TL_BAR;
const win = tlWindowOf(doc);
if (win) return win;
const timed = tlTimed();
const beatS = 60 / tlBpm();
const lanes = normalizeSequenceLanes(doc);
@@ -272,13 +291,13 @@ function tlCommit(r) {
function tlToLanes() {
const timed = tlTimed();
const beatS = 60 / tlBpm();
const total = tlLoop() ? tlLoopLen() : 0;
const total = tlLoopLen(); // every lane runs the whole window, padded with rests
const lanes = [];
const lane_opts = [];
const lanes_group_ids = [];
const map = [];
const len = (v) =>
timed ? { seconds: tlR(Math.max(0.1, v)), beats: tlR(Math.max(0.25, v / beatS)) } : { beats: tlR(Math.max(0.25, v)), seconds: tlR(Math.max(0.1, v * beatS)) };
timed ? { seconds: tlR(Math.max(0.05, v)), beats: tlR(Math.max(0.25, v / beatS)) } : { beats: tlR(Math.max(0.25, v)), seconds: tlR(Math.max(0.05, v * beatS)) };
seqEd.targets.forEach((t, ti) => {
const cs = tlClipsOn(t.uid);
if (!cs.length) return;
@@ -286,14 +305,16 @@ function tlToLanes() {
const starts = [];
let at = 0;
cs.forEach((c) => {
const cl = tlR(Math.min(c.len, total - c.start)); // cut at the window's end
if (cl <= TL_EPS) return;
if (c.start > at + TL_EPS) {
steps.push({ preset_id: '', rest: true, ...len(c.start - at) });
starts.push(at);
}
const st = c.play.startsWith('a:') ? { animation_id: c.play.slice(2) } : { preset_id: c.play.slice(2) };
steps.push({ ...st, ...len(c.len) });
steps.push({ ...st, ...len(cl) });
starts.push(c.start);
at = c.start + c.len;
at = c.start + cl;
});
if (total > at + TL_EPS) {
steps.push({ preset_id: '', rest: true, ...len(total - at) });
@@ -334,6 +355,8 @@ function seqTlOnOpen(doc) {
const lanes = normalizeSequenceLanes(doc || {});
const r = tlFlatten(doc || {}, lanes, Array.isArray(doc && doc.lane_opts) ? doc.lane_opts : []);
tlCommit(r);
const win = tlWindowOf(doc);
if (win) seqTl.loopEnd = win; // the saved window, not the clips' end
// Older sequences can use lane options the timeline doesn't have; it shows their first pass
tlNotice(
r.lossy.length
@@ -359,15 +382,26 @@ function tlHeadWidth() {
return h ? h.offsetWidth : 0;
}
/** Auto snap: the finest step the ruler shows at this zoom (never coarser than 1 beat or second). */
function tlAutoSnap() {
const steps = tlTimed() ? [0.05, 0.1, 0.25, 0.5, 1] : [0.25, 0.5, 1];
return steps.find((s) => s * seqTl.ppb >= 36) || 1;
}
function tlBuildSnap() {
const sel = tlEl('tl-snap');
if (!sel) return;
const opts = tlSnapOptions();
const opts = [['auto', 'Auto (zoom)'], ...tlSnapOptions()];
const was = String(seqTl.snap);
sel.innerHTML = '';
opts.forEach(([v, t]) => sel.appendChild(new Option(t, v)));
sel.value = opts.some(([v]) => v === was) ? was : tlTimed() ? '0.5' : '1';
seqTl.snap = parseFloat(sel.value);
if (seqTl.snapAuto) {
sel.value = 'auto';
seqTl.snap = tlAutoSnap();
} else {
sel.value = opts.some(([v]) => v === was) ? was : tlTimed() ? '0.5' : '1';
seqTl.snap = parseFloat(sel.value);
}
}
/** Select a clip or track without rebuilding the tracks (so double-clicks still land). */
@@ -381,11 +415,13 @@ function tlSelect(sel) {
function tlRender() {
const canvas = tlEl('tl-canvas');
if (!canvas || !seqTl.on) return;
if (seqTl.snapAuto) seqTl.snap = tlAutoSnap(); // Auto follows the zoom, so drags land on the ticks shown
const ppb = seqTl.ppb;
const timed = tlTimed();
const loop = tlLoop();
const loopLen = tlLoopLen();
const units = Math.max(4 * TL_BAR, Math.ceil((Math.max(tlEnd(), loop ? loopLen : 0) + 2 * TL_BAR) / TL_BAR) * TL_BAR);
// The timeline is exactly the window long: nothing past its end is shown or can be placed
const units = loopLen;
const W = units * ppb;
canvas.style.setProperty('--ppb', String(ppb));
canvas.innerHTML = '';
@@ -408,10 +444,19 @@ function tlRender() {
const ruler = document.createElement('div');
ruler.className = 'tl-ruler';
ruler.style.width = `${W}px`;
const every = timed ? (ppb >= 30 ? 1 : ppb >= 12 ? 5 : ppb >= 5 ? 10 : 30) : 1;
// Zoomed out: whole seconds, then every 5/10/30. Zoomed in: the finest step that keeps ~36px between ticks,
// down to a tenth of a second or a quarter of a beat.
let every = timed ? (ppb >= 30 ? 1 : ppb >= 12 ? 5 : ppb >= 5 ? 10 : 30) : 1;
if (ppb >= 30) {
const fine = timed ? [0.05, 0.1, 0.25, 0.5, 1] : [0.25, 0.5, 1];
every = fine.find((s) => s * ppb >= 36) || 1;
}
const barsPerLabel = ppb * TL_BAR >= 28 ? 1 : ppb * TL_BAR >= 14 ? 2 : 4; // zoomed far out
for (let u = 0; u <= units; u += every) {
const bar = !timed && u % (TL_BAR * barsPerLabel) === 0;
const FRAC = { 0.25: '¼', 0.5: '½', 0.75: '¾' };
const n = Math.floor(units / every + TL_EPS);
for (let i = 0; i <= n; i++) {
const u = tlR(i * every);
const bar = !timed && Number.isInteger(u) && u % (TL_BAR * barsPerLabel) === 0;
const major = timed || bar;
if (!major && ppb < 18) continue;
const tick = document.createElement('span');
@@ -419,7 +464,11 @@ function tlRender() {
tick.style.left = `${u * ppb}px`;
if (timed) tick.textContent = `${u}s`;
else if (bar) tick.textContent = String(u / TL_BAR + 1);
else if (ppb >= 44) tick.textContent = `${Math.floor(u / TL_BAR) + 1}.${(u % TL_BAR) + 1}`;
else if (ppb >= 44) {
const beat = Math.floor(u % TL_BAR + TL_EPS);
const frac = tlR(u - Math.floor(u / TL_BAR + TL_EPS) * TL_BAR - beat);
tick.textContent = `${Math.floor(u / TL_BAR + TL_EPS) + 1}.${beat + 1}${FRAC[frac] || ''}`;
}
ruler.appendChild(tick);
}
if (loop) {
@@ -482,7 +531,9 @@ function tlRender() {
if (e.target !== lane) return;
tlAddAt(t.uid, tlQ(tlLaneX(lane, e.clientX) / seqTl.ppb, true), seqTl.brush);
});
tlClipsOn(t.uid).forEach((c) => lane.appendChild(tlClipEl(c)));
tlClipsOn(t.uid).forEach((c) => {
if (c.start < loopLen - TL_EPS) lane.appendChild(tlClipEl(c));
});
row.appendChild(head);
row.appendChild(lane);
canvas.appendChild(row);
@@ -519,7 +570,8 @@ function tlClipEl(c) {
el.className = `tl-clip${info.anim ? ' anim' : ''}${seqTl.sel && seqTl.sel.clip === c.id ? ' sel' : ''}`;
el.dataset.id = String(c.id);
el.style.left = `${c.start * seqTl.ppb}px`;
el.style.width = `${c.len * seqTl.ppb}px`;
// A clip reaching past the window is drawn up to its end
el.style.width = `${Math.min(c.len, tlLoopLen() - c.start) * seqTl.ppb}px`;
const fill = tlFill(info);
if (fill) el.style.setProperty('--clip-fill', fill);
el.title = `${info.name} — ${c.len}${tlTimed() ? ' s' : ' beat(s)'}`;
@@ -898,7 +950,7 @@ function tlSetUnits(timed) {
if (!timed && !tlLoop()) return;
const beatS = 60 / tlBpm();
const k = timed ? beatS : 1 / beatS;
const min = timed ? 0.1 : 0.25;
const min = timed ? 0.05 : 0.25;
const q = (x) => tlR(Math.round((x * k) / min) * min);
seqTl.clips.forEach((c) => {
c.start = q(c.start);
@@ -921,7 +973,7 @@ function tlSetUnits(timed) {
}
const TL_PPB_MIN = 6;
const TL_PPB_MAX = 240;
const TL_PPB_MAX = 800; // 0.05 s comes out at 40px, the finest tick
/**
* Zoom by `f`, keeping the time under `clientX` where it is (the middle of the view without one).
@@ -947,21 +999,21 @@ function tlZoom(f, clientX) {
}
/**
* Wheel over the tracks zooms around the pointer (trackpad pinch too: it arrives as ctrl+wheel).
* The wheel over the track names, Shift+wheel and sideways trackpad swipes still scroll.
* Ctrl+wheel (trackpad pinch arrives as ctrl+wheel) zooms around the pointer. A plain wheel scrolls as
* usual: the timeline, then the page. Shift+wheel and sideways swipes scroll sideways.
* Wheel events come in bursts, so they add up and apply once per frame.
*/
function tlWheel(e) {
if (seqTl.drag) return;
const sc = tlEl('tl-scroll');
if (!sc) return;
const overHeads = e.target.closest && e.target.closest('.tl-head') && !e.ctrlKey;
if (overHeads || (e.shiftKey && !e.ctrlKey) || (Math.abs(e.deltaX) > Math.abs(e.deltaY) && !e.ctrlKey)) return;
if (!e.ctrlKey && !e.metaKey) return;
if (e.target.closest && e.target.closest('.tl-head')) return;
e.preventDefault();
const dy = e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? 400 : 1);
const w = seqTl.wheel || (seqTl.wheel = { f: 1, x: 0, raf: 0 });
// About 15% per mouse wheel notch; big deltas (fast wheels, some mice) are capped per event
w.f *= Math.min(1.25, Math.max(0.8, Math.exp(-dy * (e.ctrlKey ? 0.01 : 0.0015))));
w.f *= Math.min(1.25, Math.max(0.8, Math.exp(-dy * 0.01)));
w.x = e.clientX;
if (!w.raf) {
w.raf = requestAnimationFrame(() => {
@@ -1052,7 +1104,7 @@ function tlDragMove(e) {
const ppb = seqTl.ppb;
const dx = dxp / ppb;
if (d.kind === 'loop') {
seqTl.loopEnd = Math.max(tlR(tlEnd()), tlMin(), tlQ(d.base + dx));
seqTl.loopEnd = Math.max(tlMin(), tlQ(d.base + dx));
seqTl.loopEndSet = true;
const brace = document.querySelector('#tl-canvas .tl-loop');
const line = document.querySelector('#tl-canvas .tl-endline');
@@ -1244,12 +1296,19 @@ document.addEventListener('DOMContentLoaded', () => {
tlRender();
});
on('tl-snap', 'change', (e) => {
seqTl.snap = parseFloat(e.target.value) || 1;
seqTl.snapAuto = e.target.value === 'auto';
seqTl.snap = seqTl.snapAuto ? tlAutoSnap() : parseFloat(e.target.value) || 1;
});
on('tl-zoom-in', 'click', () => tlZoom(1.4));
// The buttons zoom around where the pointer last was over the timeline (the middle until it has been)
on('tl-zoom-in', 'click', () => tlZoom(1.4, seqTl.hoverX));
const tlScroll = tlEl('tl-scroll');
if (tlScroll) tlScroll.addEventListener('wheel', tlWheel, { passive: false });
on('tl-zoom-out', 'click', () => tlZoom(1 / 1.4));
if (tlScroll) {
tlScroll.addEventListener('wheel', tlWheel, { passive: false });
tlScroll.addEventListener('pointermove', (e) => {
seqTl.hoverX = e.clientX;
});
}
on('tl-zoom-out', 'click', () => tlZoom(1 / 1.4, seqTl.hoverX));
document.querySelectorAll('#tl-units button').forEach((b) => b.addEventListener('click', () => tlSetUnits(b.dataset.v === 'time')));
on('tl-play', 'click', async () => {
const ok = await saveSequenceEditor();
+2 -1
View File
@@ -190,7 +190,7 @@ function normalizeSequenceLanes(doc) {
length: s.length === 'animation' ? 'animation' : '',
targets: Array.isArray(s.targets) ? s.targets.map((x) => parseInt(String(x), 10)).filter((x) => x >= 0) : [],
beats: Math.max(0.25, parseFloat(String(s.beats != null ? s.beats : 1)) || 1),
seconds: Math.max(0.1, parseFloat(String(s.seconds != null ? s.seconds : 3)) || 3),
seconds: Math.max(0.05, parseFloat(String(s.seconds != null ? s.seconds : 3)) || 3),
group_ids: Array.isArray(s.group_ids)
? s.group_ids.map((x) => String(x).trim()).filter(Boolean)
: [],
@@ -890,6 +890,7 @@ async function saveSequenceEditor() {
// A one-shot (loop off) is always timed in seconds
advance_mode: (modeSel && modeSel.value === 'time') || (loopCb && !loopCb.checked) ? 'time' : 'beats',
loop: loopCb ? !!loopCb.checked : true,
...tlWindowFields(),
steps: nonEmpty.length === 1 ? nonEmpty[0] : [],
};