Skip to content

Stage and scenes

const engine = await createEngine({ play })

createEngine(options) makes the engine and resolves in under a second (it may measure the device first: see Renderers and quality tiers). Then set up the scene and call engine.start(). Every option is optional:

Option Default What it does
play none The SDK’s Play (from connect()) or window.FabiPlay. Without it there are no hands or avatars
canvas a new one The canvas to draw in. By default one is made to fill the page (position: fixed, the whole viewport)
background transparent The stage’s colour when there is no video behind it (the page shows through otherwise)
renderer 'auto' Force 'webgl2', 'webgl1', 'canvas2d' (also ?renderer= in the address)
tier measured Force 'high', 'medium' or 'low' (also ?tier= in the address)
probe true Measure the device when there is no cached tier; false starts at medium
maxPixelRatio 2 The cap on devicePixelRatio
failIfMajorPerformanceCaveat true Refuse slow (software) WebGL

engine.stage is the root group: add nodes to it to see them.

  • It is 720 units high on every screen. Its width follows the viewport’s aspect: 1280 at 16:9 (a TV), 960 at 4:3 (a tablet). Read it as stage.width and stage.height; both change when the window is resized.
  • Positions are in stage units, from (0, 0) at the top left, y downwards. A unit is not a pixel: the engine sizes the canvas’s backing store for the device and the tier, and the browser stretches it to the viewport.
  • The stage fills the game’s viewport, so the SDK’s screen fractions map onto it directly: a fraction f is f.x * stage.width, f.y * stage.height. toScreen results, and any game logic you keep in screen fractions, are multiplied the same way. The engine’s own hands and avatars are already in stage units.
  • stage.safe is the 16:9 area in the middle, { x, y, w, h }: put what must be seen on every screen inside it, and anchor the HUD to the stage’s edges.
  • STAGE_HEIGHT (720) is exported too.
const { stage } = engine
const title = stage.add(engine.text('Ready?', { x: stage.width / 2, y: stage.height / 2, size: 80 }))
const corner = stage.add(engine.text('0', { x: stage.safe.x + 40, y: 50, align: 'left' }))

engine.start() starts the loop: one requestAnimationFrame, drawing the stage every frame. In each frame:

const stop = engine.onFrame((dt, time) => {
// game logic: runs only while the game is not paused
})
engine.onRender((dt) => {
// runs every frame, paused or not, before drawing
})
  • onFrame(fn) gets dt, the seconds since the last frame, and time, the game time in seconds. It runs only while the game is not paused. Move everything by dt (x += speed * dt), never by a fixed step per frame: TVs run at 50 Hz, 60 Hz or less.
  • onRender(fn) runs every frame regardless, before drawing: for things that move during a pause (a pulsing “paused” label).
  • dt is capped at 0.1 seconds, and reset after the page was hidden, so a game never jumps ahead.
  • Both return a function that stops the listener.
  • engine.time is the game time, and engine.paused whether the game is paused.

The engine follows Fabi Play by itself: while the host pauses the game (from the phone) or shows a break, game time, onFrame, tweens, particles and sound stop. Drawing continues, so the stage stays on screen.

For the game’s own pauses (between levels, a countdown):

engine.pause() // game time, tweens, particles and sound stop
engine.resume() // the host's pauses still hold

You don’t need play.on('pause', …) for the engine’s sake; listen to it only for your own logic.

engine.destroy() stops the loop and frees the renderer, the sounds and the listeners. Usually there is no need: when the game ends (play.end(...)) Fabi Play takes over the screen.

The engine has no scene manager: a scene is a group. Make one group per screen of your game, show one at a time, and clear or destroy the others:

const menu = stage.add(engine.group())
const level = stage.add(engine.group({ visible: false }))
async function startLevel() {
await engine.tween(menu, { alpha: 0 }, 0.3)
menu.visible = false
level.visible = true
}

A hidden group and its children are not drawn. group.clear() destroys every child; group.destroy() the group too.

engine.hud is an HTML layer over the canvas, made on first use, for text-heavy or interactive overlays (a results table, a settings screen). It ignores the pointer (pointer-events: none): set pointer-events: auto on what should take clicks. Its CSS variable --unit is one stage unit in CSS pixels, so calc(48 * var(--unit)) matches a 48-unit engine text.

const banner = document.createElement('div')
banner.textContent = 'Level 2'
banner.style.cssText = 'position:absolute;top:calc(40 * var(--unit));width:100%;text-align:center;font-size:calc(64 * var(--unit))'
engine.hud.appendChild(banner)

Keep everything that moves on the canvas: dozens of moving HTML elements are slow on TVs (see Performance tips).

Field What it is
engine.canvas The canvas drawn in (after a switch to Canvas 2D on context loss, a fresh one in its place)
engine.pixelScale Backing-store pixels per stage unit
engine.players() The live player frames (the newest from the host; empty if none for a second)
engine.random(min, max) A random number between min (0) and max (1)
engine.stats What the last second cost: see Renderers and quality tiers