Add ULLI weather easter egg to falling background particles

This commit is contained in:
nyan404 committed 2026-10-18 20:02:32 +03:00
1 parent 955f47ccd6
commit 98182c179f
11 files changed
+399 -21

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
/* A little Pulkovo weather in the existing autumn background. */
(() => {
'use strict';
const field = document.querySelector('.leaf-field');
if (!field) return;
const refreshMs = 5 * 60 * 1000;
let timer;
let controller;
let nextRefresh = 0;
let lastSignature = '';
function render(weather) {
const mode = ['leaves', 'rain', 'snow', 'mixed'].includes(weather.mode) ? weather.mode : 'leaves';
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 === 'leaves' ? 7 : [0, 18, 34, 54][Math.round(intensity)];
const duration = (mode === 'rain' ? 1.7 : mode === 'leaves' ? 19 : 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 = 'leaf';
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);
}
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(), 5000);
try {
const response = await fetch('/api/weather/ulli', { signal: controller.signal, credentials: 'omit', cache: 'no-store' });
if (!response.ok) throw new Error('weather unavailable');
render(await response.json());
} catch {
render({ mode: 'leaves', intensity: 1, wind_mps: 0 });
} finally {
clearTimeout(timeout);
controller = null;
nextRefresh = Date.now() + refreshMs;
if (!document.hidden) timer = setTimeout(refresh, refreshMs);
}
}
document.addEventListener('visibilitychange', () => {
field.classList.toggle('weather-paused', document.hidden);
if (document.hidden) {
clearTimeout(timer);
controller?.abort();
} else {
refresh();
}
});
refresh();
})();