// Keep Three.js out of the critical path and retain the poster if WebGL is unavailable. export function initHeroVisualizer(reducedMotion) { const container = document.querySelector('#hero-visualizer'); const stage = document.querySelector('#visual-stage'); const button = document.querySelector('#motion-toggle'); const poster = document.querySelector('#stage-poster'); const tallPoster = document.querySelector('#stage-poster-tall'); const scenes = { silk: ['Silk Vortex', 'a twisting tunnel of folded peach, rose and lilac silk spiralling into a bright core'], helix: ['Helix Drive', 'a six-lobed tunnel twisting around the viewer, lined with racing neon seams'], hyperlane: ['Hyperlane', 'a warp corridor of neon light streaks and octagonal gates rushing toward a bright star'], }; function updateScene(name) { const [label, alt] = scenes[name]; tallPoster.srcset = `/images/hero/${name}-tall.jpg`; poster.src = `/images/hero/${name}-wide.jpg`; poster.alt = `${label}: ${alt}`; document.querySelector('#scene-name').textContent = label; } let requested = !reducedMotion.matches; let visible = false; let painted = false; let visualizer; let loading; let failed = false; let disposed = false; function updateButton(playing) { button.querySelector('use').setAttribute('href', playing ? '#icon-pause' : '#icon-play'); button.querySelector('span').textContent = playing ? 'Pause motion' : 'Play motion'; button.setAttribute('aria-label', playing ? 'Pause visualizer motion' : 'Play visualizer motion'); } function showFallback() { failed = true; visualizer?.dispose(); visualizer = undefined; container.classList.remove('ready'); document.querySelector('#scene-render-mode').textContent = 'still preview'; updateButton(false); button.disabled = true; button.querySelector('span').textContent = 'Still preview'; button.setAttribute('aria-label', 'Live visual unavailable; showing visualizer still'); } async function sync() { const shouldRun = requested && visible && painted && !document.hidden; if (failed || disposed) return; if (!shouldRun) { visualizer?.setRunning(false); updateButton(false); return; } if (!visualizer) { loading ??= import('./hero-scenes.js').then(({ mountHeroScenes }) => { if (disposed) return; visualizer = mountHeroScenes(container, { onError: showFallback, onSceneChange: updateScene }); container.classList.add('ready'); document.querySelector('#scene-render-mode').textContent = 'live visual'; }).catch(showFallback); await loading; if (!visualizer || failed || disposed) return; } // Visibility or motion preferences may have changed while the module loaded. const running = requested && visible && !document.hidden; visualizer.setRunning(running); updateButton(running); } button.addEventListener('click', () => { requested = !requested; void sync(); }); const observer = new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; void sync(); }, { threshold: 0.05 }); observer.observe(stage); document.addEventListener('visibilitychange', () => void sync()); reducedMotion.addEventListener('change', () => { requested = !reducedMotion.matches; void sync(); }); const start = () => requestAnimationFrame(() => { painted = true; void sync(); }); if (document.readyState === 'complete') start(); else window.addEventListener('load', start, { once: true }); window.addEventListener('pagehide', (event) => { visualizer?.setRunning(false); if (!event.persisted) { disposed = true; observer.disconnect(); visualizer?.dispose(); } }); window.addEventListener('pageshow', () => void sync()); }