Kapi

A keyframing API for the HTML 5 canvas

A JavaScript framework by Jeremy Kahn

Actor methods

Actors are produced from kapiInstance.add().

keyframe()

actorInstance.keyframe( keyframeId, stateObj )
keyframeId:

String or number. Where in the animation to place this keyframe. Can either be the actual keyframe number, or a valid Kapi time format string (see documentation for gotoFrame()).

stateObj:

Object. The properties of this keyframed state. Any missing parameters on this keyframe will be inferred from previous keyframes in the animation set for this actor. Individual properties can have tweening formulas applied to them and only them. To do this, pass those properties as Object literals that contain one property. That property's name must be the same as a valid kapi.tween formula.

var demo = kapi(document.getElementById('myCanvas')),
	circle1 = demo.add(circle, {
		name : 'myCircle',
		x : 50,
		y : 50,
		radius : 50,
		color : '#0f0',
		easing : 'easeInOutQuad'
	});

circle1
	.keyframe(0, {
		x: 50,
		y: 50
	})
	.keyframe('3s', {
		x: {easeInSine: 450},
		y: {easeOutSine: 250},
		color: '#f0f'
	}).liveCopy('5s', 0);
Returns:The actor Object (for chaining).

Examples:

updateKeyframe()

actorInstance.updateKeyframe( keyframeId, newProps )

Selectively modify the state properties of a keyframe. Properties that are missing from a call to updateKeyframe() are left unmodified in the keyframe it is modifying. Note! You cannot update the properties of a keyframe that is a liveCopy. You can only update the properties of the original keyframe that it is copying.

keyframeId:

String or number. The desired keyframe to remove actorInstance from, expressed in Kapi time syntax.

newprops:

Object. The properties on the keyframe to be updated and their values.

Returns: The actor Object (for chaining).

Examples:

remove()

actorInstance.remove( keyframeId )

Cleanly removes actorObj from keyframeId, as well as all internal references to it. An error is logged in the JavaScript console if actorObj does not exist at keyframeId.

keyframeId:

String or number. The desired keyframe to remove actorInstance from, expressed in Kapi time syntax.

Returns: The actor Object (for chaining).

Examples:

removeAll()

actorInstance.removeAll()

Removes the actor from all keyframes.

Returns: The actor Object (for chaining).

Examples:

liveCopy()

actorInstance.liveCopy( keyframeId, keyframeIdToCopy )

Add a keyframe to the animation that is a copy of another keyframe. If the copied keyframe is modified or removed, so is the liveCopy. This is handy for tweening back to the first keyframe state in the animation right before the loop starts over.

keyframeId:

String or number. The desired keyframe to remove actorInstance from, expressed in Kapi time syntax.

keyframeIdToCopy:

String or number. The keyframe identifier of the keyframe to copy. Expressed in Kapi time syntax.

Returns: The actor Object (for chaining).

Examples:

getState()

actorInstance.getState()

Get the current state of the actor as it exists in Kapi.

Returns:An object containing all of the properties defining the actor. Returns an empty object if the actor does not have a state when getState is called.

Examples:

get()

actorInstance.get( prop )

Get the current value of a single state property from the actor.

prop:

String. The state property to retrieve. If prop is "layer," this function will return the layer that this actor is currently in.

Returns: Whatever the current value for prop is.

Examples:

moveToLayer()

actorInstance.moveToLayer( layerId )

Change the layer that the actor is currently in. Valid parameters are any layer index between 0 and the max number of layers (inclusive). You can get the upper bound by calling kapiInstance.getNumberOfLayers().

layerId:

Number. The layer to move the actor to.

Returns: The actor Object (for chaining).

Examples:

data()

actorInstance.data( newData )

Store and retrieve arbitrary data on an actor. This data can be anything, in any format.

newData

Any data type. This is the data that will be stored on the actor.

Returns: The data that is currently stored internally on the actor.

Examples:

Immediate Actions API

to()

actorInstance.to( duration, stateObj, events )

Creates an Immediate Action and adds it to the Immediate Actions queue. This immediately starts applying a state change over time.

duration:

String or number. The length of time to apply the change. This can be either an amount of frames or a period of time, expressed in Kapi time syntax (see documentation for gotoFrame()).

stateObj:

Object. The state to animate the actor to.

events:

Event handlers to attach to this immediate action. This parameter is optional. In an event handler function, the this keyword refers to the actor object. Available events:

var demo = kapi(document.getElementById('myCanvas')),
	circle1 = demo.add(circle, {	
			name : 'myCircle',
			x : 0,
			y : 0,
			radius : 50,
			color : '#00ff00'
		});
 
circle1.keyframe(0, { })
	.to('2s', {
			x: '+=100',
			y: 50,
			color: '#3f0000'
		}, {
			'start': function () {
				console.log('Immediate action started!', this);
			},
			'complete': function () {
				console.log('Immediate action completed!', this);
			}
		});
Returns: The actor Object (for chaining).

Examples:

clearQueue()

actorInstance.clearQueue()

Removes any queued Immediate Actions that have not yet begun. This does not cancel or affect the currently executing Immediate Action.

Returns: The actor Object (for chaining).

Examples:

skipToEnd()

actorInstance.skipToEnd()

Skips to the end of the currently executing Immediate Action. The complete event is fired, if it was set. The Immediate Action queue is not affected.

Returns: The actor Object (for chaining).

Examples:

endCurrentAction()

actorInstance.endCurrentAction()

Stops and ends the currently executing Immediate Action in its current state. Note: Internally, this method calls actor.skipToEnd(), so the functionality of that method applies here as well.

Returns: The actor Object (for chaining).

Examples:

Non-method Properties

params

actorInstance.params

This is a copy of the parameters that were passed to kapi.add() when this actor was created.

kapi

actorInstance.kapi

This is a reference to the Kapi instance that controls this actor.