[BUG] Animation started late in a long synchronous task begins "already running": its start time is the task's first `time.now()` read, not the `animate()` call
Describe the bug
time.now() (frameloop/sync-time) caches one value for a whole synchronous task. If anything reads it early in a task (for example motionValue.set() at the top of a click handler) and the task then does some slow synchronous work before calling animate(), the animation's startTime is the stale cached value. The first rendered frame then shows the animation already that far in: it jumps, and then continues at normal speed.
With 50 ms of work between the early read and animate(), a 500 ms linear animation shows 50 ms of progress (10 % of its distance) on its very first frame. Without the early read, the first frame shows 0 to 3 ms, as expected.
In React this shows up without any explicit read: an element with layout / layoutId that mounts during a render calls VisualElement.scheduleRender, which reads time.now(). The layout animations then start in the microtask after the commit, stamped with that earlier time. We measured a shared-layout letter animation that started at 53.4 ms while the commit ended at 81 ms. Its first frame (86.8 ms) was already about a quarter of the way along the spring. Anything else started after the commit (for example a width animation driven by a ResizeObserver) then runs out of phase with it. You see a jump, then a near-stop.
To reproduce
A standalone page, motion only:
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/motion.js"></script>
<button id="b">animate</button>
<pre id="out"></pre>
<script>
const { animate, motionValue, time } = Motion;
const x = motionValue(0); // created up front
const other = motionValue(0);
function busy(ms) { const end = performance.now() + ms; while (performance.now() < end) {} }
document.getElementById("b").onclick = () => {
x.jump(0); // also reads time.now(), like other.set() below
other.set(other.get() + 1); // any early read of Motion's clock in this task
busy(50); // stands in for a framework render/commit
// time.set(performance.now()); // <- workaround: uncomment and the jump disappears
const tCall = performance.now();
animate(x, 100, { duration: 0.5, ease: "linear" }); // 1 px = 5 ms
const frames = [];
const sample = () => { // runs after Motion's own rAF in the same frame
frames.push(`+${(performance.now() - tCall).toFixed(1)} ms after animate(): ${(x.get() * 5).toFixed(0)} ms into the animation`);
if (frames.length < 4) requestAnimationFrame(sample);
else document.getElementById("out").textContent = frames.join("\n");
};
requestAnimationFrame(sample);
};
</script>Click the button.
Expected behavior
The first frame after animate() shows about 0 ms of progress: the animation starts when it is called, or on its first frame, as a WAAPI or CSS animation does.
Actual behavior
The first frame shows about 50 ms of progress, which is the length of the synchronous work after the early clock read. These are three clicks per case on motion 13.4.3, with a real mouse click through Playwright:
| case | Chromium 149: progress on the first frame | Firefox 151: progress on the first frame |
|---|---|---|
| no clock read before the 50 ms of work | 0 to 4 ms | 0 to 2 ms |
other.set() before the 50 ms of work |
50 to 51 ms | 50 to 51 ms |
same, plus time.set(performance.now()) just before animate() |
0 to 1 ms | 0 to 1 ms |
In every case the first frame came 0 to 6.5 ms after animate(). 12.42.2 gives about the same numbers: 50 to 52 ms in the middle row.
The delay grows with the work: it is whatever time passed between the first time.now() read in the task and the animate() call.
Cause, as far as we can tell
frameloop/sync-time:time.now()caches the first read and clears it in a microtask. Every read later in the same task returns the old value.JSAnimation.play():startTime = startTime ?? this.driver.now(). Outside a frame,frameloopDriver.now()istime.now().- Projection:
update()setsframeData.timestamp = time.now(), and the pending layout animation starts inframe.update. That's how a mount-timescheduleRenderread ends up stamping layout animations.
Maybe the fix is to take a new time when an animation starts, or to stamp a JS animation's startTime on its first processed frame (as WAAPI's pending start does). Freezing time within a task keeps animations that start together in sync, but it also puts an animation's start earlier than its first frame.
Workaround
Call time.set(performance.now()) just before starting the animation. In React, schedule it at the head of Motion's post-commit batch (microtask.setup(() => time.set(performance.now())) from a useLayoutEffect), so the layout animations and anything you start yourself share one fresh start time.
Related
#3820 (closed, wontfix) is also about startTime being chosen when the animation is created. There the trigger is DevTools playback rate, not a stale synchronous clock.
Environment
- motion 13.4.3 (latest), also 12.42.2. The same code is on
mainat 7c8156b (2026-09-25):sync-time.ts,JSAnimation.play()(startTime ?? now),frameloopDriver.now(), and the projection'sframeData.timestamp = time.now(). - Chromium 149.0.7827.55 (Playwright, headed) and Firefox 151 (Playwright, headless), macOS.
- The React observation: motion 12.42.2, React 19, development build, shared-layout (
layoutId) letters.
Source: motiondivision/motion