Skip to content

Commit ba6b647

Browse files
Merge pull request #222 from TanStack/taren/motion-clock-origin
fix: start motion timing when updates are scheduled
2 parents f29d649 + 5dcc33a commit ba6b647

15 files changed

Lines changed: 131 additions & 17 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/charts': patch
3+
---
4+
5+
Measure animation elapsed time from when an update is scheduled, so early or delayed first frames do not shift its timing.

‎benchmarks/comparison/bundle-baseline.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 4,
3-
"generatedAt": "2026-10-13T21:06:51.663Z",
3+
"generatedAt": "2026-10-13T23:35:26.997Z",
44
"packageVersions": {
55
"tanstack": "1.1.0",
66
"chartjs": "4.5.1",
@@ -11,8 +11,8 @@
1111
"sources": {
1212
"tanstack": {
1313
"kind": "workspace",
14-
"revision": "3b8e03cf7225aa61ca53f85323abc99b796aa7b4",
15-
"inputDigest": "sha256:6ffd46993814f072a66eaf1e8e31ab609fd6f2ccaa4db4babc2d84919ae6d77e"
14+
"revision": "a691036b07655adda97aa0dc3052428989e2b811",
15+
"inputDigest": "sha256:390854b69e6ac7c4453ba8bcc75dfd5240f70bbd9f69efdc8d25c7fba528d951"
1616
},
1717
"chartjs": {
1818
"kind": "package",

‎benchmarks/conformance/cases/114-spring-line-motion/tanstack.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ function firstPathY(path: SVGPathElement | null) {
258258
}
259259

260260
function installManagedFrames() {
261+
const now = vi.spyOn(window.performance, 'now').mockReturnValue(0)
261262
const callbacks = new Map<number, FrameRequestCallback>()
262263
let handle = 0
263264
const request = vi
@@ -278,9 +279,11 @@ function installManagedFrames() {
278279
[number, FrameRequestCallback] | undefined
279280
if (!next) throw new Error(`No animation frame scheduled at ${time}ms`)
280281
callbacks.delete(next[0])
282+
now.mockReturnValue(time)
281283
next[1](time)
282284
},
283285
restore() {
286+
now.mockRestore()
284287
request.mockRestore()
285288
cancel.mockRestore()
286289
},

‎benchmarks/conformance/cases/115-definition-motion/tanstack.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ function requiredLabel(
324324
}
325325

326326
function installManagedFrames() {
327+
const now = vi.spyOn(window.performance, 'now').mockReturnValue(0)
327328
const callbacks = new Map<number, FrameRequestCallback>()
328329
let handle = 0
329330
const request = vi
@@ -344,9 +345,11 @@ function installManagedFrames() {
344345
[number, FrameRequestCallback] | undefined
345346
if (!next) throw new Error(`No animation frame scheduled at ${time}ms`)
346347
callbacks.delete(next[0])
348+
now.mockReturnValue(time)
347349
next[1](time)
348350
},
349351
restore() {
352+
now.mockRestore()
350353
request.mockRestore()
351354
cancel.mockRestore()
352355
},

‎benchmarks/conformance/cases/116-geometry-morph/tanstack.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ function firstPathX(path: SVGPathElement) {
307307
}
308308

309309
function installManagedFrames() {
310+
const now = vi.spyOn(window.performance, 'now').mockReturnValue(0)
310311
const callbacks = new Map<number, FrameRequestCallback>()
311312
let handle = 0
312313
const request = vi
@@ -327,9 +328,11 @@ function installManagedFrames() {
327328
[number, FrameRequestCallback] | undefined
328329
if (!next) throw new Error(`No animation frame scheduled at ${time}ms`)
329330
callbacks.delete(next[0])
331+
now.mockReturnValue(time)
330332
next[1](time)
331333
},
332334
restore() {
335+
now.mockRestore()
333336
request.mockRestore()
334337
cancel.mockRestore()
335338
},

‎benchmarks/conformance/cases/126-drillable-sunburst/tanstack.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ function expectCenteredSectorPath(path: string | null) {
324324
}
325325

326326
function installManagedFrames() {
327+
const now = vi.spyOn(window.performance, 'now').mockReturnValue(0)
327328
const callbacks = new Map<number, FrameRequestCallback>()
328329
let handle = 0
329330
const request = vi
@@ -344,9 +345,11 @@ function installManagedFrames() {
344345
[number, FrameRequestCallback] | undefined
345346
if (!next) throw new Error(`No animation frame scheduled at ${time}ms`)
346347
callbacks.delete(next[0])
348+
now.mockReturnValue(time)
347349
next[1](time)
348350
},
349351
restore() {
352+
now.mockRestore()
350353
request.mockRestore()
351354
cancel.mockRestore()
352355
},

‎benchmarks/conformance/previews/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"width": 288,
44
"height": 192,
5-
"sourceHash": "ec502379efad9516f7a756cb1f5615957b0fbc7f37515835add01b2b36728572",
5+
"sourceHash": "5cb9f7c0512bf98f18bb88dbb7c2ec524b0239530a64d5a9b1d5a00ab5f9b3d3",
66
"assets": [
77
{
88
"id": "01-line-gaps",

‎docs/comparison.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ turning untested behavior into a checkmark.
1212

1313
| Library | Package | Measured source |
1414
| -------------------------------------------------------------------------------------- | -------------------- | ------------------- |
15-
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `3b8e03c` |
15+
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `a691036` |
1616
| [Chart.js](https://www.chartjs.org/docs/latest/) | `chart.js` | npm `4.5.1` |
1717
| [Apache ECharts](https://echarts.apache.org/handbook/en/best-practices/canvas-vs-svg/) | `echarts` | npm `6.1.0` |
1818
| [Recharts](https://recharts.github.io/en-US/) | `recharts` | npm `3.10.1` |
1919
| [Observable Plot](https://observablehq.com/plot/features/plots) | `@observablehq/plot` | npm `0.6.17` |
2020

2121
The competitor versions are exact package pins, not latest versions inferred
22-
at page render time. The measured TanStack workspace revision is `3b8e03c`.
22+
at page render time. The measured TanStack workspace revision is `a691036`.
2323

2424
## Capability matrix
2525

‎packages/charts-core/docs/comparison.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ turning untested behavior into a checkmark.
1212

1313
| Library | Package | Measured source |
1414
| -------------------------------------------------------------------------------------- | -------------------- | ------------------- |
15-
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `3b8e03c` |
15+
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `a691036` |
1616
| [Chart.js](https://www.chartjs.org/docs/latest/) | `chart.js` | npm `4.5.1` |
1717
| [Apache ECharts](https://echarts.apache.org/handbook/en/best-practices/canvas-vs-svg/) | `echarts` | npm `6.1.0` |
1818
| [Recharts](https://recharts.github.io/en-US/) | `recharts` | npm `3.10.1` |
1919
| [Observable Plot](https://observablehq.com/plot/features/plots) | `@observablehq/plot` | npm `0.6.17` |
2020

2121
The competitor versions are exact package pins, not latest versions inferred
22-
at page render time. The measured TanStack workspace revision is `3b8e03c`.
22+
at page render time. The measured TanStack workspace revision is `a691036`.
2323

2424
## Capability matrix
2525

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { scaleBand, scaleLinear } from 'd3-scale'
3+
import { barY } from './bar'
4+
import { motion } from './motion'
5+
import { createChartScene, defineChart } from './scene'
6+
7+
describe('motion run clock', () => {
8+
it.each([
9+
{ firstFrame: 900, delay: 0, observedAt: 1500 },
10+
{ firstFrame: 1250, delay: 0, observedAt: 1500 },
11+
{ firstFrame: 900, delay: 100, observedAt: 1600 },
12+
])(
13+
'measures elapsed time from scheduling: %j',
14+
({ firstFrame, delay, observedAt }) => {
15+
const callbacks = new Map<number, FrameRequestCallback>()
16+
let handle = 0
17+
const now = vi.spyOn(window.performance, 'now').mockReturnValue(1000)
18+
const request = vi
19+
.spyOn(window, 'requestAnimationFrame')
20+
.mockImplementation(function (this: Window, callback) {
21+
expect(this).toBe(window)
22+
callbacks.set(++handle, callback)
23+
return handle
24+
})
25+
const cancel = vi
26+
.spyOn(window, 'cancelAnimationFrame')
27+
.mockImplementation(function (this: Window, id) {
28+
expect(this).toBe(window)
29+
if (id !== null && id !== undefined) callbacks.delete(id)
30+
})
31+
const container = document.createElement('div')
32+
const surface = motion({
33+
transition: { type: 'tween', duration: 1000, easing: 'linear' },
34+
}).mount(container, () => {})
35+
const scene = createChartScene(
36+
defineChart({
37+
marks: [
38+
barY([{ category: 'A', value: 100 }], {
39+
x: 'category',
40+
y: 'value',
41+
motion: { delay },
42+
}),
43+
],
44+
scales: {
45+
x: { scale: scaleBand },
46+
y: { scale: scaleLinear().domain([0, 100]) },
47+
},
48+
guides: false,
49+
margin: 0,
50+
}),
51+
{ width: 300, height: 200 },
52+
)
53+
const frame = (time: number) => {
54+
const pending = [...callbacks.values()]
55+
callbacks.clear()
56+
expect(pending).not.toHaveLength(0)
57+
pending.forEach((callback) => callback(time))
58+
}
59+
try {
60+
surface.render(scene, { ariaLabel: 'Scheduled motion' })
61+
frame(firstFrame)
62+
const bar = container.querySelector('rect')!
63+
if (firstFrame < 1000 + delay)
64+
expect(Number(bar.getAttribute('height'))).toBe(0)
65+
frame(observedAt)
66+
expect(Number(bar.getAttribute('height'))).toBeCloseTo(100)
67+
frame(2000 + delay)
68+
expect(Number(bar.getAttribute('height'))).toBe(200)
69+
expect(container.querySelector('svg')!.dataset.tsMotionState).toBe(
70+
'finished',
71+
)
72+
} finally {
73+
surface.destroy()
74+
now.mockRestore()
75+
request.mockRestore()
76+
cancel.mockRestore()
77+
}
78+
},
79+
)
80+
})

0 commit comments

Comments
 (0)