oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

Writing a scene

A scene is one HTML document that draws a set of named states: a map that zooms from a field to a city, a diagram that gains a box per step. A slide shows it through a scene block, and each press moves it one state on. Slides runs the document in a sealed frame, tells it which state to show, and keeps the presses, the stills and everything else. The scene draws.

scenes/example.html is a whole scene in 40 lines: a coloured box per state, each labelled from the scene's words. Start from it.

The bundle

A scene's code is one HTML document, called its bundle:

  • UTF-8, opening with <!doctype html> or <html> (a byte-order mark and white space before it are fine);
  • at most 2 MiB;
  • every script, style, picture and font inline. Pictures can be data: URLs or blob: URLs the script makes; fonts can only be data: URLs.

Upload it as you upload a picture (api.md), with "type": "text/html". Registration reads it back, refuses anything that isn't one HTML document (not_html) or is over 2 MiB (too_large), and answers {"kind": "scene", "ref": "…"}. That ref is the scene's code in add_scene or set_scene. A bundle refusal carries a fixed error code and a short English message for you.

The bundle is served only to the deck whose scene names it, on the same three surfaces as its pictures: /d/{sqid}/scenes/{id}/{ref}, /s/{token}/scenes/{id}/{ref} and /p/{session}/scenes/{id}/{ref}. The picture routes never serve it.

What the frame forbids

The bundle is served with this header and no other policy:

Content-Security-Policy: sandbox allow-scripts; default-src 'none';
  script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:;
  font-src data:; media-src data: blob:; frame-ancestors 'self'

So the scene runs with an origin of its own that matches nothing. It cannot:

  • make any request: fetch, XMLHttpRequest, WebSocket, EventSource, navigator.sendBeacon, or an <img>, <link>, <script src> or font pointing at a URL;
  • read or write cookies, localStorage, sessionStorage, IndexedDB or the Cache API (each throws SecurityError);
  • submit a form, open a window, or navigate the page it sits in;
  • load a document from another origin into its own frame: the page around it allows frames from Slides only;
  • reach the page around it: parent.document and parent.location throw;
  • use eval, new Function, workers or WebAssembly.

Nothing a scene does reaches another origin or carries the reader's credentials. It can navigate its own frame to an address on Slides itself, and that request goes without the reader's cookies, so it reaches only what a stranger could. Navigating its frame ends the scene: Slides shows the state's still in its place.

What it can do is draw: HTML, CSS, SVG, canvas, WebGL, animations, and postMessage to its parent. It is shown, never used: the frame takes no clicks, no keys and no focus, and assistive technology reads the state's description, not the frame.

The conversation

Slides sends two kinds of message, and the scene answers one.

init, once, after the frame loads:

{type: "init", state: "field", size: {w: 960, h: 540},
 words: {title: "Vienna"}, data: {…}, reducedMotion: false, fonts: […]}
  • state: the id of the state to draw first.
  • size: the frame's size in CSS pixels. The frame is the scene's whole viewport, so a scene laid out in vw, vh or percentages needs nothing more; a resize event says when it changes.
  • words: the scene's labels, key to text.
  • data: the scene's model, any JSON value.
  • reducedMotion: whether to draw without movement (below).
  • fonts: the faces the scene asked for (below).

state, whenever the slide moves:

{type: "state", state: "city", animate: true}

With animate: true the scene moves from where it is to the new state. With animate: false (a jump, or going back several presses) it draws the new state at once.

A frame can outlive its slide. When the next slide shows the same scene in the same place (the same box on a canvas slide, the same region on a flow slide), Slides keeps the frame and sends it the new slide's state, which may be the state it is already in. Anywhere else the next slide starts a frame of its own, with its own init.

A slide can show one scene in several blocks, and each runs a frame of its own. They are paired with the next slide's blocks of that scene in order, the first with the first and the second with the second, and a pair in the same place carries on. Reordering two blocks of one scene starts both afresh.

ready, from the scene, once the first state is drawn:

parent.postMessage({type: "ready"}, "*");

Send it once. Slides ignores any later one, and any other message but one: {type: "error"} ends the scene, and the state's still shows in its place.

Both sides check where a message came from. Slides takes only messages whose source is the frame's own window; a scene should take only messages whose event.source is parent. Post to "*": the scene's origin matches nothing, so no other target origin works.

Words, data and fonts

A scene's words and data live in the deck, not in its code. People change a label or a value in the editor, and agents change them with set_scene, without touching the bundle. Draw every piece of text from words, and everything that varies between states from data. A good shape for data is the scene's pieces plus a table of what each state shows. Work out anything costly, such as routes, when the data is made, never while the scene runs.

The frame can't load the deck's fonts. To use one, list its family names, as the deck's fonts list them, in data.fonts:

"data": {"fonts": ["Brand", "Brand Mono"], …}

init then carries each face of those families as a data: URL:

fonts: [{family: "Brand", src: "data:font/woff2;base64,…",
         weight: "400", style: "normal"}, …]

Add each one before drawing text:

for (const f of msg.fonts) {
  const face = new FontFace(f.family, `url(${f.src})`, {weight: f.weight, style: f.style});
  document.fonts.add(await face.load());
}

weight is a CSS weight or, for a variable face, a range such as "100 900". Families the deck doesn't hold are not sent; system font names still work in CSS.

Reduced motion

Readers who asked for less motion don't get the scene at all: Slides shows them each state's still, which is that state at rest. So on a slide reducedMotion is always false. Handle true anyway, for previews: draw every state change as animate: false would, with no movement and at most a short fade.

Ready within 3 seconds

Until ready arrives the frame isn't shown; the state's still is. If ready hasn't arrived 3 seconds after init, Slides gives the scene up and the still stays. A scene that throws, hangs or never answers costs the reader nothing else: presses still count and the talk moves on. Load fonts and build what you draw before answering, and keep that under a second.

Stills

Give every state a still picture, PNG or WebP, at most 4 MiB. The PDF, thumbnails, the phone view, the presenter's Next preview and readers with scripts off show the still, not the scene. A state with no still shows a plain panel with the scene's name and the state's description.

Limits

| What | Limit | |---|---| | Bundle | one HTML document, 2 MiB | | data | 960 KiB as compact JSON, and its change one live update (1 MiB) | | Still | PNG or WebP, 4 MiB, 30000 pixels a side and 50 megapixels in all | | ready | 3 seconds after init |

Number-heavy data encodes larger than its JSON. A batch that adds or changes a scene becomes one live update, which may be 1 MiB at most, and each number in data takes up to nine bytes there however short it is written: 150,000 copies of 0.1 are 600 KB of JSON but a 1.35 MB update, and are refused (by the API with a 422 saying so, and by the editor before it writes anything). About 116,000 of them fit. Strings cost roughly what their JSON does.

Trying a scene

The editor's code panel previews unsaved code by posting it to POST /d/{sqid}/scenes/preview, which answers it under the same policy and stores nothing. It takes a text/html body, or a form (multipart/form-data or url-encoded) with one field, html, and no other. The text may be 2 MiB and the whole request 2 MiB plus 64 KiB, so post a large draft as multipart: url-encoding can triple its size. Only a signed-in person who can open the deck gets an answer.

Building a deck with a scene

An agent does it in five steps, all with its bearer token (api.md):

  1. Upload the bundle. POST /api/v1/decks/{sqid}/scene-bundles with the HTML as the body and Content-Type: text/html. It answers {"ref": "…"}.
  2. Add the scene and its slides in one batch. add_scene with that ref as its code, and its states, words and data; then the slides and their scene blocks. Give the scene an id of your own, twelve characters from a to z and 2 to 7, so the blocks after it in the batch can name it.
  3. Decide where it carries on. Two slides in a row whose blocks show the same scene in the same place (the same box on canvas slides, the same region on flow slides) share one frame. Start the second block at the state the first one ends on. A block over the scene's box with a higher z makes it start afresh on each slide.
  4. Work out the version from the code ref and the data and words you sent (api.md says how). Sending the same bundle to PUT /api/v1/decks/{sqid}/scenes/{id}/code answers it as well.
  5. Draw a still for each state and upload it. Serve the bundle with the header above, frame it at 1920 by 1080, send it init for the state with reducedMotion: true, wait for ready and take a picture of the frame. Then PUT /api/v1/decks/{sqid}/scenes/{id}/stills/{state}?made={version}. A 409 stale means the scene changed after you worked the version out: read the deck again and draw again.

To check the result, GET /api/v1/decks/{sqid} shows each still's made equal to the version, and GET …/slides/{id}.svg shows each slide as the PDF will.

The example

scenes/example.html expects data.states to list the states in order with a colour each, and a word per state:

-d '{"ops":[{"op":"add_scene","name":"Boxes","code":"<ref>",
             "states":[{"id":"one","name":"One","description":"A red box"},
                       {"id":"two","name":"Two","description":"A red box and a blue box"}],
             "words":{"one":"First","two":"Second"},
             "data":{"states":[{"id":"one","colour":"#c0392b"},
                               {"id":"two","colour":"#2471a3"}]}}]}'

On init it draws a box for each state and hides the ones after the current state; on state it shows or hides boxes, sliding them in when told to animate and motion is allowed. It answers ready after the first init.