Skip to content

Explainer videos and product demos made by your AI agent. Free and open source: a local voice (Kokoro), word timing (Whisper) and a canvas renderer turn a script into a narrated MP4.

s.ui respects hidden s.group timing (review finding), with a regression test

Vincent Schmalbachb4e9af28e95e
diff --git a/engine/ui.js b/engine/ui.js
index 590e980..ddeb25f 100644
--- a/engine/ui.js
+++ b/engine/ui.js
@@ -45,10 +45,13 @@ const FLOATERS = [
{ x: 0.365, y: 0.954, r: 3 }, { x: 0.635, y: 0.037, r: -3 },
];
+// While a hidden s.group() runs (to collect timing and sounds), the kit must
+// not draw: these canvas calls are skipped then, everything else goes through.
+const DRAW_CALLS = new Set(['fill', 'stroke', 'fillText', 'strokeText', 'drawImage', 'fillRect', 'strokeRect', 'clearRect', 'putImageData']);
+
export class UI {
constructor(s) {
this.s = s;
- this.c = s.ctx;
const th = s.theme;
const b = s.engine.config.brand || {};
const accent = b.accent ?? th.colors[th.accent];
@@ -78,6 +81,30 @@ export class UI {
this._layers = 0;
}
+ // The canvas, or a stand-in that draws nothing while a group is hidden.
+ get c() {
+ if (!(this.s._suppress > 0)) return this.s.ctx;
+ if (!this._quiet) {
+ const real = this.s.ctx;
+ this._quiet = new Proxy(real, {
+ get(target, key) {
+ if (DRAW_CALLS.has(key)) return () => {};
+ const v = target[key];
+ return typeof v === 'function' ? v.bind(target) : v;
+ },
+ set(target, key, value) {
+ target[key] = value;
+ return true;
+ },
+ });
+ }
+ return this._quiet;
+ }
+
+ get hidden() {
+ return this.s._suppress > 0;
+ }
+
// Scale of the current transform. Canvas shadows and blur filters work in
// device pixels, so they are multiplied by this to zoom with the camera.
get k() {
@@ -364,7 +391,7 @@ export class UI {
// ---------- text ----------
_log(str, x, y, w, size, align, base) {
- if (!str.trim()) return;
+ if (!str.trim() || this.hidden) return;
const x0 = align === 'center' ? x - w / 2 : align === 'right' ? x - w : x;
const [t, b] = base === 'top' ? [0, 1.2] : base === 'alphabetic' ? [-0.8, 0.22] : [-0.6, 0.6];
this.s._logText({ text: str, x0, y0: y + t * size, x1: x0 + w, y1: y + b * size, size, fg: null, bg: null, ui: this._layer + 1, zoomed: this.s._camMoved });
@@ -854,6 +881,7 @@ export class UI {
}
icon(name, x, y, size, color = this.colors.text, weight = 2) {
+ if (this.hidden) return;
this.s._iconAt(name, x, y, size, color, 1, 1, weight);
}
diff --git a/test/ui-kit.test.js b/test/ui-kit.test.js
new file mode 100644
index 0000000..95c363c
--- /dev/null
+++ b/test/ui-kit.test.js
@@ -0,0 +1,96 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { Stage } from '../engine/stage.js';
+import { UI } from '../engine/ui.js';
+import { getTheme } from '../engine/themes.js';
+
+Stage.UI = UI;
+
+// Just enough of the browser's DOMMatrix and DOMPoint for the stage's text log.
+globalThis.DOMPoint ??= class {
+ constructor(x = 0, y = 0) {
+ this.x = x;
+ this.y = y;
+ }
+};
+globalThis.DOMMatrix ??= class {
+ constructor([a, b, c, d, e, f]) {
+ Object.assign(this, { a, b, c, d, e, f });
+ }
+ transformPoint(p) {
+ return new DOMPoint(this.a * p.x + this.c * p.y + this.e, this.b * p.x + this.d * p.y + this.f);
+ }
+};
+
+// A canvas that records what gets drawn.
+function fakeCanvas() {
+ const drawn = [];
+ const ctx = new Proxy(
+ { font: '', fillStyle: '#000', globalAlpha: 1 },
+ {
+ get(target, key) {
+ if (key in target) return target[key];
+ if (key === 'drawn') return drawn;
+ if (key === 'measureText') return (t) => ({ width: String(t).length * 10 });
+ if (key === 'getTransform') return () => ({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0, transformPoint: (p) => p });
+ if (['fill', 'stroke', 'fillText', 'drawImage', 'fillRect'].includes(key)) return (...args) => drawn.push([key, ...args]);
+ return () => {};
+ },
+ set(target, key, value) {
+ target[key] = value;
+ return true;
+ },
+ },
+ );
+ return ctx;
+}
+
+function stageAt(t, texts) {
+ const theme = getTheme('clean');
+ const engine = {
+ theme,
+ pen: { draw() {} },
+ W: 1920,
+ H: 1080,
+ safeArea: { x: 0, y: 0, w: 1920, h: 1080, bottom: 1080 },
+ config: { brand: null },
+ timeline: { pace: 1, duration: 10, frames: 300, fps: 30, width: 1920, height: 1080, scenes: [] },
+ fps: 30,
+ scale: 1,
+ recorder: null,
+ textLog: texts,
+ };
+ const scene = { start: 0, dur: 10, id: 'demo', index: 0, words: [], marks: {}, voice: null, lead: 0 };
+ return new Stage(engine, fakeCanvas(), scene, t);
+}
+
+test('s.ui draws nothing while its s.group is hidden, before and after', () => {
+ const drawnAt = (t) => {
+ const texts = [];
+ const s = stageAt(t, texts);
+ s.group({ at: 2, out: 4, x: 500, y: 300 }, () => {
+ s.ui.card(0, 0, 400, 200);
+ s.ui.text('Inside the group', 20, 40);
+ s.ui.button('Save', 20, 120, 160, 50);
+ });
+ return { drawn: s.ctx.drawn.length, texts: texts.length };
+ };
+ assert.deepEqual(drawnAt(0), { drawn: 0, texts: 0 });
+ assert.deepEqual(drawnAt(1), { drawn: 0, texts: 0 });
+ const visible = drawnAt(3);
+ assert.ok(visible.drawn > 0, 'the group draws while it is visible');
+ assert.ok(visible.texts >= 2, 'check sees the text while it is visible');
+ assert.deepEqual(drawnAt(5), { drawn: 0, texts: 0 });
+});
+
+test('a hidden s.ui group still records its sounds', () => {
+ const texts = [];
+ const s = stageAt(0, texts);
+ const events = [];
+ s.engine.recorder = { push: (e) => events.push(e) };
+ s.group({ at: 2, x: 0, y: 0 }, () => {
+ s.ui.panel(0, 0, 100, 100, { at: 2.5, sfx: 'pop' });
+ });
+ assert.ok(events.some((e) => e.sfx === 'pop' && Math.abs(e.at - 2.5) < 1e-9));
+ assert.equal(s.ctx.drawn.length, 0);
+});