Stage and scenes
createEngine
Section titled “createEngine”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 |
The stage
Section titled “The stage”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.widthandstage.height; both change when the window is resized. - Positions are in stage units, from
(0, 0)at the top left,ydownwards. 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
fisf.x * stage.width,f.y * stage.height.toScreenresults, 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.safeis 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 } = engineconst 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' }))The loop
Section titled “The loop”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)getsdt, the seconds since the last frame, andtime, the game time in seconds. It runs only while the game is not paused. Move everything bydt(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).dtis 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.timeis the game time, andengine.pausedwhether the game is paused.
Pausing
Section titled “Pausing”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 stopengine.resume() // the host's pauses still holdYou don’t need play.on('pause', …) for the engine’s sake; listen to it only for your own logic.
Ending
Section titled “Ending”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.
Scenes
Section titled “Scenes”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.
HTML over the canvas: engine.hud
Section titled “HTML over the canvas: engine.hud”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).
Other things on engine
Section titled “Other things on engine”| 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 |