/* Pulkovo precipitation and Saint Petersburg snow cover, quietly in the background. */ (() => { 'use strict'; const field = document.querySelector('.weather-field'); if (!field) return; const snowdrift = document.querySelector('.footer-snowdrift'); let lastSignature = ''; function buildSnowdrifts() { if (!snowdrift) return; const ns = 'http://www.w3.org/2000/svg'; const svg = document.createElementNS(ns, 'svg'); svg.setAttribute('viewBox', '0 0 1200 160'); svg.setAttribute('preserveAspectRatio', 'none'); svg.setAttribute('focusable', 'false'); const random = (min, max) => min + Math.random() * (max - min); const weights = Array.from({ length: Math.random() < 0.6 ? 2 : 3 }, () => random(0.7, 1.5)); const total = weights.reduce((sum, weight) => sum + weight, 0); // One profile per page load. The gentle windward slope rises to a cornice; // a shorter, steeper lee face falls back to the next valley. let x = 0; let y = random(126, 138); let outline = `M0 ${y}`; let shadows = ''; let ridges = ''; for (const weight of weights) { const width = weight / total * 1200; const end = x + width; const crest = x + width * random(0.54, 0.7); const peak = random(18, 65); const nextY = random(124, 139); const shoulder = x + width * random(0.33, 0.43); const shoulderY = peak + (y - peak) * random(0.12, 0.23); const lee = crest + (end - crest) * random(0.25, 0.42); const leeY = peak + (nextY - peak) * random(0.65, 0.82); outline += ` C${x + width * 0.22} ${y} ${shoulder - width * 0.08} ${shoulderY + 9} ${shoulder} ${shoulderY}`; outline += ` C${crest - width * 0.09} ${peak + 9} ${crest - width * 0.025} ${peak + 4} ${crest} ${peak}`; outline += ` C${crest + width * 0.05} ${peak + 6} ${lee - width * 0.035} ${leeY - 8} ${lee} ${leeY}`; outline += ` C${lee + width * 0.09} ${nextY - 2} ${end - width * 0.1} ${nextY} ${end} ${nextY}`; shadows += ` M${crest} ${peak} C${crest + width * 0.02} ${peak + 18} ${lee - width * 0.06} ${leeY + 2} ${lee} ${leeY + 5}`; shadows += ` Q${lee + width * 0.1} ${nextY + 1} ${end} ${nextY} C${lee + width * 0.09} ${nextY - 2} ${lee} ${leeY} ${crest} ${peak}Z`; ridges += ` M${x + width * 0.18} ${y - 4} Q${shoulder} ${shoulderY - 2} ${crest} ${peak + 4}`; x = end; y = nextY; } for (const [className, path] of [ ['snow-bank-front', `${outline} V160 H0Z`], ['snow-bank-shade', shadows], ['snow-bank-ridge', ridges], ]) { const shape = document.createElementNS(ns, 'path'); shape.setAttribute('class', className); shape.setAttribute('d', path); svg.append(shape); } snowdrift.replaceChildren(svg); } function render(weather) { const mode = ['rain', 'snow', 'mixed'].includes(weather?.mode) ? weather.mode : 'none'; const intensity = Math.max(1, Math.min(3, Number(weather.intensity) || 1)); const wind = Math.max(0, Math.min(60, Number(weather.wind_mps) || 0)); const signature = `${mode}/${intensity}/${wind}`; if (signature === lastSignature) return; lastSignature = signature; const count = mode === 'none' ? 0 : [0, 18, 34, 54][Math.round(intensity)]; const duration = (mode === 'rain' ? 1.7 : 12) / (1 + Math.min(wind, 25) / 8); field.dataset.weather = mode; field.style.setProperty('--drift', `${Math.min(24, wind * 1.2)}vw`); field.style.setProperty('--rain-angle', `${Math.min(30, wind * 1.8)}deg`); const particles = document.createDocumentFragment(); for (let index = 0; index < count; index++) { const particle = document.createElement('span'); const kind = mode === 'mixed' ? (index % 2 ? 'rain' : 'snow') : mode; particle.className = 'weather-particle'; particle.dataset.kind = kind; const seconds = (kind === 'rain' && mode === 'mixed' ? duration / 6 : duration) * (0.8 + Math.random() * 0.5); particle.style.left = `${(index + Math.random()) * 100 / count}%`; particle.style.setProperty('--duration', `${seconds.toFixed(2)}s`); particle.style.animationDelay = `${(-Math.random() * seconds).toFixed(2)}s`; particle.style.setProperty('--size', (0.65 + Math.random() * 0.65).toFixed(2)); particles.append(particle); } field.replaceChildren(particles); } function renderSnowCover(cover) { const depth = cover?.depth_cm; const stamp = cover?.observed_on; const available = cover?.available === true && typeof depth === 'number' && Number.isFinite(depth) && depth >= 0 && depth <= 500 && /^\d{4}-\d{2}-\d{2}$/.test(stamp); snowdrift.dataset.depth = available ? String(depth) : ''; // A visual scale, not centimetres rendered 1:1; keep the footer usable in deep snow. snowdrift.style.setProperty('--snow-height', available && depth > 0 ? `${Math.min(144, 20 + Math.sqrt(depth) * 20)}px` : '0px'); } function poll(url, interval, update, fallback) { let timer; let controller; let nextRefresh = 0; async function refresh() { clearTimeout(timer); if (document.hidden || controller) return; if (Date.now() < nextRefresh) { timer = setTimeout(refresh, nextRefresh - Date.now()); return; } controller = new AbortController(); const timeout = setTimeout(() => controller?.abort(), 6000); try { const response = await fetch(url, { signal: controller.signal, credentials: 'omit', cache: 'no-store' }); if (!response.ok) throw new Error('weather unavailable'); update(await response.json()); } catch { if (!document.hidden) update(fallback); } finally { clearTimeout(timeout); controller = null; nextRefresh = document.hidden ? 0 : Date.now() + interval; if (!document.hidden) timer = setTimeout(refresh, interval); } } return { refresh, pause() { clearTimeout(timer); controller?.abort(); }, }; } buildSnowdrifts(); const feeds = [poll('/api/weather/ulli', 5 * 60 * 1000, render, { mode: 'none' })]; if (snowdrift) feeds.push(poll('/api/weather/snow-cover', 30 * 60 * 1000, renderSnowCover, { available: false })); document.addEventListener('visibilitychange', () => { field.classList.toggle('weather-paused', document.hidden); for (const feed of feeds) document.hidden ? feed.pause() : feed.refresh(); }); for (const feed of feeds) feed.refresh(); })();