DocsShip

Web runtime

The web runtime is one script with no dependencies. It renders a document to plain DOM elements and runs it: states, data, events, animation, transitions, overlays, focus and translations. The editor’s canvas uses the same renderer.

Install

<div id="ui" style="position: fixed; inset: 0"></div>
<script src="envy-runtime.js"></script>
<script type="module">
  const doc = await fetch("ui/my_ui.envy.json").then((r) => r.json());
  const ui = Envy.mount(document.getElementById("ui"), doc, { screen: "02 Main menu" });
  ui.on("new_hunt", () => game.startHunt());
  ui.setData("player.hp", 40);
</script>

envy-runtime.js defines a global Envy with mount, loadProject and version (the runtime’s version as text). It comes with every web and AI handoff export, and the web export dialog has envy-runtime.js only. Envy.mount also takes the document as JSON text. In a bundled game, the generated TypeScript wrapper (web/<name>-ui.ts) gives you typed events, screens and data paths.

The runtime doesn’t load Google Fonts. It registers the fonts the document embeds; for any other family it uses the family name with a system-ui fallback, so the page has to provide that font. The Web HTML export links the Google Fonts it needs.

Options

Option Default Meaning
screen First screen Screen to show first, by name or id. Without it, the first screen that isn’t a component sheet
data Sample data Initial data
locale Base text Locale for translated text, or "pseudo"
scaleMode "fit" Scale the design resolution into the element, or "none"
autoplay true Play autoplay animations when a screen shows
gamepad true Gamepad navigation on the first connected gamepad: the d-pad or left stick moves focus (a held direction repeats after 400 ms, then every 120 ms), A presses, B closes the top overlay
cancelClosesOverlay true Escape closes the top overlay (presses its focus.cancel button when it has one) while focus is in the player or on the page body. See Escape in your own code
respectReducedMotion true For reduced motion: skip screen transitions, show autoplay entrances at their end pose, don’t start autoplay loops
remoteImages false Load images from web addresses. Off by default, so a document can’t make the page fetch from other origins
safeArea The browser’s env(safe-area-inset-*) Insets in pixels for screens set to keep inside the safe area, as { left, top, right, bottom }
normalized false The document is already normalized and won’t change while the player runs, so it isn’t copied
muted false Start with sounds off. setMuted() changes it later
onSound(sound, player) Takes sounds over: every sound comes here and the player plays nothing. See Sound
liveInput false A text input writes its data on every keystroke instead of on Enter or when it loses focus
onEvent(event, payload, player) Called for every emitted event
onScreenChange(screen, player) Called when a new screen shows
onDataChange(data, player) Called when data changes

The player

Member Does
goto(screen, { transition }) Shows a screen by name or id. { transition: { type: "none" } } switches at once
push(screen, { transition }), pop(), cancel(), stack Overlays. cancel() does what Escape does
screen The screen shown
focusFirstButton() Focuses the screen’s initial button, else the first one, so keys and gamepads start there
setData(path, value), setData(patch) Changes data
getData(path), data A value, or all data with no path
play(anim), stop(anim), isPlaying(anim) Animations. stop() with no name stops them all
setLocale(locale), locales, locale Language. setLocale returns the locale it matched, or null for the base text. Switching changes the text only
on(event, fn), off(event, fn), emit(event, payload) Events. fn gets (payload, event), "*" listens to all, and on returns a function that stops listening
typing True while the player types in a text input. Hold your own shortcuts then
muted, setMuted(on), playSound(asset, volume) Sound. See Sound
layout(), destroy() Rescale after a resize you manage, unmount

goto, push, pop, cancel, play and focusFirstButton return true when they worked.

Escape in your own code

If your game also handles Escape, for example to open Pause, skip the key when the player has already used it:

addEventListener("keydown", (e) => {
  if (e.key !== "Escape" || e.defaultPrevented) return; // the player is closing an overlay
  if (ui.stack.length === 1) ui.push("Pause");
});

The player sees Escape before your handler and marks it defaultPrevented while an overlay is open. It closes the overlay after your handler has run, so a handler that closes it itself still works. To handle Escape only in your own code, pass cancelClosesOverlay: false.

Sound

Sounds come from the document: the Play sound action and the Hover, Press and Focus sounds of a layer (see Hover, tooltips and sound). Envy only triggers them and your game owns the mix. By default the player plays each one on a pool of six Audio elements, which play whatever formats the browser plays. Browsers keep audio quiet until the first click or key press, and a blocked sound is skipped without an error.

  • muted: true, or ui.setMuted(true), silences the player’s own playback.
  • ui.playSound(asset, volume) plays a sound of the document by its asset id, with a volume from 0 to 1.
  • onSound takes sounds over. Nothing plays, and your function gets { asset, name, src, volume } for each one, so your audio code can mix it. It still gets them while the player is muted.
const ui = Envy.mount(host, doc, {
  onSound: (sound) => myAudio.play(sound.src, { volume: sound.volume }),
});

Security

The runtime treats a document as data. Colors are validated, stroke markup is built with DOM calls instead of HTML strings, and embedded fonts load only from data: or blob: URLs or same-origin paths, so a document can’t make the page fetch fonts from other origins. Images from web addresses load only when you set remoteImages: true.

Project files

Envy.loadProject(source) reads a .envy project from a URL, bytes or a Blob, and returns the document with its assets embedded. Mount that document as usual.

Size

The runtime is small enough to ship with a menu: see the runtimes page for the current size.

Updated View as Markdown