45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
let flakesStarted = false;
|
|
|
|
async function loadSnow() {
|
|
if (!navigator.geolocation) return alert('Geolocation not supported');
|
|
|
|
navigator.geolocation.getCurrentPosition(async (pos) => {
|
|
const lat = pos.coords.latitude;
|
|
const lon = pos.coords.longitude;
|
|
|
|
const res = await fetch(`/api/snowfall?lat=${lat}&lon=${lon}`);
|
|
const data = await res.json();
|
|
|
|
document.getElementById('amount').textContent = data.inches + ' inches today';
|
|
document.getElementById('meta').textContent = data.location + ' • ' + data.today;
|
|
|
|
const percent = Math.min(data.inches / data.max_inches, 1);
|
|
document.getElementById('snowWrapper').style.height = (percent * 100) + '%';
|
|
|
|
const now = new Date();
|
|
document.getElementById('updated').textContent =
|
|
'Last updated: ' + now.toLocaleTimeString();
|
|
|
|
if (data.snowing && !flakesStarted) {
|
|
spawnSnowflakes();
|
|
flakesStarted = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
function spawnSnowflakes() {
|
|
const container = document.getElementById('snowfall');
|
|
for (let i = 0; i < 90; i++) {
|
|
const flake = document.createElement('div');
|
|
flake.className = 'flake';
|
|
flake.style.left = Math.random()*100+'vw';
|
|
flake.style.animationDuration = 5+Math.random()*10+'s';
|
|
flake.style.opacity = Math.random();
|
|
flake.style.transform = `scale(${Math.random()+0.5})`;
|
|
container.appendChild(flake);
|
|
}
|
|
}
|
|
|
|
loadSnow();
|
|
setInterval(loadSnow, 10*60*1000);
|