Branchwell

PulseCanvas / website / main.js

1import '@fontsource/outfit/latin-400.css';
2import '@fontsource/outfit/latin-500.css';
3import '@fontsource/manrope/latin-400.css';
4import '@fontsource/manrope/latin-500.css';
5import '@fontsource/manrope/latin-700.css';
6import './style.css';
7import { siteConfig } from './site.config.js';
8import { films } from './media.config.js';
9import { mountFilm } from './film-player.js';
10import { configurePublicLinks } from './public-links.js';
11import { initHeroVisualizer } from './hero-visualizer.js';
12
13configurePublicLinks();
14
15// Load offscreen posters near the viewport, including stills for reduced motion.
16const posterObserver = new IntersectionObserver((entries) => {
17 for (const entry of entries) {
18 if (!entry.isIntersecting) continue;
19 entry.target.poster = entry.target.dataset.poster;
20 posterObserver.unobserve(entry.target);
21 }
22}, { rootMargin: '250px 0px' });
23document.querySelectorAll('video[data-poster]').forEach((player) => posterObserver.observe(player));
24
25const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
26const mediaBase = (publicUrl(siteConfig.visualizerMediaBaseUrl) || '/media').replace(/\/$/, '');
27const loopSource = (name, size) => `${mediaBase}/loops/${name}-${size}.mp4`;
28
29const header = document.querySelector('#site-header');
30const updateHeader = () => header.classList.toggle('scrolled', window.scrollY > 24);
31updateHeader();
32window.addEventListener('scroll', updateHeader, { passive: true });
33
34initHeroVisualizer(reducedMotion);
35
36// Silent visualizer loops elsewhere on the page play only while on screen.
37const loopVideos = new Set();
38const loopObserver = new IntersectionObserver(
39 (entries) => {
40 entries.forEach((entry) => {
41 const loop = entry.target;
42 // Decode only loops that are meaningfully on screen to keep playback smooth.
43 loop.dataset.visible = String(entry.intersectionRatio >= 0.3);
44 syncLoop(loop);
45 });
46 },
47 { threshold: [0, 0.3] },
48);
49function syncLoop(loop) {
50 const shouldPlay =
51 loop.dataset.visible === 'true' && !reducedMotion.matches && !document.hidden;
52 if (!shouldPlay) {
53 loop.pause();
54 return;
55 }
56 if (!loop.getAttribute('src')) {
57 loop.preload = 'auto';
58 loop.src = loopSource(loop.dataset.loop, loop.dataset.size || 720);
59 }
60 loop.play().catch(() => {});
61}
62document.querySelectorAll('video.loop').forEach((loop) => {
63 loop.muted = true;
64 loopVideos.add(loop);
65 loopObserver.observe(loop);
66});
67document.addEventListener('visibilitychange', () => loopVideos.forEach(syncLoop));
68reducedMotion.addEventListener('change', () => loopVideos.forEach(syncLoop));
69
70if (!reducedMotion.matches && 'IntersectionObserver' in window) {
71 const revealObserver = new IntersectionObserver(
72 (entries) => {
73 entries.forEach((entry) => {
74 if (!entry.isIntersecting) return;
75 entry.target.classList.add('is-visible');
76 revealObserver.unobserve(entry.target);
77 });
78 },
79 { threshold: 0.12 },
80 );
81 document
82 .querySelectorAll(
83 '.section-heading, .film-heading, .film-frame, .workflow-grid article, .app-shot, .studio-notes article, .style-media, .style-copy, .world, .export-copy, .format-demo, .walkthrough-media, .faq-intro, .faq-list, .download-inner',
84 )
85 .forEach((element) => {
86 element.classList.add('reveal');
87 revealObserver.observe(element);
88 });
89}
90
91const formats = {
92 '16:9': { ratio: 16 / 9, label: 'Wide', dimensions: '3840 × 2160' },
93 '4:3': { ratio: 4 / 3, label: 'Landscape', dimensions: '2880 × 2160' },
94 '3:4': { ratio: 3 / 4, label: 'Portrait', dimensions: '2160 × 2880' },
95 '9:16': { ratio: 9 / 16, label: 'Phone', dimensions: '2160 × 3840' },
96 '1:1': { ratio: 1, label: 'Square', dimensions: '2160 × 2160' },
97};
98let activeFormat = '16:9';
99const canvas = document.querySelector('#format-canvas');
100const formatStage = document.querySelector('.format-stage');
101
102function resizeCanvas() {
103 const { ratio } = formats[activeFormat];
104 const style = getComputedStyle(formatStage);
105 const horizontalPadding =
106 parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
107 const verticalPadding =
108 parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
109 const width = Math.min(
110 formatStage.clientWidth - horizontalPadding,
111 (formatStage.clientHeight - verticalPadding) * ratio,
112 );
113 canvas.style.width = `${width}px`;
114 canvas.style.aspectRatio = `${ratio}`;
115}
116
117new ResizeObserver(resizeCanvas).observe(formatStage);
118document.querySelectorAll('[data-format]').forEach((button) => {
119 button.addEventListener('click', () => {
120 activeFormat = button.dataset.format;
121 const format = formats[activeFormat];
122 document
123 .querySelectorAll('[data-format]')
124 .forEach((item) =>
125 item.setAttribute('aria-pressed', String(item === button)),
126 );
127 document.querySelector('#format-overlay').textContent = activeFormat;
128 document.querySelector('#format-output').textContent =
129 `${format.label} · ${format.dimensions} pixels at the 4K setting`;
130 resizeCanvas();
131 });
132});
133
134const menuButton = document.querySelector('.menu-toggle');
135const navigation = document.querySelector('#navigation');
136function closeMenu() {
137 navigation.classList.remove('open');
138 menuButton.setAttribute('aria-expanded', 'false');
139 menuButton.setAttribute('aria-label', 'Open navigation');
140}
141menuButton.addEventListener('click', () => {
142 const open = menuButton.getAttribute('aria-expanded') !== 'true';
143 menuButton.setAttribute('aria-expanded', String(open));
144 menuButton.setAttribute(
145 'aria-label',
146 open ? 'Close navigation' : 'Open navigation',
147 );
148 navigation.classList.toggle('open', open);
149});
150navigation
151 .querySelectorAll('a')
152 .forEach((link) => link.addEventListener('click', closeMenu));
153document.addEventListener('keydown', (event) => {
154 if (
155 event.key === 'Escape' &&
156 menuButton.getAttribute('aria-expanded') === 'true'
157 ) {
158 closeMenu();
159 menuButton.focus();
160 }
161});
162document.addEventListener('click', (event) => {
163 if (!navigation.contains(event.target) && !menuButton.contains(event.target))
164 closeMenu();
165});
166
167const dialog = document.querySelector('#image-dialog');
168document.querySelectorAll('[data-image]').forEach((button) => {
169 button.addEventListener('click', () => {
170 document.querySelector('#dialog-image').src = button.dataset.image;
171 document.querySelector('#dialog-image').alt =
172 button.querySelector('img').alt;
173 document.querySelector('#image-dialog-caption').textContent =
174 button.dataset.caption;
175 dialog.showModal();
176 });
177});
178document
179 .querySelector('.dialog-close')
180 .addEventListener('click', () => dialog.close());
181dialog.addEventListener('click', (event) => {
182 if (event.target === dialog) dialog.close();
183});
184dialog.addEventListener('keydown', (event) => {
185 if (event.key === 'Tab') {
186 event.preventDefault();
187 dialog.querySelector('.dialog-close').focus();
188 }
189});
190
191function publicUrl(value) {
192 if (!value) return '';
193 // Only web URLs and site-relative media are usable public destinations.
194 if (
195 value.startsWith('/') &&
196 !value.startsWith('//') &&
197 !/^\/(Users|Volumes|home|tmp)\//.test(value)
198 )
199 return value;
200 try {
201 const url = new URL(value);
202 return url.protocol === 'https:' ? url.href : '';
203 } catch {
204 return '';
205 }
206}
207
208// 3D octagons behind the download section load only when it approaches the viewport.
209const octagonScene = document.querySelector('#octagon-scene');
210let octagons;
211let octagonsVisible = false;
212const octagonObserver = new IntersectionObserver(
213 async (entries) => {
214 const visible = entries[0].isIntersecting;
215 octagonsVisible = visible;
216 if (visible && !octagons) {
217 const { mountOctagons, supportsWebGL } = await import('./octagons.js');
218 if (!supportsWebGL()) {
219 octagonObserver.disconnect();
220 octagonScene.remove();
221 return;
222 }
223 octagons = mountOctagons(octagonScene, {
224 animate: () => !reducedMotion.matches && !document.hidden,
225 });
226 const fallback = document.querySelector('.download-backdrop');
227 loopObserver.unobserve(fallback);
228 loopVideos.delete(fallback);
229 fallback.pause();
230 fallback.remove();
231 octagonScene.classList.add('ready');
232 }
233 octagons?.setRunning(visible);
234 },
235 { rootMargin: '300px 0px' },
236);
237octagonObserver.observe(octagonScene);
238document.addEventListener('visibilitychange', () =>
239 octagons?.setRunning(octagonsVisible),
240);
241reducedMotion.addEventListener('change', () => {
242 octagons?.setRunning(octagonsVisible);
243 octagons?.renderStill();
244});
245
246// The films play from R2: a poster and play button first, the video on click.
247function configureFilms() {
248 for (const selector of ['#feature-media', '#short-film-media']) {
249 const placeholder = document.querySelector(`${selector} .film-placeholder`);
250 if (placeholder) {
251 loopObserver.unobserve(placeholder);
252 loopVideos.delete(placeholder);
253 placeholder.pause();
254 }
255 }
256 mountFilm(document.querySelector('#feature-media'), films.song, { buttonText: 'Play the music video' });
257 mountFilm(document.querySelector('#short-film-media'), films.short, { buttonText: 'Play the short film' });
258 mountFilm(document.querySelector('#walkthrough-media'), films.walkthrough, { buttonText: 'Watch the tutorial' });
259 document.querySelector('#walkthrough-transcript').hidden = false;
260}
261configureFilms();