diff --git a/CHANGELOG.md b/CHANGELOG.md index fd0a1db..63d52da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- `@botharness/pixel-morph`: one morph that throws no longer stops the shared animation loop for the others, and a morph started from inside a frame no longer starts a second frame chain. + ## 0.2.0 - `@botharness/pixel-morph`: `morphPixels` takes `frameMs` to advance in fixed steps and only draw when the step changes, and `markup` to choose the renderer; the new `pixelPathMarkup` draws one `` per colour. All running morphs now share one `requestAnimationFrame` loop instead of one each. Planning a face→symbol morph takes about 0.16 ms, and a frame takes about 0.12 ms to compute and serialise. Defaults and `pixelMarkup` output are unchanged. In BotHarness this cut 32 simultaneous pixel morphs from about 1 s to about 0.2 s of main-thread work per second. diff --git a/packages/morph/src/index.ts b/packages/morph/src/index.ts index eb28daf..8bd67d2 100644 --- a/packages/morph/src/index.ts +++ b/packages/morph/src/index.ts @@ -161,15 +161,28 @@ export function pixelPathMarkup(cells: readonly PixelCell[]): string { const active = new WeakMap(); const tickers = new Set<(time: number) => void>(); let loopId = 0; +let looping = false; function loop(time: number): void { - for (const tick of [...tickers]) tick(time); - loopId = tickers.size > 0 ? requestAnimationFrame(loop) : 0; + loopId = 0; + looping = true; + for (const tick of [...tickers]) { + try { + tick(time); + } catch (error) { + tickers.delete(tick); + queueMicrotask(() => { + throw error; + }); + } + } + looping = false; + if (tickers.size > 0) loopId = requestAnimationFrame(loop); } function addTicker(tick: (time: number) => void): void { tickers.add(tick); - if (loopId === 0) loopId = requestAnimationFrame(loop); + if (!looping && loopId === 0) loopId = requestAnimationFrame(loop); } function removeTicker(tick: (time: number) => void): void {