Players and avatars
Each child picks an avatar on the phone: a fox, a robot, a little dragon, and so on (37 of them, 13 for each age
range). When the game paints
its own background (backdrop 'none'), draw each child as their avatar, posed like them. The avatars come with the
SDK, so the script tag is enough and there is nothing to import.
let players = []FabiPlay.on('players', (e) => (players = e.players))
function draw() { paintSky() // Only over your own background: with 'video' the children are on screen for real. if (backdrop !== 'video') for (const p of players) FabiPlay.drawAvatar(ctx, p) drawStarsAndScore()}When to draw avatars: only when backdrop is not 'video'. In mirror mode the children
are in the video; an avatar on top would hide them. Switch with the backdrop, mid-game too.
drawAvatar
Section titled “drawAvatar”FabiPlay.drawAvatar(ctx, frameOrBody, player?, box?, options?), or play.drawAvatar with the npm package. It draws
on a Canvas 2D context.
frameOrBody: a player of the frame (players[i]: its body and its avatar), or a bare body.player: whose avatar it is, as a player id (1), or an avatar id ('panda'). It also selects whose smoothing to use. Leave it out for a frame.box: where player space lands on the canvas,{ x, y, w, h }in the canvas’s drawing units: a body pointplands on(x + p.x·w, y + p.y·h). By default it is theview(where the camera image is, with any backdrop), so each avatar stands where its child stands, at the child’s size, and its hands are attoScreen(wrist). Without a view (older hosts) it is the whole canvas. AsetTransformscale fordevicePixelRatiois taken into account. To draw the child somewhere else, pass your own box.options:alpha, from 0 to 1;smooth: on by default (true, which takes 0.5 of each new frame). Pass a number (how much of each new frame to take, 1 = none) orfalse. Smoothing runs per player, so the avatar doesn’t jitter and a limb the camera loses for a moment doesn’t blink. It advances once per new body, however often you draw;interpolate: on by default (see below).
It returns the body it drew the avatar with (smoothed), or null if it drew nothing (shoulders not seen).
At the display’s rate
Section titled “At the display’s rate”The camera sends about 30 bodies a second; the screen shows 60 frames or more. Call drawAvatar every animation
frame (requestAnimationFrame), not on players events: each frame it draws the body interpolated for that frame,
between the camera’s last two, about one camera interval (33 ms) behind the newest, so the avatar moves smoothly
instead of in steps. A player gone for a while comes back where they are, without sliding in.
{ interpolate: false } draws the newest body as it came. Gestures and shapes are not affected: the host reads them
on the camera’s own bodies.
Size and visibility
Section titled “Size and visibility”The avatar’s size follows the child’s body: it is measured from the torso, so it is as big as the child is in the camera image, and two children standing apart are two avatars apart. Nothing is drawn while the camera can’t see the shoulders. Missing elbows, knees, wrists and ankles are handled. Draw avatars before the things the children catch, so those stay on top.
Hands in the avatar’s hands
Section titled “Hands in the avatar’s hands”The avatar is drawn from a smoothed body, so it lags the raw one a little. Put glows, hand rings and hit-tests on the same smoothed body, or they float next to the avatar’s hands.
FabiPlay.smoothedBody(players[i])(play.smoothedBody) smooths a frame’s body exactly asdrawAvatardoes: one step per new body, shared withdrawAvatar, so calling both is fine, in any order.smoothedBody(playerId)gives the last one, ornull.- In an animation frame it is the same interpolated body
drawAvatardraws in it, so a hand catches where the child sees the avatar’s hand, 60 times a second. - Pass
{ smooth, interpolate }as todrawAvatarif you changed them there.
Over the video, use the frame’s own body: the children are real there.
// once per animation frame, over your own backgroundfor (const p of players) { const body = FabiPlay.smoothedBody(p) // for the catches... if (!body) continue const wrist = FabiPlay.toScreen({ x: body[15 * 3], y: body[15 * 3 + 1] }) catchAt(p.id, wrist)}for (const p of players) FabiPlay.drawAvatar(ctx, p) // ...and the drawing: the same bodyavatarOf
Section titled “avatarOf”FabiPlay.avatarOf(player) (play.avatarOf) gives a player’s avatar id, by player id or from a frame. Use it to
show a portrait in a scoreboard you draw yourself, or to pick a colour.
Until the first frame, from an older host, and for ids this SDK doesn’t know, it is the slot’s default: fox, lion, dragon and unicorn for players 1 to 4. Two children may pick the same avatar.
Without the SDK
Section titled “Without the SDK”Each player of a frame message carries avatar, the id of their avatar (kebab-case, at most 32 characters). It
travels on every frame, so a change of players mid-game needs no other message. Unknown ids should fall back to the
slot’s default. Hosts from before avatars don’t send it. See Other engines.