|
90 | 90 | import { getGeoContext } from '$lib/contexts/geo.js'; |
91 | 91 | import { resolveColorProp, resolveStyleProp } from '$lib/utils/dataProp.js'; |
92 | 92 | import Path, { type PathProps } from './Path.svelte'; |
93 | | - import { |
94 | | - createMotion, |
95 | | - extractTweenConfig, |
96 | | - type ResolvedMotion, |
97 | | - } from '$lib/utils/motion.svelte.js'; |
| 93 | + import { createMotion, extractTweenConfig } from '$lib/utils/motion.svelte.js'; |
98 | 94 |
|
99 | 95 | const ctx = getChartContext(); |
100 | 96 | const geo = getGeoContext(); |
|
231 | 227 | })); |
232 | 228 | }); |
233 | 229 |
|
234 | | - const extractedTween = extractTweenConfig(motion); |
235 | | -
|
236 | | - const tweenOptions: ResolvedMotion | undefined = extractedTween |
237 | | - ? { |
238 | | - type: extractedTween.type, |
239 | | - options: { |
240 | | - interpolate: interpolatePath, |
241 | | - ...extractedTween.options, |
242 | | - }, |
243 | | - } |
244 | | - : undefined; |
245 | | -
|
246 | 230 | /** |
247 | 231 | * Provide initial `0` horizontal baseline so the line animates up from y=0 on mount. |
248 | 232 | * Computes a proper flattened path using d3-line with all y-values at baseline. |
249 | 233 | */ |
250 | 234 | function defaultPathData() { |
251 | | - if (!tweenOptions) return ''; |
| 235 | + // Skip baseline computation when motion is not initially enabled (faster initial render) |
| 236 | + if (!extractTweenConfig(motion)) return ''; |
252 | 237 |
|
253 | 238 | if (ctx.config.x) { |
254 | 239 | const resolvedData = data ?? series?.data ?? ctx.data; |
|
271 | 256 | return ''; |
272 | 257 | } |
273 | 258 |
|
274 | | - const tweenState = createMotion(defaultPathData(), () => d, tweenOptions); |
| 259 | + // Always create tween motion so it's ready when motion is toggled on |
| 260 | + const tweenState = createMotion(defaultPathData(), () => d, { |
| 261 | + type: 'tween', |
| 262 | + interpolate: interpolatePath, |
| 263 | + }); |
| 264 | +
|
| 265 | + /** Reactively check whether motion is enabled */ |
| 266 | + const isTweened = $derived(extractTweenConfig(motion) != null); |
275 | 267 |
|
276 | 268 | const seriesOpacity = $derived( |
277 | 269 | series?.key == null || |
|
298 | 290 | {/each} |
299 | 291 | {:else} |
300 | 292 | <Path |
301 | | - pathData={tweenOptions ? tweenState.current : d} |
| 293 | + pathData={isTweened ? tweenState.current : d} |
302 | 294 | stroke={(typeof stroke === 'string' ? stroke : undefined) ?? series?.color} |
303 | 295 | fill={typeof fill === 'string' ? fill : undefined} |
304 | 296 | {...series?.props} |
|
0 commit comments