Actor

An Actor is the root element to a sprite animation using a hierarchy of sprites. It can be moved around by setting the x, y, rotation and scale values and can be flipped horizontally It can automatically moved by setting the motion value. Motion can define x, y, rotation and scale values these are the amount they will change by in 1 second Motion values are in th coordinate system of the root element before scaling An Actor contains layers which are individual sprites in a hierarchy

new Actor(spriteData: Object, frames: Object, image: Image)
Parameters
spriteData (Object) json returned from the spritoon app that describes the nodes and hierarchy of a multi-layered sprite
frames (Object) json for spritesheet that describes the positioning of the individual images
image (Image) the actual image described by the frames json
Example
//this.spriteData is the data downloaded from the Spritoon online app niklever.com/spritoon
let actor = new Actor(this.spriteData.sprites[i], this.spriteData.frames, this.spriteImage);
*
Instance Members
selectAction(indexOrName, motion)
update(dt)
render(context)

Action

An Action defines the motion of sprite layers in an Actor

new Action(name: string, sprites: array)
Parameters
name (string) name for this Action
sprites (array) an array of sprites being controlled by this Action *

Key

A channel in an Action is a series of Keys that are attached to sprite. They are used to manipulate the image, x, y, scale, rotation, opacity. After Key one the easing value is used to control the motion

new Key(time: number, value: number, easing: string)
Parameters
time (number) The time that we want this key to occur
value (number) the value to be at at this time
easing (string = "linear") (optional defaults to linear) see Easing for more details *

Anim

Used to create a frame animation for a multi-framed sprite

new Anim(name: string, options: object)
Parameters
name (string) the name of the animation
options (object) an option object that describes the animation.
Example
let options = {
name: "test",
fps: 25, //default is 30 frames per second if not provided
frames: [1,"..",10], //See the parseFrames method for more information
loop: false, //defaults to true if omitted
motion: {x:100, y:-50}, // defaults to 0,0 if omitted
oncomplete: function(){ console.log('animation completed')}, //Called at animation end, called repeatedly for looping animation
}
*
Instance Members
parseFrames(frames)
reset()
update(dt)

AnimSprite

A class to handle sprites with frame animations

new AnimSprite(name: string, options: object)
Parameters
name (string) for this sprite
options (object) an options object that passes in the necessary parameters to create the sprite
Example
const options = {
context: context,//A canvas 2d context
image: image,//a sprite sheet
x: 100,
y: 100,
anchor: {0.5, 0.9}, //pivot point halfway across and near bottom of frame from spritesheet
scale: 0.5, //(optional) defaults to 1
opacity: 0.5, //(optional) defaults to 1
rotation: 0.1, //(optional) defaults to 0
backgroundPosition: null, //(optional) defaults to 0,0 allows offset relative to a background location
physicsBody: null, //(optional) defaults to null  expects a Matter.Bodies object
anims: anims, //An array of animations probably created using the Spritoon online app
}
let sprite = new AnimSprite('sprite', options);
*
Instance Members
anim
anim
getAnimNamed(name, a)
animName
moving
pauseAnim(secs, hide)
update(dt)
boundingBox
hitTest(pt)
render()

Sprite

A Sprite is an individual image from the spritesheet

new Sprite(name: any, options: object)
Parameters
name (any)
options (object) an object that describes this sprite
Example
const options{
name: "test",
x: 100,
y: 100,
game: null, //(optional) The game that this sprite is attached to usual in this.sprites array
frameData: frameData, // The frames array from a json file - not used if game is set
image: image, //Spritesheet image - not used if game is set
context: context,
center: null, //if defined it overrides the x,y values and centres the sprite in the game canvas only used if game is defined
frame: "sprite{04}.png", //will replace {04} with index padded to 4 zeroes, {03} would pad to 3 zeroes
index: 1, //The index for the frame image
anchor: {x:0.5, y:0.9}, //(optional - defaults to 0.5,0.5) rotation and scaling center as a ratio of the width and height
scale: 0.5, //(optional - defaults to 1)
opacity: 0.6, //(optional - defaults to 1)
rotation: 0, //in radians
zOrder: 0, (optional - defaults to 0) higher numbers appear infront of lower numbers
// node: used when a sprite node is created by an Actor. 
// config: used when a sprite node is created by an Actor
// parent: used when a sprite node is created by an Actor
// children: used when a sprite node is created by an Actor
}
*
Instance Members
index
hitTest(pt)
update(dt)
setRest(restore)
render(context, flipped)

Vertex

A Vertex is a simple definition that holds an x and y value and allows you to add and subtract Vertices

new Vertex(x: number, y: number)
Parameters
x (number)
y (number) *
Instance Members
add(v)
sub(v)
normalize()
mag
distanceTo(b)

Rect

A simple class to define a Rectangle

new Rect(x: number, y: number, w: number, h: number)
Parameters
x (number = 0) x position
y (number = 0) y position
w (number = 0) width
h (number = 0) height *
Instance Members
left
left
right
right
top
top
bottom
bottom
width
width
height
height
inRect(pt)
overlaps(r)

Matrix

Simple 2d matrix class used when updating sprites that have parents
[a,c,e]
[b,d,f]
[0,0,1]

new Matrix(a: number, b: number, c: number, d: number, e: number, f: number)
Parameters
a (number = 1)
b (number = 0)
c (number = 0)
d (number = 1)
e (number = 0)
f (number = 0) *
Instance Members
reset(a, b, c, d, e, f)
copy(m)
plus(m)
minus(m)
mult(m)

Easing

Class to handle easing functions. Based on Robert Penner's code.

new Easing(start: number, end: number, duration: number, startTime: number, type: number)
Parameters
start (number) the value at the start of an easing
end (number) the value to change to.
duration (number) the time to elapse in seconds
startTime (number = 0) (optional defaults to 0) pass in Date.now()/1000 if you want times to be relative to now
type (number = 'linear') (optional defaults to linear) *
Instance Members
value(time)
linear()
inQuad()
outQuad()
inOutQuad()
inCubic()
outCubic()
inOutCubic()
inQuart()
outQuart()
inOutQuart()
inQuint()
outQuint()
inOutQuint()
inSine()
outSine()
inOutSine()
inExpo()
outExpo()
inOutExpo()
inCirc()
outCirc()
inOutCirc()
inElastic()
outElastic()
inOutElastic()
inBack()
outBack()
inOutBack()
inBounce(t, b)
outBounce(t, b)
inOutBounce()

TextBlock

Paints text inside a bounding rectangle, if autorender is not passed to the options object then it will be rendered to the canvas immediately

new TextBlock(context: object, text: string, rect: Rect, options: object)
Parameters
context (object) a canvas 2d context
text (string) the text to draw
rect (Rect) a rectangle that defines the bounding shape of the text
options (object = {}) an options object that describes the color, alignment, lineHeight, fontSize and font of the text
Example
const options = {
font = "Verdana",
fontSize = 30,
lineHeight = 35,
color = "#ff00aa",
alignment = 'center'
}
const txt = new TextBlock(context, "This is the text to draw\nThis will be on a second line.", new Rect(100,100,200,80), options)
*
Instance Members
render()

Emitter

Used by a particle effect

new Emitter(options: object)
Parameters
options (object) can contain the following
emissionRate - number of particles to emit per second - defaults to 33
particleCount - the number of particles that the emitter controls - defaults to 100
position - x,y position relative to the top left of the canvas - defaults to 0,0 life - the life time duration of the emitter in seconds - defaults to 3 speed - the distance a particle should move in each screen refresh - defaults to 10 speedVariation - the difference in speed for each particle - defaults to 0.6 angle - the angle of the particle emission in degrees 0 is left, 90 is down, 180 is right and 270 is up angleVariation - the amount of variation from angle that is allowed as a ratio 0-1 - defaults to 1 particleLife - the life time duration of a particle in seconds scaleWithAge - when rendering does the particle size scale up as it goes through its life time - defaults to true additive - should the globalCompositeOperation be 'lighter' - defaults to false canvasOffset - is the emitter relative to an offset position - defaults to 0,0 opacity - the opacity of a particle - defaults to 1 scale - the scale of a particle - defaults to 1 rotation - the angle of a particle - defaults to 0 spin - the angular speed of a particle - defaults to 0 gravity - the gravity applied to a particle - defaults to 0 image - the spritesheet image to use for a bitmap particle json - the json that describes the spritesheet image index - the index into the json that should be used for a particle *
Instance Members
update(dt)
render(context)

Particle

A simple class to use with an Emitter. Probably not used directly.

new Particle(options: object)
Parameters
options (object) An options object containing the parameters for the particle, see Emitter for details *

Preloader

A class to handle preloading assets

new Preloader(options: object)
Parameters
options (object) An options object that defines the assets and callbacks for onprogress and oncomplete
Example
*

Button

A class to handle a button

new Button(options: object)
Parameters
options (object = {}) an options object to handle the button
{string} text - the text on this button - defaults to no text {string} font - the font to use - defaults to Verdana {Rect} rect - A Rect that gives the position and size of the Button in canvas coordinates {string} color - the color of the text in css style eg #ffffff would be white {string} background - the color of the background in css style eg #ffffff would be white {number} fontSize - the size of the font in pixels {function} onClick - the code to run when a click occurs *
Instance Members
hitTest(mousePos)
render(context)

SFX

A simple class to handle sound effects and music loops in games

new SFX(options: object)
Parameters
options (object) an options object to handle initialising the class
{object} context - an AudioContext object
{number} volume - a number 0-1 to control the volume - default 1
{boolean} loop - does this sfx loop - default false
{number} fadeDuration - a number that controls the change in volume speed - default 0.5
{boolean} autoPlay - should this sound start automatically on load *
Static Members
supportsVideoType(type)
Instance Members
loop
play()
volume
pause()
stop()