Bake and atlases
The engine draws everything from textures. Your art gets there in two ways: you load files (images, atlases), or you bake art you draw yourself with Canvas 2D. Either way it gets a name, and sprites show it by that name.
Loading assets
Section titled “Loading assets”await engine.load({ sky: 'art/sky.webp', // an image: the frame "sky" art: 'art/stars.json', // an atlas: the frames "art/star", "art/rock", … digits: 'fonts/digits.fnt', // a bitmap font: engine.text('0', { bitmapFont: 'digits' }) ding: 'sfx/ding.mp3', // a sound: engine.sound.play('ding') Fredoka: 'fonts/fredoka-600.woff2', // a font: the family "Fredoka"})
const sky = stage.add(engine.sprite('sky', { width: stage.width, height: stage.height }))const star = stage.add(engine.sprite('art/star', { x: 640, y: 100, anchor: 0.5 }))engine.load(assets) takes names and URLs, loads them all in parallel, and resolves when they are ready. What it does
with each depends on the file’s extension:
| Extension | What it becomes |
|---|---|
.png, .webp, .jpg, any other image |
A frame of that name |
.json |
A texture atlas in TexturePacker’s or Aseprite’s JSON format (hash or array), with its image: one frame per entry, named <name>/<entry> without the image extension. Or a BMFont in JSON (chars and no frames): a bitmap font |
.fnt |
A bitmap font (BMFont, text or XML), with its page images |
.mp3, .ogg, .wav, .m4a, .aac, .webm |
A sound of that name |
.woff, .woff2, .ttf, .otf |
A font, added to the page under the name as its family |
The atlas’s image is the one its meta.image names, next to the .json; without it, the .json’s own name with
.png. From the atlas the engine also reads:
- trimmed frames (
trimmed,spriteSourceSize,sourceSize): each is drawn at its untrimmed size, with its picture where it was; - nine-slice borders (
scale9Borders): see Nine-slice; - animations: TexturePacker’s and PixiJS’s
animations, and Aseprite’s tags (meta.frameTags) with each frame’s duration, forengine.animation('art/name').
Rotated frames are not read (a warning says so in the console): export without rotation.
All the files come from your own site, like the game itself (see Publishing). Load before
play.ready(), so Fabi Play’s loader covers the wait.
engine.frame(name) gives a loaded or baked frame by name, and throws if there is none: no frame "name": load it or bake it first.
Baking your own art
Section titled “Baking your own art”Draw once with Canvas 2D, then use it like any image:
engine.bake('cloud', 160, 80, (ctx) => { ctx.fillStyle = '#fff' ctx.beginPath() ctx.ellipse(80, 50, 70, 26, 0, 0, Math.PI * 2) ctx.ellipse(60, 38, 34, 26, 0, 0, Math.PI * 2) ctx.ellipse(100, 32, 38, 28, 0, 0, Math.PI * 2) ctx.fill()}, { x: 80, y: 40 })
stage.add(engine.sprite('cloud', { x: 300, y: 120 }))engine.bake(name, width, height, draw, origin?):
width,heightare in stage units, and so is everythingdrawdraws: the context is already scaled to the backing store’s resolution, so the art is sharp on every screen.originis where the frame’s own origin is, in stage units: sprites showing it are anchored there until you set an anchor. By default the top-left corner.- It returns the
Frame, but give sprites the name: a sprite made from the name finds the frame again after the atlas is wiped (below), a sprite made from theFrameobject does not.
draw may be called again. The engine keeps the function and bakes the frame again when it needs to: when the
resolution changes (a resize, a lower tier) and after the atlas is wiped (below). So draw must draw the same thing
every time, from nothing but its arguments and constants. Baking again under the same name replaces the art.
What to bake: anything with paths, gradients or shadows. Drawing them every frame is exactly what is slow on a TV; drawing their baked image is cheap.
The atlas
Section titled “The atlas”The engine keeps baked art in pages of an atlas: big canvases (2048 pixels square, or the GPU’s limit if smaller) packed with frames, so a whole scene draws from a few textures in a handful of draw calls. Shapes, texts, your bakes and the avatars’ heads all live there.
- A game’s atlas has at most 4 pages. One item bigger than a page gets a page of its own, until the next wipe.
- When every page is full, the atlas is wiped: everything in it is baked again the next time it is drawn. That is automatic, and costs a few frames of baking. If it happens all the time, the game bakes too much: see Performance tips.
- Loaded images and atlases are not in the engine’s atlas: they keep their own textures, and are never wiped.
Context loss
Section titled “Context loss”TVs lose GPU contexts (the app goes to the background, a video decoder takes the memory). The engine keeps the pixels of every texture (pages, images), so it can upload them again when the context comes back, or move to Canvas 2D. You don’t need to load or bake anything again. See Renderers and quality tiers.