Skip to content

AnimatedSprite

A sprite showing the frames of an animation one after the other, on game time.

const hero = stage.add(engine.animation('hero/run', { fps: 12, x: 200, y: 500, anchor: 0.5 }))
hero.gotoFrame(0)
const jump = stage.add(engine.animation('hero/jump', { loop: false, onComplete: () => jump.destroy() }))

new AnimatedSprite(kernel, def, props?): AnimatedSprite

Use engine.animation(frames, props).

Kernel

AnimationDef

AnimationProps

AnimatedSprite

Sprite.constructor

blend: Blend

‘add’ for glows and sparks; ‘normal’ otherwise.

Sprite.blend


parent: Group | null

The group the node is in, or null.

Sprite.parent


hitRadius: number | null

Hit tests use a circle of this radius (local units) around the origin instead of the rectangle; null: the rectangle.

Sprite.hitRadius


clip: ClipRect | null

Clips the node and its children to this rectangle, in the node’s own units (x, y, width, height). While no rotation is above it, the renderer clips on whole pixels without breaking the batch; rotated, it is drawn as a mask. Clips nest (they intersect). Hit tests ignore it.

list.clip = { x: 0, y: 0, width: 400, height: 300 } // a scrolling list: move its children, not the clip

Sprite.clip


mask: Node | null

Shows the node and its children only where this node is opaque, times its alpha: a circle, a star, a text, any node. The mask is placed as a child of the masked node would be, and is not drawn otherwise (keep it out of the stage). The masked node is drawn into a layer: about two full-canvas composites per mask, so use a few.

photo.mask = engine.shape({ type: 'circle', r: 80, fill: '#fff' })

Sprite.mask


filters: Filter[] | null

Filters the node and its children go through, in order: tint, brightness, contrast, saturate, grayscale, sepia, hueRotate, invert, colorMatrix, blur, dropShadow (the same on WebGL and Canvas 2D), shader (WebGL only). The node is drawn into a layer and composited through them (the filters inside a clip and under a mask). Change their fields freely (flash.amount = 0.5, tweens); null or [] for none.

import { blur, dropShadow } from '@fabiplay/engine'
card.filters = [dropShadow(0, 8, 6, 'rgba(0,0,0,0.35)')]
background.filters = [blur(6)]

Sprite.filters


anchorX: number

Horizontal anchor, 0–1 of the width: 0 left, 0.5 the middle.

Sprite.anchorX


anchorY: number

Vertical anchor, 0–1 of the height: 0 top, 0.5 the middle.

Sprite.anchorY


slice: number | Insets

Nine-slice insets in frame pixels (0: none, or the frame’s own): one number for all sides, or [top, right, bottom, left]. The corners keep their size when the sprite is resized.

Sprite.slice


readonly frames: readonly (string | Frame)[]

The frames, in order.


durations: readonly number[] | null

Seconds per frame, when the animation has its own (else 1 / fps).


fps: number

Frames per second, when there are no durations.


loop: boolean

Start again after the last frame.


speed: number

Time scale: 2 twice as fast, -1 backwards.


onComplete: (() => void) | null

Called when a non-looping animation reaches its end.

get x(): number

Position in the parent, stage units.

number

set x(v): void

number

void

Sprite.x


get y(): number

Position in the parent, stage units (y downwards).

number

set y(v): void

number

void

Sprite.y


get rotation(): number

Radians, clockwise.

number

set rotation(v): void

number

void

Sprite.rotation


get scaleX(): number

Horizontal scale (negative: mirrored).

number

set scaleX(v): void

number

void

Sprite.scaleX


get scaleY(): number

Vertical scale.

number

set scaleY(v): void

number

void

Sprite.scaleY


get alpha(): number

0–1, multiplied down the tree.

number

set alpha(v): void

number

void

Sprite.alpha


get visible(): boolean

Hidden nodes and their children are not drawn. Hit tests (engine.touched, engine.hits) still see them.

boolean

set visible(v): void

boolean

void

Sprite.visible


get destroyed(): boolean

Set by destroy(): the node is gone (its handle is stale).

boolean

Sprite.destroyed


get world(): Mat

The transform to the stage, as of the last draw (or updateWorld): a view on the node store, written in place. Read it when you need it: a view kept across frames stops following the node once the store grows (new arrays).

Mat

Sprite.world


get data(): Record<string, any>

Free for the game: speed, state, anything (made on first use).

Record<string, any>

set data(v): void

Record<string, any>

void

Sprite.data


get scale(): number

Both scales: reads scaleX, sets scaleX and scaleY.

number

set scale(v): void

number

void

Sprite.scale


get z(): number

Drawing order among siblings: higher on top (equal: order of adding).

number

set z(v): void

number

void

Sprite.z


get tint(): number

A colour multiplied with the node’s pixels (white: none), as 0xRRGGBB or CSS.

number

set tint(v): void

ColorLike

void

Sprite.tint


get frame(): Frame | null

What it shows: set a frame or the name of a loaded or baked one (an animation swaps it every few frames).

Frame | null

set frame(f): void

string | Frame | null

void

Sprite.frame


set anchor(v): void

Sets anchorX and anchorY (until then the frame’s own origin is used).

number | { x: number; y: number; }

void

Sprite.anchor


get naturalWidth(): number

The content size the frame has, stage units (without its margin; untrimmed).

number

Sprite.naturalWidth


get naturalHeight(): number

The content height the frame has, stage units (without its margin; untrimmed).

number

Sprite.naturalHeight


get width(): number

Local width, stage units: the frame’s own (naturalWidth) until set; setting it stretches the sprite.

number

set width(v): void

Local size, for hit tests and anchors; groups have none.

number

void

Sprite.width


get height(): number

Local height, stage units: the frame’s own (naturalHeight) until set; setting it stretches the sprite.

number

set height(v): void

Local height, for hit tests and anchors; groups have none.

number

void

Sprite.height


get frameIndex(): number

The frame shown, from 0.

number


get frameCount(): number

How many frames.

number


get playing(): boolean

Playing (not stopped, not at the end of a non-looping run).

boolean

set(props): this

Sets many fields at once.

NodeProps

this

Sprite.set


destroy(): void

Removes the node: invisible at once (its parent null), out of its group’s children at the end of the frame. Its tweens stop.

void

Sprite.destroy


updateWorld(): Mat

world, computed now from the parents (for hit tests between draws).

Mat

Sprite.updateWorld


draw(ctx, alpha, tint): void

Draws the frame (the engine calls it).

DrawContext

number

number

void

Sprite.draw


play(): this

Plays from where it is (from the start again if a non-looping run had ended).

this


stop(): this

Stops on the current frame.

this


gotoFrame(i): this

Shows frame i (from 0; wrapped into range), keeping the playing state.

number

this


gotoAndPlay(i): this

Shows frame i and plays from it.

number

this


gotoAndStop(i): this

Shows frame i and stops on it.

number

this


update(dt): void

Advances by dt seconds of game time (the engine calls it).

number

void