[Bug] `motion()`: the run clock starts at the first frame timestamp, not at the update #192
Version: @tanstack/charts 1.1.0 (57774e1).
Description#
A motion run cannot be lined up with an external event, because its clock is the first requestAnimationFrame timestamp. After a long task that timestamp can precede the update, so a tween skips its first part or ends early.
A. Clock origin is the first rAF timestamp#
runTrackssets the origin withstart ??= time(motion.ts:3990-3991).timeis the rAF argument; the clock is not read when the run is scheduled (motion.ts:4012).- After a long task the first timestamp can precede the update, so a tween skips its first part or ends early.
- A negative
delayis clamped to 0 (motion.ts:3792-3793), so it cannot compensate.
Repro: save as packages/charts-core/src/repro.test.ts, run pnpm vitest run packages/charts-core/src/repro.test.ts (rAF and performance.now mocked; the first frame timestamp is 100 ms before the update).
import { it, expect, vi } from 'vitest'
import { scaleBand, scaleLinear } from 'd3-scale'
import { barY } from './bar'
import { motion } from './motion'
import { createChartScene, defineChart } from './scene'
it('run clock starts at update time, not first frame timestamp', () => {
const scene = createChartScene(defineChart({
marks: [barY([{ id: 'a', category: 'A', value: 100 }], { x: 'category', y: 'value', key: 'id' })],
scales: { x: { scale: scaleBand().domain(['A']) }, y: { scale: scaleLinear().domain([0, 100]) } },
guides: false,
}), { width: 300, height: 200 })
let frame: FrameRequestCallback | undefined
vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => ((frame = cb), 1))
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
vi.spyOn(window.performance, 'now').mockReturnValue(1000) // update at t0 = 1000
const container = document.createElement('div')
const surface = motion({ transition: { type: 'tween', duration: 1000, easing: 'linear' } })
.mount(container, () => {})
surface.render(scene, { ariaLabel: 'Bars' })
const rect = container.querySelector('g.ts-chart__bar-y > rect')!
frame!(900) // first frame timestamp, 100 ms before t0
frame!(1500) // 500 ms after t0
expect(Number(rect.getAttribute('height'))).toBeCloseTo(100, 0) // 50% of 200 px
})
Output: AssertionError: expected 120 to be close to 100 (600 ms of the tween played 500 ms after the update).
Expected: the run starts at the time it was scheduled.
Proposed: read performance.now() in runTracks and use it as the origin (now() exists at motion.ts:375), or add an optional startAt.
Downstream measurement (bklit migration, Chrome headless): after a long task a fade started 75-105 ms before the update and finished 40-145 ms early.
B. Spring rest thresholds are undocumented#
Moved to #212 (documentation of the renderer's public contracts).
C. "Motion finished" signal is undocumented#
Moved to #212.
toomingos(GitHub profile, opens in a new tab) changed the title
tannerlinsley closed this
Sign in to join this conversation.
Thanks @toomingos, the reproduction made this easy to pin down. Fixed in #222.
The clock now starts when the update is scheduled, using the owning window's performance clock. Early and delayed first frames no longer shift the animation's timing. Regression tests cover both cases and per-track delays, and the before/after checks pass in Chromium, Firefox and WebKit. All 19 validation tasks and CI passed.
The optional motion bundle is 0.008 kB smaller gzip and 0.066 kB smaller Brotli. No bundle budgets were raised.
This is merged on main and queued for the next patch through Changesets. It is not published to npm yet.