Avatars
Each child picks an avatar on the phone (a fox, a robot, a little dragon…, 37 of them, 13 for each age range). Over your own background the children see themselves as their avatars, posed like them and standing where they stand.
In a game: engine.avatars()
Section titled “In a game: engine.avatars()”stage.add(engine.avatars())That is all. engine.avatars(options?) is a node that draws every player’s avatar:
- where the child stands, at the child’s size: in the
viewthe host sends (see Mirror mode, backdrop and view), or the whole stage without one. The node’s own position, rotation and scale are ignored; - posed from the smoothed body, interpolated at the display’s rate, exactly as the SDK’s
drawAvatarwould, soengine.hands()are in the avatars’ hands; - with the avatar each child picked, changing mid-game when a child picks another;
- nothing over the video: in mirror mode the children are in the video, and the node draws nothing until the
backdrop is
'none'again; - nothing for a child whose shoulders the camera can’t see.
Add it before the things the children catch, so those are drawn on top. alpha (0 to 1) draws faint avatars: each
is composited as one layer, so its outlines and overlapping parts don’t show through each other.
Name tags
Section titled “Name tags”stage.add(engine.avatars({ tags: true })) // "1", "2", … above the headsstage.add(engine.avatars({ tags: (player) => (player === leader ? 'Leader' : null) }))tags puts a tag above each head, in a pill of the player’s colour, clear of ears, horns and hats:
true: the player numbers;- a function of the player id: that player’s tag, or
null(or'') for none. It is asked every frame, so the tags can change (a new leader, a team name).
Games don’t get the children’s names (Fabi Play keeps them to itself), so the labels are the game’s own: numbers,
teams, roles. tagHeight (34 stage units) and tagFont (Nunito) change their size and font family. Each tag is
baked once per text, colour and resolution, and baked again when web fonts arrive.
The engine draws avatars with its own renderer: the parts that keep their shape (the head, with face, ears and hats) are baked once into the atlas, and the limbs, torso and the rest are drawn as fast vector pieces with no garbage per frame. Four avatars cost about a tenth of a millisecond of JavaScript per frame; the GPU does the rest.
Outside a game: createAvatarRenderer
Section titled “Outside a game: createAvatarRenderer”For pages that are not engine games (Fabi Play’s own TV screen uses it for calibration and for the simulator’s
preview), createAvatarRenderer(canvas, options) draws avatars on a canvas of their own, with the engine’s renderer
(WebGL2, WebGL1 or Canvas 2D, chosen as the engine chooses), its own atlas and the SDK’s smoothing.
import { createAvatarRenderer } from '@fabiplay/engine'
const avatars = createAvatarRenderer(canvas, { units: { width: 1280, height: 720 } })
function frame() { avatars.draw( players.map((p) => ({ body: p.body, avatar: p.avatar, id: p.id, name: names[p.id], color: colors[p.id] })), { x: 0, y: 0, w: 1280, h: 720 }, // where player space (0–1) lands, in drawing units ) requestAnimationFrame(frame)}requestAnimationFrame(frame)draw(players, box), every animation frame, clears the canvas and draws each player’s avatar. A player is
{ body, avatar?, id?, key?, name?, color?, alpha? }:
body: the 33 points, in player space;avatar: an avatar id (unknown ids get the default avatar);idorkey: who it is, for smoothing and interpolation.keyis any stable string or number (a tracker’s person id, a name) and wins overid; without either, the place in the array;nameandcolor: a name tag above the head, in a pill of that colour;alpha: 0 to 1; below 1 the avatar and its tag are composited as one layer.
box is where player space lands, in drawing units.
Options (all optional):
| Option | Default | What it does |
|---|---|---|
units |
CSS pixels | A fixed drawing space { width, height } (like a 2D canvas’s), stretched by CSS |
pixelRatio |
1 with units, else devicePixelRatio (at most 2) |
Backing-store pixels per drawing unit |
smooth |
0.5 |
The SDK’s per-player smoothing, or false for bodies you smoothed yourself |
interpolate |
true |
Bodies interpolated at the display’s rate, or false to draw them as given |
tagHeight, tagFont |
34, Nunito | The name tags’ height in drawing units, and their font family |
renderer |
'auto' |
Force a renderer, as for createEngine |
It also has clear() (an empty canvas), reset() (forget the previous bodies, for a new scene), destroy(), and
kind and lost (the renderer in use, and whether its GL context is lost right now). After 3 seconds of context
loss it switches to Canvas 2D on a fresh canvas in the old one’s place: read avatars.canvas for the current one.
Name tags are baked once per name, colour and resolution, and baked again once the page’s web fonts have loaded.
Portraits: avatarPortrait
Section titled “Portraits: avatarPortrait”import { avatarPortrait } from '@fabiplay/engine'
const img = avatarPortrait(play.avatarOf(1), 96) // a 96 × 96 CSS-pixel canvasscoreboard.appendChild(img)avatarPortrait(avatarId, size, pixelRatio?) draws an avatar’s head and shoulders, in a neutral pose, on a canvas of
size × size CSS pixels, and caches it. Use it for HTML places such as a scoreboard in
engine.hud. To show it on the stage, draw it into a
baked frame:
const portrait = avatarPortrait(play.avatarOf(1), 96)engine.bake('portrait-1', 96, 96, (ctx) => ctx.drawImage(portrait, 0, 0, 96, 96))stage.add(engine.sprite('portrait-1', { x: 40, y: 40 }))Without the engine
Section titled “Without the engine”The SDK’s drawAvatar draws the same avatars on any Canvas 2D context: see
Players and avatars.