| 1 | # Writing a scene |
| 2 | |
| 3 | A scene is one HTML document that draws a set of named states: a map |
| 4 | that zooms from a field to a city, a diagram that gains a box per step. |
| 5 | A slide shows it through a `scene` block, and each press moves it one |
| 6 | state on. Slides runs the document in a sealed frame, tells it which |
| 7 | state to show, and keeps the presses, the stills and everything else. |
| 8 | The scene draws. |
| 9 | |
| 10 | [`scenes/example.html`](scenes/example.html) is a whole scene in 40 |
| 11 | lines: a coloured box per state, each labelled from the scene's words. |
| 12 | Start from it. |
| 13 | |
| 14 | ## The bundle |
| 15 | |
| 16 | A scene's code is one HTML document, called its bundle: |
| 17 | |
| 18 | - UTF-8, opening with `<!doctype html>` or `<html>` (a byte-order mark |
| 19 | and white space before it are fine); |
| 20 | - at most 2 MiB; |
| 21 | - every script, style, picture and font inline. Pictures can be |
| 22 | `data:` URLs or `blob:` URLs the script makes; fonts can only be |
| 23 | `data:` URLs. |
| 24 | |
| 25 | Upload it as you upload a picture ([api.md](api.md#getting-the-bytes-there)), |
| 26 | with `"type": "text/html"`. Registration reads it back, refuses anything |
| 27 | that isn't one HTML document (`not_html`) or is over 2 MiB |
| 28 | (`too_large`), and answers `{"kind": "scene", "ref": "…"}`. That `ref` |
| 29 | is the scene's `code` in `add_scene` or `set_scene`. A bundle refusal |
| 30 | carries a fixed `error` code and a short English `message` for you. |
| 31 | |
| 32 | The bundle is served only to the deck whose scene names it, on the |
| 33 | same three surfaces as its pictures: |
| 34 | `/d/{sqid}/scenes/{id}/{ref}`, `/s/{token}/scenes/{id}/{ref}` and |
| 35 | `/p/{session}/scenes/{id}/{ref}`. The picture routes never serve it. |
| 36 | |
| 37 | ## What the frame forbids |
| 38 | |
| 39 | The bundle is served with this header and no other policy: |
| 40 | |
| 41 | ``` |
| 42 | Content-Security-Policy: sandbox allow-scripts; default-src 'none'; |
| 43 | script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:; |
| 44 | font-src data:; media-src data: blob:; frame-ancestors 'self' |
| 45 | ``` |
| 46 | |
| 47 | So the scene runs with an origin of its own that matches nothing. It |
| 48 | cannot: |
| 49 | |
| 50 | - make any request: `fetch`, `XMLHttpRequest`, `WebSocket`, |
| 51 | `EventSource`, `navigator.sendBeacon`, or an `<img>`, `<link>`, |
| 52 | `<script src>` or font pointing at a URL; |
| 53 | - read or write cookies, `localStorage`, `sessionStorage`, IndexedDB or |
| 54 | the Cache API (each throws `SecurityError`); |
| 55 | - submit a form, open a window, or navigate the page it sits in; |
| 56 | - load a document from another origin into its own frame: the page |
| 57 | around it allows frames from Slides only; |
| 58 | - reach the page around it: `parent.document` and `parent.location` |
| 59 | throw; |
| 60 | - use `eval`, `new Function`, workers or WebAssembly. |
| 61 | |
| 62 | Nothing a scene does reaches another origin or carries the reader's |
| 63 | credentials. It can navigate its own frame to an address on Slides |
| 64 | itself, and that request goes without the reader's cookies, so it |
| 65 | reaches only what a stranger could. Navigating its frame ends the |
| 66 | scene: Slides shows the state's still in its place. |
| 67 | |
| 68 | What it can do is draw: HTML, CSS, SVG, canvas, WebGL, animations, and |
| 69 | `postMessage` to its parent. It is shown, never used: the frame takes no |
| 70 | clicks, no keys and no focus, and assistive technology reads the state's |
| 71 | description, not the frame. |
| 72 | |
| 73 | ## The conversation |
| 74 | |
| 75 | Slides sends two kinds of message, and the scene answers one. |
| 76 | |
| 77 | **`init`**, once, after the frame loads: |
| 78 | |
| 79 | ```js |
| 80 | {type: "init", state: "field", size: {w: 960, h: 540}, |
| 81 | words: {title: "Vienna"}, data: {…}, reducedMotion: false, fonts: […]} |
| 82 | ``` |
| 83 | |
| 84 | - `state`: the id of the state to draw first. |
| 85 | - `size`: the frame's size in CSS pixels. The frame is the scene's |
| 86 | whole viewport, so a scene laid out in `vw`, `vh` or percentages |
| 87 | needs nothing more; a `resize` event says when it changes. |
| 88 | - `words`: the scene's labels, key to text. |
| 89 | - `data`: the scene's model, any JSON value. |
| 90 | - `reducedMotion`: whether to draw without movement (below). |
| 91 | - `fonts`: the faces the scene asked for (below). |
| 92 | |
| 93 | **`state`**, whenever the slide moves: |
| 94 | |
| 95 | ```js |
| 96 | {type: "state", state: "city", animate: true} |
| 97 | ``` |
| 98 | |
| 99 | With `animate: true` the scene moves from where it is to the new state. |
| 100 | With `animate: false` (a jump, or going back several presses) it draws |
| 101 | the new state at once. |
| 102 | |
| 103 | A frame can outlive its slide. When the next slide shows the same scene |
| 104 | in the same place (the same box on a canvas slide, the same region on a |
| 105 | flow slide), Slides keeps the frame and sends it the new slide's state, |
| 106 | which may be the state it is already in. Anywhere else the next slide |
| 107 | starts a frame of its own, with its own `init`. |
| 108 | |
| 109 | A slide can show one scene in several blocks, and each runs a frame of |
| 110 | its own. They are paired with the next slide's blocks of that scene in |
| 111 | order, the first with the first and the second with the second, and a |
| 112 | pair in the same place carries on. Reordering two blocks of one scene |
| 113 | starts both afresh. |
| 114 | |
| 115 | **`ready`**, from the scene, once the first state is drawn: |
| 116 | |
| 117 | ```js |
| 118 | parent.postMessage({type: "ready"}, "*"); |
| 119 | ``` |
| 120 | |
| 121 | Send it once. Slides ignores any later one, and any other message but |
| 122 | one: `{type: "error"}` ends the scene, and the state's still shows in |
| 123 | its place. |
| 124 | |
| 125 | Both sides check where a message came from. Slides takes only messages |
| 126 | whose source is the frame's own window; a scene should take only |
| 127 | messages whose `event.source` is `parent`. Post to `"*"`: the scene's |
| 128 | origin matches nothing, so no other target origin works. |
| 129 | |
| 130 | ## Words, data and fonts |
| 131 | |
| 132 | A scene's words and data live in the deck, not in its code. People |
| 133 | change a label or a value in the editor, and agents change them with |
| 134 | `set_scene`, without touching the bundle. Draw every piece of text from |
| 135 | `words`, and everything that varies between states from `data`. A good |
| 136 | shape for `data` is the scene's pieces plus a table of what each state |
| 137 | shows. Work out anything costly, such as routes, when the data is made, |
| 138 | never while the scene runs. |
| 139 | |
| 140 | The frame can't load the deck's fonts. To use one, list its family |
| 141 | names, as the deck's fonts list them, in `data.fonts`: |
| 142 | |
| 143 | ```json |
| 144 | "data": {"fonts": ["Brand", "Brand Mono"], …} |
| 145 | ``` |
| 146 | |
| 147 | `init` then carries each face of those families as a `data:` URL: |
| 148 | |
| 149 | ```js |
| 150 | fonts: [{family: "Brand", src: "data:font/woff2;base64,…", |
| 151 | weight: "400", style: "normal"}, …] |
| 152 | ``` |
| 153 | |
| 154 | Add each one before drawing text: |
| 155 | |
| 156 | ```js |
| 157 | for (const f of msg.fonts) { |
| 158 | const face = new FontFace(f.family, `url(${f.src})`, {weight: f.weight, style: f.style}); |
| 159 | document.fonts.add(await face.load()); |
| 160 | } |
| 161 | ``` |
| 162 | |
| 163 | `weight` is a CSS weight or, for a variable face, a range such as |
| 164 | `"100 900"`. Families the deck doesn't hold are not sent; system font |
| 165 | names still work in CSS. |
| 166 | |
| 167 | ## Reduced motion |
| 168 | |
| 169 | Readers who asked for less motion don't get the scene at all: Slides |
| 170 | shows them each state's still, which is that state at rest. So on a |
| 171 | slide `reducedMotion` is always false. Handle `true` anyway, for |
| 172 | previews: draw every state change as `animate: false` would, with no |
| 173 | movement and at most a short fade. |
| 174 | |
| 175 | ## Ready within 3 seconds |
| 176 | |
| 177 | Until `ready` arrives the frame isn't shown; the state's still is. If |
| 178 | `ready` hasn't arrived 3 seconds after `init`, Slides gives the scene up |
| 179 | and the still stays. A scene that throws, hangs or never |
| 180 | answers costs the reader nothing else: presses still count and the talk |
| 181 | moves on. Load fonts and build what you draw before answering, and |
| 182 | keep that under a second. |
| 183 | |
| 184 | ## Stills |
| 185 | |
| 186 | Give every state a still picture, PNG or WebP, at most 4 MiB. The PDF, |
| 187 | thumbnails, the phone view, the presenter's Next preview and readers |
| 188 | with scripts off show the still, not the scene. A state with no still |
| 189 | shows a plain panel with the scene's name and the state's description. |
| 190 | |
| 191 | ## Limits |
| 192 | |
| 193 | | What | Limit | |
| 194 | |---|---| |
| 195 | | Bundle | one HTML document, 2 MiB | |
| 196 | | `data` | 960 KiB as compact JSON, and its change one live update (1 MiB) | |
| 197 | | Still | PNG or WebP, 4 MiB, 30000 pixels a side and 50 megapixels in all | |
| 198 | | `ready` | 3 seconds after `init` | |
| 199 | |
| 200 | Number-heavy data encodes larger than its JSON. A batch that adds or |
| 201 | changes a scene becomes one live update, which may be 1 MiB at most, |
| 202 | and each number in `data` takes up to nine bytes there however short it |
| 203 | is written: 150,000 copies of `0.1` are 600 KB of JSON but a 1.35 MB |
| 204 | update, and are refused (by the API with a `422` saying so, and by the |
| 205 | editor before it writes anything). About 116,000 of them fit. Strings |
| 206 | cost roughly what their JSON does. |
| 207 | |
| 208 | ## Trying a scene |
| 209 | |
| 210 | The editor's code panel previews unsaved code by posting it to |
| 211 | `POST /d/{sqid}/scenes/preview`, which answers it under the same |
| 212 | policy and stores nothing. It takes a `text/html` body, or a form |
| 213 | (`multipart/form-data` or url-encoded) with one field, `html`, and no |
| 214 | other. The text may be 2 MiB and the whole request 2 MiB plus 64 KiB, |
| 215 | so post a large draft as multipart: url-encoding can triple its size. |
| 216 | Only a signed-in person who can open the deck gets an answer. |
| 217 | |
| 218 | ## Building a deck with a scene |
| 219 | |
| 220 | An agent does it in five steps, all with its bearer token |
| 221 | ([api.md](api.md)): |
| 222 | |
| 223 | 1. **Upload the bundle.** `POST /api/v1/decks/{sqid}/scene-bundles` |
| 224 | with the HTML as the body and `Content-Type: text/html`. It answers |
| 225 | `{"ref": "…"}`. |
| 226 | 2. **Add the scene and its slides in one batch.** `add_scene` with that |
| 227 | ref as its `code`, and its states, words and data; then the slides |
| 228 | and their `scene` blocks. Give the scene an `id` of your own, twelve |
| 229 | characters from `a` to `z` and `2` to `7`, so the blocks after it in |
| 230 | the batch can name it. |
| 231 | 3. **Decide where it carries on.** Two slides in a row whose blocks show |
| 232 | the same scene in the same place (the same `box` on canvas slides, |
| 233 | the same region on flow slides) share one frame. Start the second |
| 234 | block at the state the first one ends on. A block over the scene's |
| 235 | box with a higher `z` makes it start afresh on each slide. |
| 236 | 4. **Work out the version** from the code ref and the data and words you |
| 237 | sent ([api.md](api.md#scenes) says how). Sending the same bundle to |
| 238 | `PUT /api/v1/decks/{sqid}/scenes/{id}/code` answers it as well. |
| 239 | 5. **Draw a still for each state and upload it.** Serve the bundle with |
| 240 | the header above, frame it at 1920 by 1080, send it `init` for the |
| 241 | state with `reducedMotion: true`, wait for `ready` and take a picture |
| 242 | of the frame. Then `PUT /api/v1/decks/{sqid}/scenes/{id}/stills/{state}?made={version}`. |
| 243 | A `409 stale` means the scene changed after you worked the version |
| 244 | out: read the deck again and draw again. |
| 245 | |
| 246 | To check the result, `GET /api/v1/decks/{sqid}` shows each still's |
| 247 | `made` equal to the version, and `GET …/slides/{id}.svg` shows each |
| 248 | slide as the PDF will. |
| 249 | |
| 250 | ## The example |
| 251 | |
| 252 | [`scenes/example.html`](scenes/example.html) expects `data.states` to |
| 253 | list the states in order with a colour each, and a word per state: |
| 254 | |
| 255 | ```sh |
| 256 | -d '{"ops":[{"op":"add_scene","name":"Boxes","code":"<ref>", |
| 257 | "states":[{"id":"one","name":"One","description":"A red box"}, |
| 258 | {"id":"two","name":"Two","description":"A red box and a blue box"}], |
| 259 | "words":{"one":"First","two":"Second"}, |
| 260 | "data":{"states":[{"id":"one","colour":"#c0392b"}, |
| 261 | {"id":"two","colour":"#2471a3"}]}}]}' |
| 262 | ``` |
| 263 | |
| 264 | On `init` it draws a box for each state and hides the ones after the |
| 265 | current state; on `state` it shows or hides boxes, sliding them in when |
| 266 | told to animate and motion is allowed. It answers `ready` after the |
| 267 | first `init`. |
| 268 | |