| 6246d1e5 Andy McCall ↶ |
1 |
{# A backdrop (docs/backgrounds.md): the scene the page's |
| 2 |
`#backdrop-data` names (written just before this include) drawn by the |
| 3 |
engine, Rust compiled to WebAssembly. The viewer's choice (Settings › Appearance) is a cookie; |
| 0a3b244b Andy McCall ↶ |
4 |
unset, a touch-only device or Save-Data gets a still sky. Any failure |
| da8f7826 Andy McCall ↶ |
5 |
leaves no backdrop and nothing else changed. A sky with `live` polls |
| 6 |
`/sky/arrivals` for what happened since and adds it. #} |
| 0a3b244b Andy McCall ↶ |
7 |
<canvas id="backdrop" aria-hidden="true"></canvas> |
| 8 |
<script type="module"> |
| 9 |
const canvas = document.getElementById("backdrop"); |
| 10 |
const chosen = document.cookie.match(/(?:^|; )backdrop=(moving|still|off)/); |
| 11 |
const touch = matchMedia("(pointer: coarse)").matches && !matchMedia("(pointer: fine)").matches; |
| 12 |
const pick = chosen ? chosen[1] : (touch || (navigator.connection && navigator.connection.saveData) ? "still" : "moving"); |
| 13 |
if (pick === "off") { |
| 14 |
canvas.remove(); |
| 15 |
} else { |
| 16 |
try { |
| 17 |
const bd = await import("/static/backdrop/backdrop.js?v={{ shell.assets() }}"); |
| 18 |
await bd.default({ module_or_path: "/static/backdrop/backdrop_bg.wasm?v={{ shell.assets() }}" }); |
| da8f7826 Andy McCall ↶ |
19 |
const data = JSON.parse(document.getElementById("backdrop-data").textContent); |
| 20 |
bd.start(canvas, data, pick === "still"); |
| 21 |
|
| 22 |
|
| 23 |
if (data.live) { |
| 24 |
let since = data.now; |
| 25 |
const poll = async () => { |
| 26 |
if (document.hidden) return; |
| 27 |
try { |
| 28 |
const r = await fetch(`/sky/arrivals?scene=${data.live}&since=${since}`, { headers: { accept: "application/json" } }); |
| 29 |
if (!r.ok) return; |
| 30 |
const got = await r.json(); |
| 31 |
since = got.now; |
| 32 |
if (got.events.length) bd.arrive(got); |
| 33 |
} catch (e) {} |
| 34 |
}; |
| 35 |
setInterval(poll, 30000); |
| 36 |
document.addEventListener("visibilitychange", poll); |
| 37 |
} |
| 0a3b244b Andy McCall ↶ |
38 |
} catch (e) { |
| 39 |
canvas.remove(); |
| 40 |
} |
| 41 |
} |
| 42 |
</script> |