| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <meta charset="utf-8"> |
| 4 | <title>Boxes</title> |
| 5 | <style> |
| 6 | html, body { margin: 0; height: 100%; font: 600 5vh system-ui, sans-serif; } |
| 7 | main { height: 100%; display: flex; gap: 3vw; align-items: center; justify-content: center; } |
| 8 | .box { width: 20vw; height: 20vw; display: grid; place-items: center; color: #fff; |
| 9 | border-radius: 1vw; transition: opacity 0.6s, transform 0.6s; } |
| 10 | .box[hidden] { display: grid; opacity: 0; transform: translateY(5vh); } |
| 11 | .still .box { transition: none; } |
| 12 | </style> |
| 13 | <main></main> |
| 14 | <script> |
| 15 | // One box per state in data.states ({id, colour}), labelled from words. |
| 16 | // A state shows its own box and every box before it. |
| 17 | const main = document.querySelector('main'); |
| 18 | let scene = null; |
| 19 | function draw(state, animate) { |
| 20 | const at = scene.data.states.findIndex((s) => s.id === state); |
| 21 | main.classList.toggle('still', !animate || scene.reducedMotion); |
| 22 | main.querySelectorAll('.box').forEach((box, i) => { box.hidden = i > at; }); |
| 23 | } |
| 24 | addEventListener('message', (e) => { |
| 25 | if (e.source !== parent || !e.data) return; |
| 26 | if (e.data.type === 'init') { |
| 27 | const first = scene === null; |
| 28 | scene = e.data; |
| 29 | main.replaceChildren(...scene.data.states.map((s) => { |
| 30 | const box = document.createElement('div'); |
| 31 | box.className = 'box'; |
| 32 | box.style.background = s.colour; |
| 33 | box.textContent = scene.words[s.id] ?? s.id; |
| 34 | return box; |
| 35 | })); |
| 36 | draw(scene.state, false); |
| 37 | if (first) parent.postMessage({type: 'ready'}, '*'); |
| 38 | } else if (e.data.type === 'state' && scene) draw(e.data.state, e.data.animate); |
| 39 | }); |
| 40 | </script> |
| 41 | |