Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | import { useRef, useCallback } from 'react' import type { NumberLineState } from '../../types' import { goldenRatioDemoViewport } from './goldenRatioDemo' import { piDemoViewport } from './piDemo' import { tauDemoViewport } from './tauDemo' import { eDemoViewport } from './eDemo' import { gammaDemoViewport } from './gammaDemo' import { sqrt2DemoViewport } from './sqrt2Demo' import { sqrt3DemoViewport } from './sqrt3Demo' import { ln2DemoViewport } from './ln2Demo' import { ramanujanDemoViewport } from './ramanujanDemo' import { feigenbaumDemoViewport } from './feigenbaumDemo' import { lerpViewport, snapViewport, computeViewportDeviation, FADE_IN_MS, FADE_OUT_MS, } from '../../viewportAnimation' export type DemoPhase = 'idle' | 'animating' | 'presenting' | 'fading' export interface DemoState { phase: DemoPhase constantId: string | null /** 0-1 progress for subdivision reveal */ revealProgress: number /** 0-1 overall overlay opacity */ opacity: number } const INITIAL_STATE: DemoState = { phase: 'idle', constantId: null, revealProgress: 0, opacity: 0, } // --- Animation timing --- /** Duration of viewport fly-in animation (ms) */ const VIEWPORT_ANIM_MS = 1200 /** Duration of subdivision reveal after viewport arrives (ms) */ const REVEAL_ANIM_MS = 15000 /** How far the user can deviate before the demo fades out (fraction of viewport) */ const DEVIATION_THRESHOLD = 0.4 import { CONSTANT_IDS } from '../../talkToNumber/explorationRegistry' /** Constants that have demos available */ const DEMO_AVAILABLE = CONSTANT_IDS /** * Dynamic demo viewport registry — allows non-constant demos (e.g. LCM Hopper) * to register their viewport target at runtime. * * Call `DYNAMIC_DEMO_VIEWPORTS.set(id, fn)` before `startDemo(id)`. */ export const DYNAMIC_DEMO_VIEWPORTS = new Map< string, (cssWidth: number, cssHeight: number) => { center: number; pixelsPerUnit: number } >() /** Check if a demo is available (static constant OR dynamically registered). */ function isDemoAvailable(id: string): boolean { return DEMO_AVAILABLE.has(id) || DYNAMIC_DEMO_VIEWPORTS.has(id) } /** Compute the target viewport for a given constant's demo. */ function getDemoViewport( constantId: string, cssWidth: number, cssHeight: number ): { center: number; pixelsPerUnit: number } { // Check dynamic viewports first (e.g. LCM hopper with variable LCM) const dynamicFn = DYNAMIC_DEMO_VIEWPORTS.get(constantId) if (dynamicFn) return dynamicFn(cssWidth, cssHeight) if (constantId === 'phi') return goldenRatioDemoViewport(cssWidth, cssHeight) if (constantId === 'pi') return piDemoViewport(cssWidth, cssHeight) if (constantId === 'tau') return tauDemoViewport(cssWidth, cssHeight) if (constantId === 'e') return eDemoViewport(cssWidth, cssHeight) if (constantId === 'gamma') return gammaDemoViewport(cssWidth, cssHeight) if (constantId === 'sqrt2') return sqrt2DemoViewport(cssWidth, cssHeight) if (constantId === 'sqrt3') return sqrt3DemoViewport(cssWidth, cssHeight) if (constantId === 'ln2') return ln2DemoViewport(cssWidth, cssHeight) if (constantId === 'ramanujan') return ramanujanDemoViewport(cssWidth, cssHeight) if (constantId === 'feigenbaum') return feigenbaumDemoViewport(cssWidth, cssHeight) return { center: 0, pixelsPerUnit: 100 } } // ── Viewport zoom keyframes ──────────────────────────────────────── // // Demos that zoom into their constant's decimal expansion define // keyframes here. The tick function interpolates through them, // driving the ACTUAL number-line viewport (center + pixelsPerUnit). interface ViewportKeyframe { /** revealProgress at which this viewport should be reached */ progress: number center: number pixelsPerUnit: number } /** Compute PPU needed to fit a [lo, hi] range across 75% of the screen width. */ function ppuForRange(cssWidth: number, lo: number, hi: number): number { return (cssWidth * 0.75) / (hi - lo) } /** * Build zoom keyframes for a given demo. * Returns null if the demo has no zoom phase. */ function getZoomKeyframes( constantId: string, cssWidth: number, cssHeight: number ): ViewportKeyframe[] | null { if (constantId === 'sqrt2') { const base = sqrt2DemoViewport(cssWidth, cssHeight) return [ // Start at base viewport (matches the demo's initial fly-in target) { progress: 0.8, ...base }, // Progressive zoom into √2 ≈ 1.4142135623730951... { progress: 0.82, center: 1.5, pixelsPerUnit: ppuForRange(cssWidth, 1, 2) }, { progress: 0.835, center: 1.45, pixelsPerUnit: ppuForRange(cssWidth, 1.4, 1.5) }, { progress: 0.85, center: 1.415, pixelsPerUnit: ppuForRange(cssWidth, 1.41, 1.42) }, { progress: 0.865, center: 1.4145, pixelsPerUnit: ppuForRange(cssWidth, 1.414, 1.415) }, { progress: 0.88, center: 1.41425, pixelsPerUnit: ppuForRange(cssWidth, 1.4142, 1.4143) }, { progress: 0.895, center: 1.414215, pixelsPerUnit: ppuForRange(cssWidth, 1.41421, 1.41422) }, { progress: 0.91, center: 1.4142135, pixelsPerUnit: ppuForRange(cssWidth, 1.414213, 1.414214), }, // Zoom back out for the reveal phase (√2 label, star, formula) { progress: 0.935, ...base }, ] } if (constantId === 'sqrt3') { const base = sqrt3DemoViewport(cssWidth, cssHeight) return [ // Start at base viewport (matches the demo's initial fly-in target) { progress: 0.8, ...base }, // Progressive zoom into √3 ≈ 1.7320508075688772... { progress: 0.82, center: 2, pixelsPerUnit: ppuForRange(cssWidth, 1, 2) }, { progress: 0.835, center: 1.75, pixelsPerUnit: ppuForRange(cssWidth, 1.7, 1.8) }, { progress: 0.85, center: 1.735, pixelsPerUnit: ppuForRange(cssWidth, 1.73, 1.74) }, { progress: 0.865, center: 1.7325, pixelsPerUnit: ppuForRange(cssWidth, 1.732, 1.733) }, { progress: 0.88, center: 1.73205, pixelsPerUnit: ppuForRange(cssWidth, 1.732, 1.7321) }, { progress: 0.895, center: 1.732052, pixelsPerUnit: ppuForRange(cssWidth, 1.73205, 1.73206) }, { progress: 0.91, center: 1.7320508, pixelsPerUnit: ppuForRange(cssWidth, 1.73205, 1.732051), }, // Zoom back out for the reveal phase (√3 label, star, formula) { progress: 0.935, ...base }, ] } if (constantId === 'pi') { const base = piDemoViewport(cssWidth, cssHeight) return [ { progress: 0.9, ...base }, // Progressive zoom into π ≈ 3.14159265358979... { progress: 0.915, center: 3.5, pixelsPerUnit: ppuForRange(cssWidth, 3, 4) }, { progress: 0.93, center: 3.15, pixelsPerUnit: ppuForRange(cssWidth, 3.1, 3.2) }, { progress: 0.945, center: 3.145, pixelsPerUnit: ppuForRange(cssWidth, 3.14, 3.15) }, { progress: 0.96, center: 3.1415, pixelsPerUnit: ppuForRange(cssWidth, 3.141, 3.142) }, { progress: 0.975, center: 3.14155, pixelsPerUnit: ppuForRange(cssWidth, 3.1415, 3.1416) }, { progress: 0.99, center: 3.141595, pixelsPerUnit: ppuForRange(cssWidth, 3.14159, 3.1416) }, { progress: 1.0, center: 3.1415925, pixelsPerUnit: ppuForRange(cssWidth, 3.141592, 3.141593), }, ] } if (constantId === 'ln2') { const base = ln2DemoViewport(cssWidth, cssHeight) // Zoom in during seg 2 (more bounces) then HOLD for seg 3+4 so the // spiraling convergence is apparent from the arcs shrinking, not the // viewport. Zoom back out for the reveal labels. return [ // Hold base viewport through seg 0 + seg 1 (place + first bounces) { progress: 0.0, ...base }, { progress: 0.35, ...base }, // Seg 2: zoom in as bounces 5–12 get smaller { progress: 0.45, center: 0.65, pixelsPerUnit: ppuForRange(cssWidth, 0.3, 1.0) }, { progress: 0.55, center: 0.69, pixelsPerUnit: ppuForRange(cssWidth, 0.45, 0.85) }, // Seg 3 + 4: hold steady — let the tiny arcs show convergence { progress: 0.86, center: 0.69, pixelsPerUnit: ppuForRange(cssWidth, 0.45, 0.85) }, // Seg 5: zoom back out for reveal labels { progress: 0.9, center: 0.55, pixelsPerUnit: ppuForRange(cssWidth, 0.15, 1.05) }, { progress: 1.0, center: 0.55, pixelsPerUnit: ppuForRange(cssWidth, 0.15, 1.05) }, ] } if (constantId === 'feigenbaum') { const base = feigenbaumDemoViewport(cssWidth, cssHeight) return [ // Seg 0–1: Focus on iteration track at r=2.8 { progress: 0.0, ...base }, // Seg 2: Sliding dial — shift center right { progress: 0.14, center: 2.9, pixelsPerUnit: ppuForRange(cssWidth, 2.5, 3.3) }, // Seg 3: Approaching split point { progress: 0.2, center: 3.1, pixelsPerUnit: ppuForRange(cssWidth, 2.7, 3.5) }, // Seg 3: First split at r=3.2 { progress: 0.3, center: 3.1, pixelsPerUnit: ppuForRange(cssWidth, 2.8, 3.5) }, // Seg 5: Moving to r=3.5 { progress: 0.37, center: 3.3, pixelsPerUnit: ppuForRange(cssWidth, 2.9, 3.7) }, // Seg 6: Cascade region { progress: 0.45, center: 3.45, pixelsPerUnit: ppuForRange(cssWidth, 3.0, 3.6) }, // Seg 7: Zoom out for full diagram { progress: 0.56, center: 3.1, pixelsPerUnit: ppuForRange(cssWidth, 2.4, 3.7) }, // Seg 7: Hold for diagram { progress: 0.64, center: 3.1, pixelsPerUnit: ppuForRange(cssWidth, 2.4, 3.7) }, // Seg 8: Zoom in for gap bars { progress: 0.7, center: 3.25, pixelsPerUnit: ppuForRange(cssWidth, 2.9, 3.6) }, // Seg 9: Hold on gaps region for tiling visual { progress: 0.82, center: 3.25, pixelsPerUnit: ppuForRange(cssWidth, 2.9, 3.6) }, // Seg 10a: Zoom into the right end of the fractal — cascade region { progress: 0.85, center: 3.55, pixelsPerUnit: ppuForRange(cssWidth, 3.44, 3.58) }, // Seg 10a: Hold zoom — let the fractal detail sink in { progress: 0.905, center: 3.55, pixelsPerUnit: ppuForRange(cssWidth, 3.44, 3.58) }, // Seg 10b: Pan right to delta { progress: 0.94, center: 4.669, pixelsPerUnit: ppuForRange(cssWidth, 4.0, 5.3) }, { progress: 1.0, center: 4.669, pixelsPerUnit: ppuForRange(cssWidth, 4.0, 5.3) }, ] } if (constantId === 'ramanujan') { const base = ramanujanDemoViewport(cssWidth, cssHeight) return [ // Act 1: divergence [0,15] — hold wide view for racing dot { progress: 0.0, ...base }, { progress: 0.075, ...base }, // Hold through diverge + early hook // Act 2: harmonic/square convergence — zoom to s-parameter view [0,4] { progress: 0.1, center: 2, pixelsPerUnit: ppuForRange(cssWidth, 0, 4) }, // Act 3: knob/curve — widen to see s=2,3,4 points [−0.5,6.5] { progress: 0.26, center: 3, pixelsPerUnit: ppuForRange(cssWidth, -0.5, 6.5) }, // Act 4: pole — hold view for curve + approaching wall [−1,6] { progress: 0.42, center: 2.5, pixelsPerUnit: ppuForRange(cssWidth, -1, 6) }, // Act 5: bridge — failed attempts + smooth continuation [−3,5] { progress: 0.52, center: 1, pixelsPerUnit: ppuForRange(cssWidth, -3, 5) }, // Act 5→6 transition: crossing zero, flipper concept [−3,3] { progress: 0.65, center: 0, pixelsPerUnit: ppuForRange(cssWidth, -3, 3) }, // Act 6: flipper + connect — see both sides [−2,2] { progress: 0.7, center: 0, pixelsPerUnit: ppuForRange(cssWidth, -2, 2) }, // Act 7: volcano — tight on s=−1 [−2,1] { progress: 0.81, center: -0.5, pixelsPerUnit: ppuForRange(cssWidth, -2, 1) }, // Act 7: trust — zoom OUT to see convergent points at s=2,3,4 [−1.5,5] { progress: 0.85, center: 1.75, pixelsPerUnit: ppuForRange(cssWidth, -1.5, 5) }, // Act 7: reveal — zoom back tight for −1/12 starburst [−2,1] { progress: 0.895, center: -0.5, pixelsPerUnit: ppuForRange(cssWidth, -2, 1) }, // Act 7: hold tight for recap + ghost note { progress: 1.0, center: -0.5, pixelsPerUnit: ppuForRange(cssWidth, -2, 1) }, ] } return null } /** * Interpolate between viewport keyframes at the given progress. * Returns null if progress is before the first keyframe. * Uses smoothstep easing and log-space PPU interpolation for smooth zoom. */ function interpolateViewportKeyframes( keyframes: ViewportKeyframe[], progress: number ): { center: number; pixelsPerUnit: number } | null { if (keyframes.length === 0 || progress < keyframes[0].progress) return null // Past the last keyframe — hold at last value if (progress >= keyframes[keyframes.length - 1].progress) { const last = keyframes[keyframes.length - 1] return { center: last.center, pixelsPerUnit: last.pixelsPerUnit } } // Find the two keyframes we're between for (let i = 0; i < keyframes.length - 1; i++) { const a = keyframes[i] const b = keyframes[i + 1] if (progress >= a.progress && progress < b.progress) { const t = (progress - a.progress) / (b.progress - a.progress) // Smoothstep easing const st = t * t * (3 - 2 * t) // Linear center interpolation const center = a.center + (b.center - a.center) * st // Log-space PPU interpolation for perceptually smooth zoom const logA = Math.log(a.pixelsPerUnit) const logB = Math.log(b.pixelsPerUnit) const pixelsPerUnit = Math.exp(logA + (logB - logA) * st) return { center, pixelsPerUnit } } } return null } /** * Hook managing the constant demonstration lifecycle. * * State machine: idle → animating → presenting → fading → idle * * During 'animating', the viewport is smoothly interpolated to the target * and subdivisions are revealed progressively. During 'presenting', the * user observes the end result. If they pan/zoom beyond a threshold, the * overlay fades out ('fading') and then returns to 'idle'. */ export function useConstantDemo( stateRef: React.MutableRefObject<NumberLineState>, cssWidthRef: React.MutableRefObject<number>, cssHeightRef: React.MutableRefObject<number>, onRedraw: () => void ): { demoState: React.MutableRefObject<DemoState> startDemo: (constantId: string) => void /** Restore a demo at a specific progress (skip fly-in, start paused) */ restoreDemo: (constantId: string, progress: number) => void /** Called every frame from draw() to update animation and detect deviation */ tickDemo: () => void cancelDemo: () => void /** Pauses auto-play and sets revealProgress to the given value (0-1) */ setRevealProgress: (value: number) => void /** Signal that the user has zoomed/panned — demo will fade out */ markUserInteraction: () => void } { const demoStateRef = useRef<DemoState>({ ...INITIAL_STATE }) const animStartRef = useRef(0) const targetViewportRef = useRef({ center: 0, pixelsPerUnit: 100 }) const sourceViewportRef = useRef({ center: 0, pixelsPerUnit: 100 }) const rafRef = useRef<number>(0) const fadeStartRef = useRef(0) const isPausedRef = useRef(false) const userInteractedRef = useRef(false) const stopLoop = useCallback(() => { if (rafRef.current) { cancelAnimationFrame(rafRef.current) rafRef.current = 0 } }, []) const startLoop = useCallback(() => { if (rafRef.current) return const tick = () => { onRedraw() rafRef.current = requestAnimationFrame(tick) } rafRef.current = requestAnimationFrame(tick) }, [onRedraw]) const startDemo = useCallback( (constantId: string) => { if (!isDemoAvailable(constantId)) return const target = getDemoViewport(constantId, cssWidthRef.current, cssHeightRef.current) // Store source viewport for interpolation sourceViewportRef.current = { center: stateRef.current.center, pixelsPerUnit: stateRef.current.pixelsPerUnit, } targetViewportRef.current = target demoStateRef.current = { phase: 'animating', constantId, revealProgress: 0, opacity: 0, } isPausedRef.current = false animStartRef.current = performance.now() startLoop() }, [stateRef, cssWidthRef, cssHeightRef, startLoop] ) const cancelDemo = useCallback(() => { demoStateRef.current = { ...INITIAL_STATE } stopLoop() }, [stopLoop]) /** * Called every frame from draw() to: * 1. Animate viewport during 'animating' phase * 2. Update reveal progress * 3. Detect user deviation during 'presenting' phase * 4. Handle fade-out timing */ const tickDemo = useCallback(() => { const ds = demoStateRef.current if (ds.phase === 'idle') return const now = performance.now() const elapsed = now - animStartRef.current if (ds.phase === 'animating') { // User zoomed/panned on the number line → dismiss the demo. // Checked BEFORE lerpViewport/zoom-keyframes so the user's // viewport position is preserved (not overwritten). if (userInteractedRef.current) { userInteractedRef.current = false ds.phase = 'fading' // Start the fade from the current opacity (may be < 1 during fly-in) fadeStartRef.current = now - (1 - ds.opacity) * FADE_OUT_MS return } // When paused (narration driving progress, or user scrubbed), // don't override the viewport — setRevealProgress() handles positioning. if (isPausedRef.current) { // When reveal is complete, transition to 'presenting' so // deviation detection continues via the normal path. if (ds.revealProgress >= 1) { ds.phase = 'presenting' ds.opacity = 1 snapViewport(targetViewportRef.current, stateRef.current) } return } // --- Normal auto-play flow --- // Fade in ds.opacity = Math.min(1, elapsed / FADE_IN_MS) // Viewport interpolation const vpT = lerpViewport( sourceViewportRef.current, targetViewportRef.current, elapsed, VIEWPORT_ANIM_MS, stateRef.current ) const revealStart = VIEWPORT_ANIM_MS * 0.6 if (elapsed > revealStart) { ds.revealProgress = Math.min(1, (elapsed - revealStart) / REVEAL_ANIM_MS) } // Transition to presenting when both viewport and reveal are done const totalDuration = revealStart + REVEAL_ANIM_MS if (elapsed >= totalDuration) { ds.phase = 'presenting' ds.revealProgress = 1 ds.opacity = 1 snapViewport(targetViewportRef.current, stateRef.current) } // Suppress unused variable warning — vpT is used implicitly // (lerpViewport mutates stateRef; vpT available for future use) void vpT // Apply zoom keyframes — overrides lerpViewport for demos with // irrationality zoom phases (√2, π). The keyframes drive the // actual number-line viewport to zoom into the constant's position. if (ds.constantId) { const kfs = getZoomKeyframes(ds.constantId, cssWidthRef.current, cssHeightRef.current) if (kfs) { const zoomVp = interpolateViewportKeyframes(kfs, ds.revealProgress) if (zoomVp) { stateRef.current.center = zoomVp.center stateRef.current.pixelsPerUnit = zoomVp.pixelsPerUnit // Update source+target so lerpViewport converges here next frame // and deviation detection uses the zoom viewport as reference sourceViewportRef.current = { ...zoomVp } targetViewportRef.current = { ...zoomVp } } } } } else if (ds.phase === 'presenting') { // User zoomed/panned → dismiss if (userInteractedRef.current) { userInteractedRef.current = false ds.phase = 'fading' fadeStartRef.current = now - (1 - ds.opacity) * FADE_OUT_MS return } const deviation = computeViewportDeviation(stateRef.current, targetViewportRef.current) if (deviation > DEVIATION_THRESHOLD) { ds.phase = 'fading' fadeStartRef.current = now ds.opacity = 1 } } else if (ds.phase === 'fading') { const fadeElapsed = now - fadeStartRef.current ds.opacity = Math.max(0, 1 - fadeElapsed / FADE_OUT_MS) if (ds.opacity <= 0) { demoStateRef.current = { ...INITIAL_STATE } stopLoop() return } } }, [stateRef, stopLoop, cssWidthRef, cssHeightRef]) const setRevealProgress = useCallback( (value: number) => { const ds = demoStateRef.current if (ds.phase === 'idle') return isPausedRef.current = true ds.revealProgress = Math.max(0, Math.min(1, value)) // Apply zoom keyframes if scrubbing into a zoom phase let handled = false if (ds.constantId) { const kfs = getZoomKeyframes(ds.constantId, cssWidthRef.current, cssHeightRef.current) if (kfs) { const zoomVp = interpolateViewportKeyframes(kfs, ds.revealProgress) if (zoomVp) { stateRef.current.center = zoomVp.center stateRef.current.pixelsPerUnit = zoomVp.pixelsPerUnit sourceViewportRef.current = { ...zoomVp } targetViewportRef.current = { ...zoomVp } handled = true } } } if (!handled) { // Snap viewport to target (skip interpolation) when scrubbing snapViewport(targetViewportRef.current, stateRef.current) } // Ensure opacity is full while scrubbing ds.opacity = 1 // If we were fading, go back to animating so the overlay stays visible if (ds.phase === 'fading') { ds.phase = 'animating' } onRedraw() }, [stateRef, onRedraw, cssWidthRef, cssHeightRef] ) /** * Restore a demo at a specific progress point (e.g. from URL params on reload). * Unlike startDemo, this skips the fly-in animation, snaps the viewport * immediately, and starts paused so the user can press play when ready. */ const restoreDemo = useCallback( (constantId: string, progress: number) => { if (!isDemoAvailable(constantId)) return const target = getDemoViewport(constantId, cssWidthRef.current, cssHeightRef.current) // Snap viewport (skip fly-in animation) sourceViewportRef.current = { ...target } targetViewportRef.current = { ...target } snapViewport(target, stateRef.current) // Initialize state — phase 'animating' + isPaused so tickDemo holds position demoStateRef.current = { phase: 'animating', constantId, revealProgress: 0, opacity: 1 } isPausedRef.current = true userInteractedRef.current = false animStartRef.current = performance.now() startLoop() // Delegate to setRevealProgress for zoom keyframe handling + viewport snap setRevealProgress(Math.max(0, Math.min(1, progress))) }, [stateRef, cssWidthRef, cssHeightRef, startLoop, setRevealProgress] ) const markUserInteraction = useCallback(() => { const ds = demoStateRef.current if (ds.phase === 'idle' || ds.phase === 'fading') return userInteractedRef.current = true }, []) return { demoState: demoStateRef, startDemo, restoreDemo, tickDemo, cancelDemo, setRevealProgress, markUserInteraction, } } |