Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(ui-juice): countUp must not paint values outside [from, to]
fudjia349ea416a074
diff --git a/src/ui-juice.js b/src/ui-juice.jsindex fd672bfdd..a21e73aba 100644--- a/src/ui-juice.js+++ b/src/ui-juice.js@@ -89,10 +89,17 @@ export function countUp(el, from, to, opts = {}) {}const dur = opts.duration || durationMs(opts.token || '--duration-slow', 420);if (dur <= 0) { el.textContent = format(to); return; }- const start = now();+ // Seed the start from the FIRST frame's own timestamp rather than performance.now().+ // requestAnimationFrame is not contractually on the same time origin as+ // performance.now() (polyfills and some embedders pass a Date-based clock), and a+ // `t` behind `start` drives p negative — easeOutCubic then overshoots wildly and the+ // element renders garbage like "-42797%" before settling. Clamping p as well keeps a+ // single frame from ever painting a value outside [from, to].+ let start = null;const delta = to - from;const step = (t) => {- const p = Math.min(1, (t - start) / dur);+ if (start === null) start = t;+ const p = Math.max(0, Math.min(1, (t - start) / dur));el.textContent = format(from + delta * ease(p));if (p < 1) { if (countTimers) countTimers.set(el, requestAnimationFrame(step)); }else { el.textContent = format(to); if (countTimers) countTimers.delete(el); }diff --git a/tests/ui-juice.test.js b/tests/ui-juice.test.jsindex aaf4b7503..8f6247b32 100644--- a/tests/ui-juice.test.js+++ b/tests/ui-juice.test.js@@ -45,6 +45,56 @@ describe('countUp — instant path (no requestAnimationFrame in node)', () => {});});+describe('countUp — animated path never paints outside [from, to]', () => {+ // requestAnimationFrame is not contractually on performance.now()'s time origin.+ // Polyfills and some embedders hand the callback a Date-derived timestamp, which+ // can sit *behind* a performance.now() start — driving the eased progress negative+ // and overshooting wildly (the real "-42797% there" seen on the $THREE gate).+ // Every frame must stay clamped between the endpoints regardless of clock skew.+ // Install a manually-pumped rAF so frames fire on demand with the timestamps we+ // choose, and stays installed for the frames countUp schedules from inside step().+ function withManualRaf(fn) {+ const realRaf = globalThis.requestAnimationFrame;+ const realCancel = globalThis.cancelAnimationFrame;+ let pending = null;+ globalThis.requestAnimationFrame = (cb) => { pending = cb; return 1; };+ globalThis.cancelAnimationFrame = () => { pending = null; };+ const frame = (t) => { const cb = pending; pending = null; if (cb) cb(t); };+ try { return fn(frame); } finally {+ globalThis.requestAnimationFrame = realRaf;+ globalThis.cancelAnimationFrame = realCancel;+ }+ }++ it('clamps a frame whose timestamp trails the start clock', () => {+ withManualRaf((frame) => {+ const el = { textContent: '', dataset: {} };+ const seen = [];+ countUp(el, 0, 40, { duration: 400, format: (n) => { seen.push(n); return String(Math.round(n)); } });+ // A Date-style timestamp far behind a performance.now() start. Pre-fix this+ // drove p negative and painted a large negative number.+ frame(0);+ frame(1);+ for (const n of seen) {+ expect(n).toBeGreaterThanOrEqual(0);+ expect(n).toBeLessThanOrEqual(40);+ }+ expect(Number(el.textContent)).toBeGreaterThanOrEqual(0);+ expect(Number(el.textContent)).toBeLessThanOrEqual(40);+ });+ });++ it('lands exactly on the target once the duration elapses', () => {+ withManualRaf((frame) => {+ const el = { textContent: '', dataset: {} };+ countUp(el, 0, 40, { duration: 400, format: (n) => String(Math.round(n)) });+ frame(1000); // first frame seeds the start+ frame(1000 + 500); // past `duration` → p === 1+ expect(el.textContent).toBe('40');+ });+ });+});+describe('updateValue', () => {it('counts from the element\'s last tracked value to the new one', () => {// flash:false isolates the count math from the DOM tint pulse