Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 `<path>` 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.
Expand Down
19 changes: 16 additions & 3 deletions packages/morph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,28 @@
const active = new WeakMap<SVGGElement, PixelMorphRun>();
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]) {

Check warning on line 169 in packages/morph/src/index.ts

View workflow job for this annotation

GitHub Actions / verify

unicorn(no-useless-spread)

packages/morph/src/index.ts:169:23: Using a spread operator here creates a new array unnecessarily.
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 {
Expand Down
Loading