Files
BFlat cfbf83c158 Merge pull request #2802 from storytold/feat/2539-present-entry
M5: the browser build presents a prototype file from its own site, view only, from a link
2026-12-01 10:34:40 +07:00

19 KiB
Raw Permalink Blame History

Hosting FigCraft for the web

figcraft-web-<version>.zip (from the GitHub release, or packaging/web/package.sh) holds a static site in figcraft-web-<version>/:

File What it is
index.html The page. It loads everything through relative URLs.
figcraft-web-<hash>.js wasm-bindgen glue (generated, ES module)
figcraft-web-<hash>_bg.wasm The app: about 18 MiB raw, 7 MiB with gzip, 4.5 MiB with Brotli (see Sizes)
_headers, .htaccess Sample header rules for Netlify/Cloudflare Pages and Apache

There is no server-side code and no hand-written JavaScript: the glue is generated by wasm-bindgen from the Rust sources (apps/figcraft-web). Upload the folder's contents anywhere that serves static files.

The editor runs entirely on the visitor's device. The web build starts with one new document; there is no desktop TCP control server, and native file dialogs aren't wired up yet. A browser with WebGPU or WebGL2 and WebAssembly SIMD is required (any current browser; Safari 16.4 or later).

File and image formats

Sketch import and Penpot import/export require the desktop app. To use those documents in the browser, open them on desktop and save as .figcraft. Browser conversion requests report this requirement instead of producing an incomplete document. Native .figcraft, .fig, SVG and PDF support is retained.

The browser package keeps PNG, JPEG, WebP, GIF and the common bitmap codecs. Less-common formats (TIFF, OpenEXR, JPEG 2000, JPEG-LS, DDS, DICOM, DPX/Cineon, FITS, IFF, Netpbm, TGA and XPM) require the desktop app. Convert those images to PNG, JPEG or WebP before importing them in the browser. Existing native documents keep their original image bytes and stored dimensions when saved in the browser; images in these omitted formats require the desktop app to display. Desktop format support is unchanged.

Fonts

The web build draws text with the fonts it was built with. In browsers with Local Font Access (Chromium: Chrome, Edge, Opera), the font menu's "Use fonts installed on this computer…" adds the computer's fonts after the browser asks for permission; each is read only when text uses it.

Live sessions

The web build can join a live session hosted by the desktop app (File ▸ Join live session…, then paste the invite link), join files on a figcraft-server, and publish files to one (Share ▸ Online with the server in Preferences, or Share ▸ Publish to server…); it can't host a session itself. Desktop-hosted sessions use plain ws://, which browsers block from a page served over https:// except to the same computer. To join sessions on other machines, serve the site over http:// on a trusted network (or use the desktop app); wss:// comes with the standalone server.

A site served beside its own figcraft-server can name it in index.html: <meta name="figcraft-server" content="wss://figcraft.example.com/"> (a ws:// or wss:// address without a query). The page then uses it as the server for online sharing, join codes and the server's team list (Home ▸ Online), without anyone setting it in Preferences.

Sizes

Measured on the 0.1.0 build (trunk build --release; gzip -9, Brotli quality 11):

File Raw gzip Brotli
figcraft-web-<hash>_bg.wasm 18,534,563 bytes (17.7 MiB) 7,023,460 (6.7 MiB) 4,642,764 (4.4 MiB)
figcraft-web-<hash>.js about 148 KB about 22 KB

Per-file upload limits: Cloudflare Pages and Workers static assets reject any file over 25 MiB (26,214,400 bytes). packaging/web/package.sh fails the build if the wasm ever grows past 25 MiB, so a release can't ship a file a common host refuses.

Any path works

All URLs in index.html are relative (public_url = "./" in apps/figcraft-web/Trunk.toml), so the site works at a domain root (https://example.com/), under a prefix (https://example.com/tools/figcraft/) and from a CDN bucket. The asset names carry a content hash, so they can be cached forever. Only index.html needs revalidation.

Required server settings

  • MIME type: serve .wasm as application/wasm. Browsers refuse to stream-compile it under any other type, and the app then loads slowly or not at all. Serve .js as text/javascript. Most hosts already do both. For nginx, check that mime.types has application/wasm wasm;.
  • Compression: turn on gzip or Brotli for .wasm, .js and .html. That takes the download from about 18 MiB to about 7 MiB (gzip) or 4.5 MiB (Brotli). You can also precompress (brotli -k *.wasm) and let the server send Content-Encoding: br.
  • Caching: Cache-Control: public, max-age=31536000, immutable on the hashed .wasm and .js files, and no-cache on index.html.
  • HTTPS: WebGPU (and the clipboard) only work in a secure context, which means https:// or http://localhost. Over plain HTTP elsewhere, the app falls back to WebGL2.
  • No special isolation headers: FigCraft doesn't use SharedArrayBuffer, so it doesn't need Cross-Origin-Opener-Policy or Cross-Origin-Embedder-Policy. If your site already sends COEP require-corp, also send Cross-Origin-Resource-Policy: same-origin (or cross-origin when the files live on a CDN) on the app's files.

nginx example:

location /figcraft/ {
    types { application/wasm wasm; text/javascript js; text/html html; }
    gzip on;
    gzip_types application/wasm text/javascript text/html;
    location ~* \.(wasm|js)$ { add_header Cache-Control "public, max-age=31536000, immutable"; }
    location ~* index\.html$ { add_header Cache-Control "no-cache"; }
}

Local test: python3 -m http.server 8767 inside the folder, then open http://localhost:8767/.

Put .figcraft files next to the web build (or anywhere on the same site) and link to index.html#present=<file>&start=<frame>: the page downloads that file, opens it and presents its prototype full-window, with no editor behind it. A static folder of the web build and its files is then a set of prototype links.

  • present is the file's path, relative to the page (protos/App.figcraft) or absolute on the same site (/protos/App.figcraft); escape a & in it as %26. Only a file on the page's own site, over HTTP(S), is fetched; the page's cookies go with the request, as for the page itself, and a redirect to another site is refused.
  • start (optional) is the id of the frame to start at, as Cmd/Ctrl+L in the presenter gives it (figcraft://present/<id>); without it, or when the file has no such frame (the user is told), the prototype starts at the first flow of the page the file opens on, else its first frame.
  • The presenter is view only: no Close (Escape and Cmd/Ctrl+W do nothing), no comments, no device or responsive choice. Scale, hotspot hints, Inspect, restart and stepping between frames stay. Nothing is saved.
  • When the file can't be fetched or opened, or has nothing to present, the page says why.
  • The fragment isn't sent to the web host, but the address bar and history keep it. In host mode (?host=parent, data-host="page") the entry is ignored: the host sends the files.

Embedding in a page (iframe)

<iframe
  src="https://example.com/figcraft/"
  title="FigCraft interface design tool"
  style="width: 100%; height: 720px; border: 0;"
  allow="fullscreen; clipboard-read; clipboard-write"
  allowfullscreen>
</iframe>
  • The app fills the iframe and follows its size, so size the iframe and not the app.
  • Keyboard shortcuts go to the iframe after the user clicks into it, as with any embedded app.
  • Sandboxed iframes need at least sandbox="allow-scripts allow-same-origin allow-popups" (allow-popups lets Help links open in a new tab).
  • Don't send X-Frame-Options: DENY or a frame-ancestors CSP that excludes the embedding page.

Host mode (?host=parent, or the page's own document)

A page can embed FigCraft so that it hands files over instead of downloading them and reports unsaved changes. Two opt-in transports carry the same messages:

  • ?host=parent on the iframe's URL (https://example.com/figcraft/?host=parent): a same-origin framing page talks to the app over postMessage. Opened outside an iframe, the URL gives the normal app with downloads; with a parent on another origin there is no host mode either (messages to another origin are dropped silently, so saves would be lost while the document looked saved).
  • data-host="page" (the exact value page) on the canvas: a page that loads the module into its own document gets the same behaviour over DOM CustomEvents on the canvas, with no iframe (see below).
Message Direction Fields
figcraft:ready app → page version, appVersion: the app is running and listening; send a file now
figcraft:open page → app name, bytes (an ArrayBuffer or Uint8Array): open like File › Open (a payload over the browser open limit of 512 MB is refused; anything malformed is ignored)
figcraft:save app → page name, saveAs, bytes (a transferred ArrayBuffer), and id for a document save to a page that confirms saves (below): every file the app would otherwise download. The page stores it and reports failures itself. saveAs says whether the file becomes the document's file: false after Save of the file the page sent, under its own name; true for a document file (.figcraft, .json) under a name the page didn't send — Save As, or a Save of a document the page never sent — after which the next Save carries that name with false; false for copies and exports that don't become the document (rendered images, SVG, PDF, .fig copies, the variables exports)
figcraft:saved page → app id: the page stored the figcraft:save that carried this id (only with confirmed saves)
figcraft:dirty app → page dirty: unsaved changes appeared or went away
figcraft:failed app → page error, and id for a save the page didn't confirm in time: the app couldn't start (no WebGPU or WebGL2, for example), or that save counts as not stored
figcraft:publish page → app name (optional): publish the open file to the server the page names in <meta name="figcraft-server"> (below)
figcraft:published app → page file, name, edit, view once the server has the file (its id, its name there, and the links to edit and to view it), or error

Files reach the page through the app's download service, the path this web build saves by: Save As (and a Save of a document with no file yet), the .fig copy, the export panel and its dialog, Export frames as PDF, plugin output and the variables export (Variables panel ▸ Export…). In host mode each arrives as figcraft:save instead of a download.

In host mode the app starts with no document (the home screen), so the page sends the file to edit and guards leaving the page while changes are unsaved. &author= on the URL names the user for comments and board notes.

Confirmed saves

By default a file counts as saved as soon as the app posts it, so a page that fails to store it must say so itself. A page that wants the app to wait opts in with &save=managed on the URL (or data-save="managed" on the canvas in same-document mode). Then:

  • Each Save and Save As of the document carries an id (save-1, save-2, …, never reused on a page). The document keeps its unsaved changes (figcraft:dirty stays true) until the page answers figcraft:saved with that id. Edits made while the save was on its way stay unsaved after the answer: the document is saved as of the file the page stored.
  • A Save As (saveAs: true) becomes the document's file only once confirmed, so a Save after a failed Save As is still saveAs: true.
  • One save waits at a time. A newer save replaces it, and a figcraft:saved for any other id (an older save, a repeat) is ignored. A figcraft:open forgets the save still waiting: the user is told it wasn't saved, and the document it was for keeps its unsaved changes.
  • A save the page doesn't confirm within 30 seconds counts as not stored: the user is told, and the page gets figcraft:failed with that id.
  • Copies and exports (images, SVG, PDF, .fig copies, the variables exports) carry no id and saveAs: false, and count as delivered once posted, as before.

There is no message yet for a page to refuse a save with a reason; until the shared contract has one, a page that can't store a file leaves it unconfirmed and the save fails at the timeout.

Publishing to the page's server

A page served beside its own figcraft-server (named in <meta name="figcraft-server">, see Live sessions) can ask for the open file to be put on that server, so the team finds it there: send figcraft:open, then figcraft:publish ({ name }, or {} for the file's own name); a publish sent while that file is still opening waits for it.

  • The app opens File ▸ Publish to server… for the file in front, addressed to the page's server and with that name. The user still presses Publish: the page can't publish without them, as in the app. The dialog asks the server whether it is open to the public and shows its terms; a server that needs its key takes it only when the user types it into the address.
  • The page gets figcraft:published with file, name, edit and view once the server has the file. A file put on an open server without the key also shows the user its owner token, once, as the dialog always does; the page never gets the token.
  • Otherwise it gets { error }, and nothing was published to its server: "cancelled" when the user closed the dialog (or switched files) without publishing; the reason when the page names no server, no file is open, another figcraft:publish still waits, the publish failed, or the user changed the address to another server (that file's links aren't sent to the page).
  • Only the server in the page's meta tag is ever used: a server or URL in the message is ignored.

Same-document host mode

The host application loads the wasm-bindgen module itself and gives FigCraft one of its own canvases, so there is no iframe and no ?host=parent. It opts in with data-host="page" on that canvas — the exact value page — and the canvas keeps the id the app looks up (id="figcraft_canvas"):

<canvas id="figcraft_canvas" data-host="page" style="width: 100%; height: 720px; display: block;"></canvas>

The page and the app then talk with DOM CustomEvents on the canvas instead of postMessage. The app dispatches the same messages it would post to a parent — figcraft:ready, figcraft:save, figcraft:dirty, figcraft:failed, figcraft:published — with the message fields (without the type) as the event's detail, and it opens a document when the page dispatches a figcraft:open event with detail: { name, bytes } (and offers to publish it on figcraft:publish with detail: { name }). Events do not bubble, so listen on the canvas itself (two editors on one page can't hear each other).

Add the listeners before init: the app announces itself with figcraft:ready while init is still running, and a file sent in reply would otherwise be missed.

import init from "./figcraft-web-<hash>.js"; // the wasm-bindgen glue shipped with the app

const canvas = document.getElementById("figcraft_canvas");

canvas.addEventListener("figcraft:ready", async () => {
  // The app is listening; hand it a document now, if you have one.
  const bytes = await documentBytes(); // an ArrayBuffer or Uint8Array
  canvas.dispatchEvent(new CustomEvent("figcraft:open", {
    detail: { name: "Poster.figcraft", bytes },
  }));
});

// Every file FigCraft would otherwise download arrives here.
canvas.addEventListener("figcraft:save", (event) => {
  const { name, saveAs, bytes } = event.detail; // bytes is an ArrayBuffer
  // Store the file under `name`; `saveAs` says whether it becomes the document's file.
  // Report your own errors.
  storeFile(name, bytes, saveAs);
});
canvas.addEventListener("figcraft:dirty", (event) => {
  unsaved = event.detail.dirty; // warn before leaving, as you like
});
canvas.addEventListener("figcraft:failed", (event) => {
  showError(event.detail.error); // the app couldn't start
});

await init({ module_or_path: "./figcraft-web-<hash>_bg.wasm" });
  • Saves are not downloads in this mode: the bytes in figcraft:save hold the file, and the host decides where it goes.
  • ?author= on the page's URL still names the user for comments and board notes.
  • An unframed page that uses ?host=parent keeps the normal app with downloads. data-host="page" turns this mode on only when ?host=parent is absent: if ?host=parent is present it takes precedence, so a same-origin framed page uses the parent bridge, while an unframed or cross-origin page gets no bridge and keeps normal downloads.

The messages and both transports are the shared contract of craft-web-host (storytold/craft-libs#420), the same for every Craft app: its README lists every message, including saved, with the compatibility notes. In figcraft:ready, version is the contract's ("1") and appVersion is this app's release. FigCraft reads bytes, so a host sends bytes, not a File. figcraft:publish and figcraft:published are FigCraft's own (the contract has no message for a server): they travel the same way, with the same checks.

Trust and limits

  • Who is trusted. In page host mode the page and FigCraft share one document, so any script on the page can drive the app: open files in it, receive what it saves and offer to publish the open file to the page's server (the user still confirms). Use this mode only for a page you control. The iframe mode (?host=parent) answers only the parent window, on the page's own origin.
  • Files from the host are untrusted input. An opened file goes through the same reading as one picked with File ▸ Open or dropped on the window, with the same format limits; before it is copied into the app's memory, its size is checked against the host open limit (512 MB) and a larger file is refused, and a name is cut to 255 characters.
  • The event and message names are the interface. figcraft:ready, figcraft:open, figcraft:save, figcraft:saved, figcraft:dirty, figcraft:failed, figcraft:publish and figcraft:published, and the fields they carry (the same as the ?host=parent messages), stay stable; changing one is a breaking change.

Renderer selection and fallback flags

FigCraft renders with wgpu. It uses WebGPU when the browser has it and falls back to WebGL2 on its own. A URL query flag overrides this, and it works on the iframe src too:

Flag Effect
(none) WebGPU if available, otherwise WebGL2
?webgl Force the WebGL2 backend (useful when a WebGPU driver misbehaves, and in headless browsers)

For example: <iframe src="https://example.com/figcraft/?webgl" ...>.

A browser with neither WebGPU nor WebGL2 gets a message in place of the app.

Smoke test (headless Chrome)

Headless Chrome exposes navigator.gpu but its adapter request returns nothing, so force WebGL2 on SwiftShader:

"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless=new \
  --use-angle=swiftshader --enable-unsafe-swiftshader --window-size=1440,900 \
  --timeout=30000 --screenshot=figcraft-web.png "http://127.0.0.1:8767/?webgl"