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 | import { useRef, useEffect, useState, useCallback } from 'react' import type { Meta, StoryObj } from '@storybook/react' import { renderSieveOverlay, computeSieveViewports, getSieveViewportState, computeSieveTickTransforms, SWEEP_MAX_N, COMPOSITION_START_MS, } from './renderSieveOverlay' import { renderNumberLine } from '../renderNumberLine' import { computeTickMarks } from '../numberLineTicks' import { computePrimeInfos } from './sieve' import { DEFAULT_TICK_THRESHOLDS } from '../types' import type { NumberLineState } from '../types' // Initial viewport matches the zoomed-in ancient-trick start const SIEVE_INITIAL_STATE: NumberLineState = { center: 13, pixelsPerUnit: 50 } const CELEBRATION_VP = { center: 55, pixelsPerUnit: 5 } const TOTAL_MS = 36000 // enough to see celebration + composition reveal // --- Storybook harness --- interface HarnessProps { width: number height: number dark: boolean speed: number autoPlay: boolean } function SieveHarness({ width, height, dark, speed, autoPlay }: HarnessProps) { const canvasRef = useRef<HTMLCanvasElement>(null) const [elapsedMs, setElapsedMs] = useState(0) const [playing, setPlaying] = useState(autoPlay) const playingRef = useRef(playing) const speedRef = useRef(speed) const lastFrameRef = useRef<number | null>(null) playingRef.current = playing speedRef.current = speed // Animation loop useEffect(() => { let raf = 0 const tick = (now: number) => { if (playingRef.current) { const dt = lastFrameRef.current !== null ? now - lastFrameRef.current : 0 setElapsedMs((prev) => Math.min(prev + dt * speedRef.current, TOTAL_MS)) } lastFrameRef.current = now raf = requestAnimationFrame(tick) } raf = requestAnimationFrame(tick) return () => cancelAnimationFrame(raf) }, []) // Cache sieve viewports once (depends on width) const sieveViewportsRef = useRef(computeSieveViewports(width, 120)) useEffect(() => { sieveViewportsRef.current = computeSieveViewports(width, 120) }, [width]) // Render useEffect(() => { const canvas = canvasRef.current if (!canvas) return const dpr = window.devicePixelRatio || 1 canvas.width = width * dpr canvas.height = height * dpr canvas.style.width = `${width}px` canvas.style.height = `${height}px` const ctx = canvas.getContext('2d')! ctx.setTransform(dpr, 0, 0, dpr, 0, 0) // Compute dynamic viewport from sieve animation keyframes const vpState = getSieveViewportState( elapsedMs, sieveViewportsRef.current, CELEBRATION_VP, width, 120 ) const state: NumberLineState = vpState ? { center: vpState.center, pixelsPerUnit: vpState.pixelsPerUnit } : SIEVE_INITIAL_STATE // Compute sieve tick transforms + prime infos for the real number line const storyViewportRight = state.center + width / (2 * state.pixelsPerUnit) const sieveTransforms = computeSieveTickTransforms( SWEEP_MAX_N, elapsedMs, height, storyViewportRight ) const ticks = computeTickMarks(state, width, DEFAULT_TICK_THRESHOLDS) const primeInfos = computePrimeInfos(ticks) // Smooth ramp: ease-out quad over first 2s const rawT = Math.min(1, elapsedMs / 2000) const sieveUniformity = rawT * (2 - rawT) // Render the real number line with sieve transforms applied renderNumberLine( ctx, state, width, height, dark, DEFAULT_TICK_THRESHOLDS, 0, 0, 0.5, undefined, undefined, undefined, primeInfos, undefined, undefined, undefined, undefined, undefined, sieveTransforms, sieveUniformity ) renderSieveOverlay(ctx, state, width, height, dark, elapsedMs, 1) }, [width, height, dark, elapsedMs]) const handleScrub = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { const ms = Number(e.target.value) setElapsedMs(ms) setPlaying(false) lastFrameRef.current = null }, []) const togglePlay = useCallback(() => { setPlaying((p) => { if (!p) lastFrameRef.current = null // reset dt on resume return !p }) }, []) const restart = useCallback(() => { setElapsedMs(0) setPlaying(true) lastFrameRef.current = null }, []) // Phase label (matches virtual timeline with fall-animation tails between sweeps) const phase = elapsedMs < 4000 ? 'Waiting (skip counting intro)' : elapsedMs < 9000 ? 'Skip count by 2s (slow)' : elapsedMs < 10200 ? 'Composites falling...' : elapsedMs < 13200 ? 'Skip count by 3s (faster)' : elapsedMs < 14400 ? 'Composites falling...' : elapsedMs < 16200 ? 'Skip count by 5s (faster!)' : elapsedMs < 16600 ? 'Composites falling / zoom transition' : elapsedMs < 17900 ? 'Skip count by 7s (quick!)' : elapsedMs < 18300 ? 'Composites falling...' : elapsedMs < 19100 ? 'Skip count by 11s (lightning!)' : elapsedMs < 20300 ? 'Composites falling...' : elapsedMs < COMPOSITION_START_MS ? 'Prime celebration!' : 'Composition reveal (2×2×3 = 12)' return ( <div data-component="sieve-story-harness"> <canvas ref={canvasRef} data-element="sieve-canvas" style={{ borderRadius: 8, border: '1px solid #ccc' }} /> <div data-element="controls" style={{ display: 'flex', flexDirection: 'column', gap: 8, marginTop: 12, fontFamily: 'sans-serif', fontSize: 13, color: dark ? '#ccc' : '#333', }} > <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}> <button onClick={togglePlay} data-action="toggle-play" style={{ width: 60 }}> {playing ? 'Pause' : 'Play'} </button> <button onClick={restart} data-action="restart"> Restart </button> <input type="range" min={0} max={TOTAL_MS} step={50} value={elapsedMs} onChange={handleScrub} data-element="scrubber" style={{ flex: 1 }} /> <span style={{ minWidth: 60, textAlign: 'right' }}>{(elapsedMs / 1000).toFixed(1)}s</span> </div> <div data-element="phase-label" style={{ opacity: 0.7 }}> Phase: <strong>{phase}</strong> </div> </div> </div> ) } // --- Storybook meta --- const meta: Meta<HarnessProps> = { title: 'Toys/NumberLine/SieveOverlay', component: SieveHarness, parameters: { layout: 'centered', docs: { description: { component: 'Animated Sieve of Eratosthenes overlay for the "ancient-trick" prime tour stop. ' + 'Factors 2, 3, 5, 7 sweep across the visible range marking composites, ' + 'then primes celebrate as survivors.', }, }, }, argTypes: { width: { control: { type: 'range', min: 400, max: 1200, step: 50 } }, height: { control: { type: 'range', min: 80, max: 300, step: 10 } }, dark: { control: 'boolean' }, speed: { control: { type: 'range', min: 0.25, max: 4, step: 0.25 } }, autoPlay: { control: 'boolean' }, }, } export default meta type Story = StoryObj<HarnessProps> export const Default: Story = { args: { width: 800, height: 150, dark: false, speed: 1, autoPlay: true, }, } export const DarkMode: Story = { name: 'Dark Mode', args: { width: 800, height: 150, dark: true, speed: 1, autoPlay: true, }, } export const Mobile: Story = { name: 'Mobile (400px)', args: { width: 400, height: 120, dark: false, speed: 1, autoPlay: true, }, } export const Scrubbing: Story = { name: 'Paused (for scrubbing)', args: { width: 800, height: 150, dark: false, speed: 1, autoPlay: false, }, } |