Skip to content

Sprites, shapes, text and groups

Everything on the stage is a node. Make a node once with the engine, add it to the stage or a group, then change its fields every frame: the engine draws it. No per-frame description of the whole scene, so no per-frame garbage.

const star = stage.add(engine.shape({ type: 'star', r: 40, fill: '#FBD46A' }, { x: 640, y: 100 }))
engine.onFrame((dt) => {
star.y += 150 * dt
star.rotation += dt
})

group.add(node) returns the node, so making and adding fit on one line.

Set them when making the node (the second argument), one by one, or several at once with node.set({ … }).

Field What it is
x, y Position in the parent, stage units, y downwards
rotation Radians, clockwise
scale, scaleX, scaleY scale sets both; a negative scaleX mirrors
alpha 0 to 1, multiplied down the tree
visible Hidden nodes and their children are not drawn
tint A colour multiplied with the pixels (white: none), as 0xRRGGBB or CSS
blend 'normal', or 'add' for glows and sparks (light adds up)
z Drawing order among siblings: higher on top; equal z keeps the order of adding
anchor 0 to 1 of the size: 0 top-left, 0.5 the middle; a number sets both, or { x, y }
width, height Local size, stage units: sprites and shapes stretch to it
hitRadius Hit tests use a circle of this radius around the origin instead of the rectangle
clip { x, y, width, height } in the node’s own units: draw the node and its children only inside it (see Clips and masks)
mask Another node: draw this one only where the mask is opaque (see Clips and masks)
filters A list of effects for the node and its children: tint, colour, blur, drop shadow, your own shader (see Filters)
data Free for the game: speed, state, anything

node.destroy() removes it: it disappears at once and leaves its group at the end of the frame, and its tweens stop. That makes it safe to destroy nodes while iterating over group.children. node.destroyed says it is gone.

await engine.load({ fox: 'art/fox.png' })
const fox = stage.add(engine.sprite('fox', { x: 640, y: 500, anchor: 0.5 }))

engine.sprite(frame, props) shows a frame: the name of a loaded or baked one, or a Frame object. Its size is the frame’s own (naturalWidth, naturalHeight) until you set width and height. Images and baked frames are anchored at their top-left corner until you set an anchor.

Change sprite.frame to show another one. For frame-by-frame animation, use engine.animation().

slice nine-slices the sprite, so it resizes without stretching its corners: engine.sprite('ui/panel', { slice: 24, width: 420, height: 160 }). See Nine-slice.

engine.shape({ type: 'circle', r: 30, fill: '#8CC9A0', stroke: '#fff', lw: 4 })
engine.shape({ type: 'roundRect', w: 300, h: 80, r: 24, fill: 'rgba(15,24,42,0.6)' })
engine.shape({ type: 'star', r: 40, fill: '#FBD46A', shadow: { color: '#FBD46A', blur: 12 } })

engine.shape(spec, props) makes a shape. Sizes are in stage units, colours in CSS.

type Size fields What it is
circle r A disc
ellipse rx, ry An ellipse
roundRect w, h, r A rectangle with round corners (r: 0 for square ones); resizes freely
star r, points (5), inner (0.5) A star; inner is the inner radius as a share of r
ring r A circle’s outline (stroke and lw)
line length A horizontal capsule length long and lw thick (stroke colour, or fill)
path d, size, r Any SVG path (what Path2D takes) in a size × size box, drawn centred; scaled to r (half the box) if given

And how it is painted:

Field What it is
fill The fill colour; none without it
stroke, lw The outline colour and width
outlineBelow The outline under the fill (only its outer half shows), as when a path is stroked before it is filled
shadow { color, blur, x?, y? }: a soft shadow or glow. Drawn on the high tier only

Shapes are anchored at their centre until you set an anchor.

Shapes are baked. A shape is drawn once with Canvas 2D (outline and glow included) into the engine’s atlas, then drawn as an image: after that it is cheap to move, rotate, scale and fade. Two shapes with the same description share one baked image. Setting shape.spec bakes it again: fine now and then, not every frame.

Rounded rectangles resize freely. A roundRect is drawn nine-sliced: set its node’s width and height every frame and the corners keep their shape. That is what panels, backgrounds and progress bars are made of:

const bar = stage.add(engine.shape({ type: 'roundRect', w: 400, h: 24, r: 12, fill: '#FBD46A' }, { x: 40, y: 680, anchor: { x: 0, y: 0.5 } }))
engine.onFrame(() => (bar.width = 400 * progress))

Other shapes stretch when resized (their outline too).

const score = stage.add(engine.text('0', { x: 40, y: 40, size: 48, align: 'left', outline: true }))
score.text = String(points)

engine.text(string, style) makes a line of text. The style and the node’s fields go in the same object.

Style Default What it is
size 40 Font size, stage units
font Fredoka, then rounded fallbacks A CSS font family list (DEFAULT_FONT)
weight 700 A CSS font weight
color cream, #FBEFE3 A CSS colour
align 'center' 'left': x is the start; 'right': x is the end. y is always the middle of the line
outline, outlineWidth none A dark outline (true: the default halo colour, HALO), or a CSS colour; outlineWidth in stage units (12% of size, at least 3)
shadow, shadowBlur none A soft shadow (true: the default halo colour); drawn on the high tier only

Texts are baked too. A text is drawn once into the atlas and moved as an image. Changing text.text, or setting text.style = { … } (merged with the current style), bakes it again: fine for a score, not for a timer that changes every frame. Changing a field of text.style in place is not seen: assign a new style object.

For a text that changes often, a score or a timer, draw it with a bitmap font ({ bitmapFont: 'score' } in the style): it bakes nothing when it changes.

Fonts. Texts use Fredoka if the page has loaded it (the Fabi Play look), else the system’s rounded or sans-serif font. When a web font finishes loading, texts bake again by themselves. To use your own font, load it with engine.load and name it in font.

Over the video, give texts an outline (or a shadow): it keeps them readable over a real room, light or dark.

const stars = stage.add(engine.group())
stars.add(engine.shape({ type: 'star', r: 40, fill: '#FBD46A' }, { x: 100 }))
for (const star of stars.children) star.y += 150 * dt

engine.group(props) holds other nodes: they move, rotate, scale, fade and tint with it.

  • group.add(node, ...more) adds nodes (moving them from another group) and returns the first.
  • group.remove(node) takes a node out without destroying it.
  • group.children are the nodes in drawing order; destroying them while iterating is safe.
  • group.clear() destroys every child; group.destroy() the group and every child.

Groups have no size of their own. Use them for layers (background, play, HUD) and for scenes.

engine.particles(...) is a node too: see Tweens and particles.

The node classes (Node, Sprite, Shape, Text, Group, Particles) are exported for type annotations and instanceof checks. Make nodes with the engine’s factories (engine.sprite, engine.shape, …), not with new.