Play
The game’s side of Fabi Play, from connect(): who plays, their movements, and how the game tells it is ready, the scores and the end.
Example
Section titled “Example”const play = await connect()play.on('gesture', (g) => g.gesture === 'jump' && hop(g.player))play.ready()Properties
Section titled “Properties”locale
Section titled “locale”
readonlylocale:Locale
The language to show text in.
readonlymode:"tv"|"solo"
‘tv’: on a TV or Chromecast, children standing back; ‘solo’: a tablet in front of one child.
players
Section titled “players”
readonlyplayers: readonlynumber[]
The ids of the players playing now (1, 2, …), as frames and gestures name them; a frozen array, read it again after a turn. With input ‘body’ the same for the whole game. With input ‘remote’ exactly one child, and it changes only inside the break of a break(‘turn’) (the ‘turn’ event, between break-start and break-done). Keep each player’s score by id across turns, and end with the scores of everyone who played.
readonlyturns: readonlynumber[]
Every child who plays this game, in turn order (frozen): with input ‘remote’ and several children, one round each, players being the one playing now (turns[0] at the start). Only for games whose manifest says “turns”: true (with “remote” in inputs); otherwise remote mode is one child. The contract:
- play a round with the child in players;
- at the end of it (whatever happened: a crash ends a round too), if turns remain, call break(‘turn’): the screen always answers break-start (its turn screen), ‘turn’ (the next child), then break-done;
- after the last child’s round, call end() with every child’s scores. ‘level’ and ‘gameover’ breaks never swap players. With ‘body’, one child, or hosts that don’t say: players.
readonlyinput:GameInput
How this session is played: ‘body’ (the camera on the phone; the default, and what older hosts mean) or ‘remote’ (a TV without a phone, only for games whose manifest lists it in inputs): the screen turns the remote’s keys into the same gestures (↑ or OK jump, ↓ held squat then stand, ← → lanes, OK twice clap) and moves a puppet body in the frames, one child at a time. Adapt if it helps: slow down, show a selection the arrows move instead of a hand cursor.
pointer
Section titled “pointer”
readonlypointer:boolean
Only with input ‘remote’: the remote also points (LG’s Magic Remote), reaching the game as ordinary pointer and mouse events (hover, click with OK). Then let what the game has to pick be clicked too (large hit areas, a hover highlight). Also true for a game whose manifest says “remoteCursor”: true, when the arrows move a cursor: the SDK makes the same pointer and mouse events from it (OK held: pointerdown … pointerup, then click), so a game written for the pointer works with both. false otherwise, and from hosts that don’t say. Not the pointer() adapter, which makes a child’s hand the mouse.
canSavePicture
Section titled “canSavePicture”
readonlycanSavePicture:boolean
Whether savePicture can reach a phone now: true when the screen says a phone that saves pictures is linked (the app; the web phone can’t). It can change mid-game: listen to ‘capabilities’. Hide or grey a “save” button while it is false, and never make the children wait on it.
orientation
Section titled “orientation”
readonlyorientation:Orientation
The screen’s orientation when the game started: ‘landscape’ (width ≥ height; the TV, the tablet, and what older hosts mean) or ‘portrait’ (a phone held upright, only for games whose manifest lists it in orientations). Use it to choose a layout, but size everything from the real viewport (innerWidth × innerHeight): it is not updated if the screen turns.
safeArea
Section titled “safeArea”
readonlysafeArea:Readonly<SafeArea>
What a phone’s notch, rounded corners and home bar cover, in fractions of the viewport: top and bottom of its height, left and right of its width, each 0–0.25. The game’s frame can’t read env(safe-area-inset-*), so the screen says it. Keep buttons, scores and targets out of it (e.g. a top bar from safeArea.top × innerHeight down); still paint the background under it. Zeros (frozen) on a TV, a tablet, and from hosts that don’t say.
readonlymuted:boolean
The screen’s sound is off: start the game muted.
ageRange
Section titled “ageRange”
readonlyageRange:AgeRange
The players’ age range: simpler rules for 3-5, scores and challenges for 9-12.
backdrop
Section titled “backdrop”
readonlybackdrop:Backdrop
What is behind the game, now: ‘video’ (mirror mode: the players’ camera video fills the screen under the game; paint no opaque background, only what is on top) or ‘none’ (paint your own background). Only games whose manifest says “backdrop”: [“video”] ever get ‘video’; it can change mid-game.
readonlyview:BackdropView|null
Where the whole camera image is, in fractions of the game’s viewport, with any backdrop. The screen decides it: with ‘video’ where the video is drawn, with ‘none’ where the avatars stand. null if the host didn’t say (older hosts). Use toScreen rather than reading it, and don’t compute your own.
drawAvatar
Section titled “drawAvatar”drawAvatar:
DrawAvatar
Draws a player’s avatar posed like their body, smoothed per player. who: a frame (its body and avatar) or a body; player: a player id (whose avatar, and whose smoothing) or an avatar id. box: where player space (0–1) lands on the canvas, in its drawing units; by default the view (where the child stands), else the whole canvas. Draw avatars only when backdrop is not ‘video’: in mirror mode the children are on screen themselves. Draws nothing when the shoulders aren’t seen. Returns the body it posed the avatar with (smoothed), or null if it drew nothing: put hands and effects on that body’s points, so they stay in the avatar’s hands. Call it every animation frame: the body is interpolated for that frame between the camera’s last two (about one camera interval, 33 ms, behind the newest), so the avatar moves at the display’s rate, not the camera’s ({ interpolate: false } for the newest).
smoothedBody
Section titled “smoothedBody”smoothedBody:
SmoothedBody
A player’s body smoothed as drawAvatar draws it, for hit-tests and effects in avatar mode: from a frame, it smooths that frame’s body (once per new body, shared with drawAvatar); from a player id, the last smoothed body, or null. Options: { smooth, interpolate } as in drawAvatar: by default the same interpolated body drawAvatar draws in this animation frame, so hit-tests catch where the child sees the avatar’s hand.
device
Section titled “device”
readonlydevice:DeviceHint|null
What the screen knows about the device (DeviceHint): a starting quality tier and the display’s rate, Hz. null if it said nothing (older hosts, a plain browser). A hint for engines: start at this tier instead of measuring the device again (@fabiplay/engine does it by itself), and still lower it if frames are slow.
localPoses
Section titled “localPoses”
readonlylocalPoses:boolean
The bodies come from a camera on this same device (a phone or a tablet playing alone), not from a phone over the network. drawAvatar and smoothedBody then smooth and interpolate lighter by default (less latency, as there is little jitter to absorb): LOCAL_SMOOTHING (0.8) instead of 0.5, half an interval behind the newest body instead of one. A smooth amount the game gives (a number, or false) still wins. false from hosts that don’t say.
Methods
Section titled “Methods”savePicture()
Section titled “savePicture()”savePicture(
source,opts?):Promise<PictureResult>
Saves a picture on the family’s phone, in its gallery (a drawing, the photo of a wall). source: a canvas (scaled down to MAX_PICTURE_SIDE, 1920 px on its longer side, drawn on white and encoded as JPEG at quality 0.85, lower if it would be over 3 MiB) or a data URL (data:image/jpeg or data:image/png, base64, sent as it is). title: a name for it (up to 60 characters). Resolves with what became of it: ‘sent’ (the phone has it), ‘unavailable’ (no phone that saves pictures: canSavePicture is false), ‘too-big’ (over 3 MiB), ‘error’ (a canvas that can’t be read, not an image, the link dropped, no answer in 2 minutes). Never rejects.
Privacy: the picture goes only from the TV to the phone over their direct link (WebRTC, encrypted), never to a server, and nothing else can be sent this way. If the phone can’t save it (permission denied), Fabi Play tells the parent on the phone: don’t tell the children.
Parameters
Section titled “Parameters”source
Section titled “source”title?
Section titled “title?”string
Returns
Section titled “Returns”Promise<PictureResult>
Example
Section titled “Example”if (play.canSavePicture) await play.savePicture(canvas, { title: 'Our wall' })toScreen()
Section titled “toScreen()”toScreen(
p):Point
A body point (player space, 0–1, as in frame.players[i].body) on the game’s viewport, in fractions: where the child is, in the video or on the avatar. Draw hands and effects on the children at toScreen(wrist), in every mode. Unchanged without a view.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”aim(
player,side):Point|null
Where a player’s hand aims on the game’s viewport, in fractions (0–1), by player id (their newest frame) or from a frame: use it for what the hand touches, catches, presses; toScreen(wrist) only to draw on the body. One rule for every game, the protocol’s aimPoint: in landscape (a TV, a tablet) the wrist through toScreen, on the avatar’s hand (its smoothed body) or, over the video, on the real one; in portrait (a phone held upright, where the camera image is small at the bottom and raised hands leave it) the wrist stretched around the shoulders to the whole screen (as PlayerFrame.leftHand), so arms straight up reach the top (y ≈ 0.15) and arms out the edges, held 300 ms when the camera loses the wrist. null when the hand isn’t seen.
Parameters
Section titled “Parameters”player
Section titled “player”number | PlayerFrame
Returns
Section titled “Returns”Point | null
Example
Section titled “Example”const p = play.aim(1, 'right')if (p && hit(star, p.x * innerWidth, p.y * innerHeight)) pop(star)avatarOf()
Section titled “avatarOf()”avatarOf(
player):string
The avatar id a player picked on the phone (fox, lion, robot, …), by player id or from a frame. Each frame carries it; before the first frame, from an older host, or for an id this SDK doesn’t know: the slot’s default (fox, lion, dragon, unicorn for players 1–4: the 6–8 band’s).
Parameters
Section titled “Parameters”player
Section titled “player”number | PlayerFrame
Returns
Section titled “Returns”string
reportStats()
Section titled “reportStats()”reportStats(
stats):void
For engines, debug only: how the game draws now (fps, renderer, tier). The SDK adds the last one to its stats messages when a debug host (the simulator) asked for them, and drops it otherwise. @fabiplay/engine calls it by itself about once a second.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”void
onBackdrop()
Section titled “onBackdrop()”onBackdrop(
listener): () =>void
Called when backdrop or view changes; returns a function that stops listening.
Parameters
Section titled “Parameters”listener
Section titled “listener”(backdrop, view) => void
Returns
Section titled “Returns”() => void
on<
K>(event,listener): () =>void
Listens to the host: ‘frame’, ‘gesture’, ‘pause’, ‘resume’, ‘break-start’, ‘break-done’, ‘backdrop’, ‘turn’, ‘capabilities’, ‘cursor’ (see Events). Returns a function that stops listening.
Type Parameters
Section titled “Type Parameters”K extends keyof Events
Parameters
Section titled “Parameters”K
listener
Section titled “listener”(m) => void
Returns
Section titled “Returns”() => void
Example
Section titled “Example”const off = play.on('frame', (f) => (players = f.players))ready()
Section titled “ready()”ready():
void
Assets loaded, the game can start: the screen hides its loader. Call it within 10 seconds of the page loading.
Returns
Section titled “Returns”void
score()
Section titled “score()”score(
player,score):void
A player’s score so far, for the screen to show.
Parameters
Section titled “Parameters”player
Section titled “player”number
number
Returns
Section titled “Returns”void
break()
Section titled “break()”break(
reason?):Promise<boolean>
The game has stopped by itself: a level is over, the game is over, or (with the remote, see turns) a child’s round is over and the next child’s turn is due: ‘turn’ (never at its pause menu). Fabi Play may show an ad there, if its rules allow (break-start, then break-done; with ‘turn’, the turn message in between); resolves when the game can go on, true if an ad was shown. Never call it in the middle of play.
Parameters
Section titled “Parameters”reason?
Section titled “reason?”Returns
Section titled “Returns”Promise<boolean>
end(
scores):void
The game is over: each player’s score and stars (0–3). The screen shows the results and takes over.
Parameters
Section titled “Parameters”scores
Section titled “scores”object[]
Returns
Section titled “Returns”void
Example
Section titled “Example”play.end([{ player: 1, score: 120, stars: 3 }])