/* Be Their Someone ยท festive touches: falling snow + Christmas countdown.
Purely decorative. The page works fine without it. */
(() => {
'use strict';
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/* ---------- Countdown to Christmas ---------- */
const badge = document.querySelector('[data-countdown]');
if (badge) {
const now = new Date();
const xmas = new Date(now.getFullYear(), 11, 25);
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const days = Math.round((xmas - today) / 86400000);
const text = badge.querySelector('[data-countdown-text]');
if (days > 1) text.innerHTML = `Only ${days} days until Christmas`;
else if (days === 1) text.innerHTML = 'Tomorrow is Christmas!';
else if (days === 0) text.innerHTML = 'Merry Christmas!';
if (days >= 0 && days <= 60) badge.hidden = false; // only show during the campaign window
}
/* ---------- Snow ---------- */
const canvases = [...document.querySelectorAll('canvas.snow')];
if (!canvases.length) return;
const DPR = Math.min(window.devicePixelRatio || 1, 2);
function makeSnow(canvas) {
const ctx = canvas.getContext('2d');
let w = 0, h = 0, flakes = [], running = false, raf = 0, last = 0;
function resize() {
const r = canvas.getBoundingClientRect();
w = r.width; h = r.height;
canvas.width = Math.round(w * DPR); canvas.height = Math.round(h * DPR);
ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
const count = Math.min(140, Math.round((w * h) / 9000));
flakes = Array.from({ length: count }, () => spawn(true));
if (!running) draw(); // static frame (reduced motion, or before the loop starts)
}
function spawn(anywhere) {
const r = Math.random() * 2.6 + 0.8;
return {
x: Math.random() * w,
y: anywhere ? Math.random() * h : -10,
r,
vy: 12 + r * 14 + Math.random() * 10, // px per second; bigger flakes fall faster
sway: Math.random() * 18 + 6,
phase: Math.random() * Math.PI * 2,
a: 0.45 + Math.random() * 0.5,
};
}
function draw() {
ctx.clearRect(0, 0, w, h);
for (const f of flakes) {
ctx.globalAlpha = f.a;
ctx.beginPath();
ctx.arc(f.x + Math.sin(f.phase) * f.sway, f.y, f.r, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
}
ctx.globalAlpha = 1;
}
function tick(t) {
const dt = Math.min(0.05, (t - (last || t)) / 1000);
last = t;
for (let i = 0; i < flakes.length; i++) {
const f = flakes[i];
f.y += f.vy * dt;
f.phase += dt * 0.8;
if (f.y - f.r > h) flakes[i] = spawn(false);
}
draw();
raf = requestAnimationFrame(tick);
}
function start() { if (running || reduceMotion) return; running = true; last = 0; raf = requestAnimationFrame(tick); }
function stop() { running = false; cancelAnimationFrame(raf); }
resize();
return { resize, start, stop };
}
const snows = new Map(canvases.map(c => [c, makeSnow(c)]));
// Only animate canvases that are on screen and while the tab is visible.
const onScreen = new Set();
if ('IntersectionObserver' in window) {
const io = new IntersectionObserver(entries => {
for (const e of entries) {
if (e.isIntersecting) { onScreen.add(e.target); if (!document.hidden) snows.get(e.target).start(); }
else { onScreen.delete(e.target); snows.get(e.target).stop(); }
}
});
canvases.forEach(c => io.observe(c));
} else {
canvases.forEach(c => { onScreen.add(c); snows.get(c).start(); });
}
document.addEventListener('visibilitychange', () => {
snows.forEach((s, c) => (document.hidden ? s.stop() : onScreen.has(c) && s.start()));
});
let resizeTimer;
window.addEventListener('resize', () => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => snows.forEach(s => s.resize()), 150);
});
})();