Branchwell

PulseCanvas / website / hero-visualizer.js

1// Keep Three.js out of the critical path and retain the poster if WebGL is unavailable.
2export function initHeroVisualizer(reducedMotion) {
3 const container = document.querySelector('#hero-visualizer');
4 const stage = document.querySelector('#visual-stage');
5 const button = document.querySelector('#motion-toggle');
6 const poster = document.querySelector('#stage-poster');
7 const tallPoster = document.querySelector('#stage-poster-tall');
8 const scenes = {
9 silk: ['Silk Vortex', 'a twisting tunnel of folded peach, rose and lilac silk spiralling into a bright core'],
10 helix: ['Helix Drive', 'a six-lobed tunnel twisting around the viewer, lined with racing neon seams'],
11 hyperlane: ['Hyperlane', 'a warp corridor of neon light streaks and octagonal gates rushing toward a bright star'],
12 };
13 function updateScene(name) {
14 const [label, alt] = scenes[name];
15 tallPoster.srcset = `/images/hero/${name}-tall.jpg`;
16 poster.src = `/images/hero/${name}-wide.jpg`;
17 poster.alt = `${label}: ${alt}`;
18 document.querySelector('#scene-name').textContent = label;
19 }
20 let requested = !reducedMotion.matches;
21 let visible = false;
22 let painted = false;
23 let visualizer;
24 let loading;
25 let failed = false;
26 let disposed = false;
27
28 function updateButton(playing) {
29 button.querySelector('use').setAttribute('href', playing ? '#icon-pause' : '#icon-play');
30 button.querySelector('span').textContent = playing ? 'Pause motion' : 'Play motion';
31 button.setAttribute('aria-label', playing ? 'Pause visualizer motion' : 'Play visualizer motion');
32 }
33
34 function showFallback() {
35 failed = true;
36 visualizer?.dispose();
37 visualizer = undefined;
38 container.classList.remove('ready');
39 document.querySelector('#scene-render-mode').textContent = 'still preview';
40 updateButton(false);
41 button.disabled = true;
42 button.querySelector('span').textContent = 'Still preview';
43 button.setAttribute('aria-label', 'Live visual unavailable; showing visualizer still');
44 }
45
46 async function sync() {
47 const shouldRun = requested && visible && painted && !document.hidden;
48 if (failed || disposed) return;
49 if (!shouldRun) {
50 visualizer?.setRunning(false);
51 updateButton(false);
52 return;
53 }
54 if (!visualizer) {
55 loading ??= import('./hero-scenes.js').then(({ mountHeroScenes }) => {
56 if (disposed) return;
57 visualizer = mountHeroScenes(container, { onError: showFallback, onSceneChange: updateScene });
58 container.classList.add('ready');
59 document.querySelector('#scene-render-mode').textContent = 'live visual';
60 }).catch(showFallback);
61 await loading;
62 if (!visualizer || failed || disposed) return;
63 }
64 // Visibility or motion preferences may have changed while the module loaded.
65 const running = requested && visible && !document.hidden;
66 visualizer.setRunning(running);
67 updateButton(running);
68 }
69
70 button.addEventListener('click', () => {
71 requested = !requested;
72 void sync();
73 });
74 const observer = new IntersectionObserver(([entry]) => {
75 visible = entry.isIntersecting;
76 void sync();
77 }, { threshold: 0.05 });
78 observer.observe(stage);
79 document.addEventListener('visibilitychange', () => void sync());
80 reducedMotion.addEventListener('change', () => {
81 requested = !reducedMotion.matches;
82 void sync();
83 });
84 const start = () => requestAnimationFrame(() => { painted = true; void sync(); });
85 if (document.readyState === 'complete') start();
86 else window.addEventListener('load', start, { once: true });
87 window.addEventListener('pagehide', (event) => {
88 visualizer?.setRunning(false);
89 if (!event.persisted) {
90 disposed = true;
91 observer.disconnect();
92 visualizer?.dispose();
93 }
94 });
95 window.addEventListener('pageshow', () => void sync());
96}