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/*.
npm install @fabiplay/sdk @fabiplay/engineimport { connect } from '@fabiplay/sdk'import { createEngine } from '@fabiplay/engine'
const play = await connect()const engine = await createEngine({ play })// … set up the sceneengine.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.
Copy fabi-play.js (from @fabiplay/sdk, dist/) and fabi-play-engine.lite.js (from @fabiplay/engine,
dist/) to your site, next to each other, and load them in this order:
<script src="fabi-play.js" data-ready="manual"></script><script src="fabi-play-engine.lite.js"></script><script> FabiEngine.createEngine({ play: FabiPlay }).then((engine) => { // … set up the scene engine.start() FabiPlay.ready() })</script>fabi-play-engine.lite.js sets window.FabiEngine (everything the npm package exports, such as
FabiEngine.createEngine). data-ready="manual" keeps the SDK from saying “ready” on page load: the game says
it once its scene is set up.
The same two scripts, as UMD, are on https://cdn.fabiplay.app/sdk/<version>/ and /engine/<version>/: load
them from there with their integrity hashes and crossorigin="anonymous" (see
Get the SDK), the SDK first, both of the same version.
Why the lite build
Section titled “Why the lite build”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. WithoutFabiPlay.avatarsit throws:[engine] fabi-play-engine.lite.js needs fabi-play.js (with FabiPlay.avatars) loaded before it. - Use the
fabi-play.jsof the same SDK version as the engine. - The full
fabi-play-engine.jscarries its own avatars (over a third of its size) and works with anyfabi-play.js, including an older one withoutFabiPlay.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:
resolve: { alias: [ { find: /^@fabiplay\/(sdk\/)?avatars$/, replacement: '/src/avatars-from-sdk.ts' }, { find: /^@fabiplay\/sdk$/, replacement: '/src/sdk-from-script.ts' }, ],},const shared = (window as any).FabiPlay?.avatarsif (!shared) throw new Error('load fabi-play.js before the game')export const { avatarById, avatarShapes, BAND_BODY, DEFAULT_AVATAR, drawShapes, frameOf, iconShapes } = sharedThe 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:
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.
Development checks
Section titled “Development checks”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(thedevelopmentexport condition, as Vite and webpack do); a production build getsdist/index.js, without them; - script tags: the readable
*.umd.jshave them;*.umd.min.jsandfabi-play-engine[.lite].jsdo 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.
Content-Security-Policy
Section titled “Content-Security-Policy”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).