Skip to content

Installation

The engine comes in three builds. Pick by how you build your game:

You use Install Size (minified / gzip)
A bundler (Vite, esbuild, webpack) npm install @fabiplay/sdk @fabiplay/engine about 81 KB / 30 KB
Script tags, no bundler (recommended) fabi-play-engine.lite.js after fabi-play.js about 81 KB / 30 KB
Script tags, with an old fabi-play.js fabi-play-engine.js about 131 KB / 46 KB

The sizes are the engine’s alone. The SDK comes on top, and with the npm build and the lite build it is the SDK that carries the avatars (about 45 KB minified; fabi-play.js is about 60 KB / 21 KB in all): Bubbles, the first game, bundles to about 128 KB / 45 KB with the SDK. Every game carries its own copy of all this (games live on different sites, so there is no shared cache): the sizes matter on a TV’s Wi-Fi.

Everything in this guide is in those sizes (clips and masks, nine-slice, bitmap fonts, timelines, animation): they are methods and options of the one engine, so a bundler keeps them whether a game uses them or not.

The npm packages are published under the @fabiplay scope from 0.9.0; earlier versions were @fabi-play/*.

Terminal window
npm install @fabiplay/sdk @fabiplay/engine
import { connect } from '@fabiplay/sdk'
import { createEngine } from '@fabiplay/engine'
const play = await connect()
const engine = await createEngine({ play })
// … set up the scene
engine.start()
play.ready()

The engine takes the SDK as a peer dependency: install both. Its npm build imports the avatars from the SDK (@fabiplay/sdk/avatars), so with connect() they ship once.

The avatars (their rig and data, about 45 KB minified) are in fabi-play.js already, for the SDK’s drawAvatar. fabi-play.js hands its copy over as FabiPlay.avatars, and the lite build uses it instead of carrying a second one. Its one import from the SDK, the avatar smoothing, asks FabiPlay.smoothedBody too. So the avatars ship once.

  • It must load after fabi-play.js. Without FabiPlay.avatars it throws: [engine] fabi-play-engine.lite.js needs fabi-play.js (with FabiPlay.avatars) loaded before it.
  • Use the fabi-play.js of the same SDK version as the engine.
  • The full fabi-play-engine.js carries its own avatars (over a third of its size) and works with any fabi-play.js, including an older one without FabiPlay.avatars.

Bundling the engine with the script-tag SDK

Section titled “Bundling the engine with the script-tag SDK”

A game that loads fabi-play.js with a script tag and bundles the engine from npm ships the avatars twice. Point the engine’s imports at the script’s copy with two aliases in your bundler, as Fabi Play’s own game does:

vite.config.ts
resolve: {
alias: [
{ find: /^@fabiplay\/(sdk\/)?avatars$/, replacement: '/src/avatars-from-sdk.ts' },
{ find: /^@fabiplay\/sdk$/, replacement: '/src/sdk-from-script.ts' },
],
},
src/avatars-from-sdk.ts
const shared = (window as any).FabiPlay?.avatars
if (!shared) throw new Error('load fabi-play.js before the game')
export const { avatarById, avatarShapes, BAND_BODY, DEFAULT_AVATAR, drawShapes, frameOf, iconShapes } = shared

The second alias is needed because the engine also imports AvatarBook from the SDK (the smoothing behind createAvatarRenderer), and the real one would bring part of the avatars back. A game has FabiPlay already, and createEngine takes its bodies from play.smoothedBody anyway, so the stand-in asks it too:

src/sdk-from-script.ts
import type { Body, Easy, PlayerFrame } from '@fabiplay/sdk'
declare const FabiPlay: Easy
export class AvatarBook {
posed(player: number, body: Body, amount: number, interpolate = true): Body {
const frame = { id: player, body } as PlayerFrame
return FabiPlay.smoothedBody(frame, { smooth: amount < 1 ? amount : false, interpolate }) ?? body
}
}

Fabi Play’s Star Shower went from 113 KB to 68 KB (40 to 27 KB gzip) this way. Only do it with the script tag: with connect() from the npm package there is a single copy already.

Some mistakes do not break a game at once but move the wrong thing later, such as using a node after destroy() or after giving it back to its node pool. Development builds warn about them in the console, once per node; production builds leave the checks out entirely:

  • npm: your bundler’s development mode picks dist/index.development.js (the development export condition, as Vite and webpack do); a production build gets dist/index.js, without them;
  • script tags: the readable *.umd.js have them; *.umd.min.js and fabi-play-engine[.lite].js do not.

See Performance tips.

Both ways are typed: the npm package has dist/index.d.ts, and each script has its .d.ts declaring FabiEngine. See TypeScript types.

The engine needs nothing outside your site: serve its script from your own origin like fabi-play.js, and the publishing CSP works as it is. Loading it from https://cdn.fabiplay.app instead takes that origin in script-src, and nothing else (see Publishing).