Kapi

A keyframing API for the HTML 5 canvas

A JavaScript framework by Jeremy Kahn

Kapi constructor

kapi()

kapi( canvas, params, events )
canvas:

This is a reference to your canvas on the DOM.

params:

Optional parameters you may pass to the kapi instance. You can pass:

events:

An object containing events that can be set on this instance of Kapi. Supply event handlers as functions. Available events:

Returns: An instance of kapi.

Examples:

Kapi instance methods

getVersion()

kapiInstance.getVersion()
Returns: String. The version of Kapi you are using.

getContext()

kapiInstance.getContext()
Returns: A reference to the context object of the canvas that Kapi is controlling.

isPlaying()

kapiInstance.isPlaying()
Returns: Boolean. Whether or not the animation is playing (meaning not paused or stopped).

play()

kapiInstance.play()

Starts the animation if it was not running before, or resumes the animation if it was not running previously.

Returns: The Kapi object (for chaining).

pause()

kapiInstance.pause()

Freeze the animation at its current state. Resuming from the paused state does not start the animation from the beginning, the state of the animation is maintained.

Returns: The Kapi object (for chaining).

stop()

kapiInstance.stop()

Stops the animation. When the animation is started again with play(), it starts from the beginning of the loop. Note: The canvas will be cleared out automatically for you if the clearOnStop option is set to true. This option is set to false by default.

Returns: The Kapi object (for chaining).

repeat()

kapiInstance.repeat( repetitions, callback )

Play the animation for a set amount of repetitions. After starting over the specified amount of times, the animation will call stop(). Note: The canvas will be cleared out automatically for you if the clearOnComplete option is set to true. This option is set to false by default.

repetitions:

A number. The number of times to start over.

callback:

A Function. An optional callback function that will be invoked when the repeat() sequence completes.

Returns: The Kapi object (for chaining).

Examples:

iterate()

kapiInstance.iterate( iterations, callback )

Play the animation and let it run for a set amount of iterations. After running for the specified amount of times, the animation will call stop(). This is extremely similar to the functionality of kapiInstance.repeat(), but the parameter controls how many times the animation runs for, not how many times it starts over. Note: The canvas will be cleared out automatically for you if the clearOnComplete option is set to true. This option is set to false by default.

iterations:

A number. The number of times to run for.

callback:

A Function. An optional callback function that will be invoked when the iterate() sequence completes.

Returns: The Kapi object (for chaining).

Examples:

clear()

kapiInstance.clear()

Clears out the canvas and sets it to its default color.

Returns: The Kapi object (for chaining).

redraw()

kapiInstance.redraw()

Forces a redraw of the current actors' states. Handy if used in tandem with .clear().

Returns: The Kapi object (for chaining).

add()

kapiInstance.add( actor, initialState, setupParams )

Add an actor to the animation. Importantly, this method returns a reference to the actor that it added to the Kapi instance.

actor:

A function or object. This is an actor template.

If you are passing a function: Logically, this function should draw something to the canvas. All actor functions access their state properties with the this JavaScript keyword. Actor functions get a reference to the canvas's context as the first parameter, a reference to the kapi instance as the second, and the actor object as the third. This means that all public Kapi APIs are available from within any actor's draw method. Handy note: A reference to the canvas element can be be retrieved from the context like so: ctx.canvas Here's an example of an actor:

function circle(ctx, kapiInst, actorInst){
	ctx.beginPath();
	ctx.arc(
		this.x || 0,
		this.y || 0,
		this.radius || 0,
		0,
		Math.PI*2, 
		true
		);
	ctx.fillStyle = this.color || '#f0f';
	ctx.fill();
	ctx.closePath();

	return this;
}

If you are passing an Object: You can pass an object with any number of the following properties:

Here's a example of an actor similar to the previous example, but as an object with setup and teardown methods:

var circle = {
	setup: function (kapiInst, actorInst, params) {
		if (console) {
			console.log('Setting up ', this)
		}
	},
	draw: function (ctx, kapiInst, actorInst){
		ctx.beginPath();
		ctx.arc(
			this.x || 0,
			this.y || 0,
			this.radius || 0,
			0,
			Math.PI*2, 
			true
			);
		ctx.fillStyle = this.color || '#f0f';
		ctx.fill();
		ctx.closePath();

		return this;
	},
	teardown: function (actorName, kapiInst) {
		if (console) {
			console.log('Tearing down ', this);
		}
	}
}
initialState:

An object defining the initial state for the actor. If an actor uses a property in any of its keyframes, it must be present in this Object. There is a special data parameter you can add here. It simply stores arbitrary data on the actor, and can be updated and accessed at any time with actor.data(). This data property is not present on any of the keyframes. Note: If the name property is not provided, it is added automatically as a random string.

setupParams

This can be anything - but it's probably best to make it an Object. Whatever you specify for this parameter, it will be passed along to the actor's setup parameter as the third parameter (see example above).

Here's an example of how to call kapiInstance.add():

myKapi = kapi(document.getElementsByTagName('canvas')[0]);

myCircle = myKapi.add(circle, {
	name: 	myCircle,
	x: 		70,
	y: 		70,
	radius: 50,
	color: 	'#00ffaa',
	easing: 'easeInOutQuint',
	data: {
		val: 'You can store whatever data you please here and access it later with myCircle.data().'
	}
}, {
	setupParam1: "Hello! I am a setup parameter!  I get passed into the actor's `setup` function!"
});
Returns: A reference to the actor that was added to Kapi.

removeActor()

kapiInstance.removeActor( actorName )

Completely removes an actor from the animation. This also removes all keyframes for the removed actor. This method calls the actor's teardown method, if one was defined when the actor was added.

actorName:

A string. The name of the actor to be removed.

Returns:The Kapi object (for chaining).

Examples:

removeAllKeyframes()

kapiInstance.removeAllKeyframes()

Remove all of the keyframes for all of the actors in the animation. Aside from the fact that added actors are still available in the Kapi instance, this effectively resets the state of Kapi.

Returns: The Kapi object (for chaining).

Examples:

gotoFrame()

kapiInstance.gotoFrame( frame )

Go to a specific frame in the animation. You can go to any frame, it doesn't have to be a keyframe.

frame:

A keyframe identifier (integer, {number}s or {number}ms) specifying which frame to go to and render. Specify an integer if you know exactly what frame you want to go to. If you want to go to a frame that exists at a specific amount of time into the animation, pass a string in the {number}s or {number}ms formats. {number}s is seconds, "{number}ms" is milliseconds.

myKapi.gotoFrame('1.5s');

This will go to and render the frame that exists 1.5 seconds into the animation.

Returns: The Kapi object (for chaining).

gotoAndPlay()

kapiInstance.gotoAndPlay( frame )

Works exactly like gotoFrame(), but has the added benefit of also .play()ing the animation.

Returns: The Kapi object (for chaining).

Examples:

getState()

kapiInstance.getState()

Gets the current state of all of the actors in the animation as an object.

Returns: An object.

Examples:

getNumberOfLayers()

kapiInstance.getNumberOfLayers()

Gets the number of layers currently in the Kapi instance. Note: This is also the same as the number of actors in the animation.

Returns: A number.

Examples:

getActor()

kapiInstance.getActor( actorName )

Get a specific actor object.

actorName:

A string representing the name of the actor to fetch.

Returns:An actor object.

Examples:

getActorList()

kapiInstance.getActorList()

Returns a list of all the actors currently registered with this Kapi instance.

Returns: An Array of all the string names of the actors that can be accessed with kapi.getActor().

Examples:

framerate()

kapiInstance.framerate( newFramerate )

Gets or sets the framerate that Kapi updates at. The current framerate is always returned, but if an argument is specified, Kapi's framerate is set to that number.

newFramerate:

The new framerate to set.

Returns:The current framerate. If newFramerate is an integer and greater than 0, this number is the same as newFramerate.

Examples:

bind()

kapiInstance.bind( eventName, handler )

Bind an event handler to a Kapi event. Throughout the course of its execution, Kapi fires events at verious key points. You can attach an event handler that gets invoked when these events fire. You can attach as many event handlers to an event as you'd like; event handlers are invoked in the order they are attached.

Event handlers are invoked in the context of the Kapi instance. This means the this keyword is a reference to the Kapi object.

You can bind event handlers to the following events:

eventName:

A string. The name of the event to bind an event handler to.

handler:

A function. The event handler to attach.

function myHandler () {
	console.log('Hello there.');
}

var demo = kapi(document.getElementById('myCanvas'));

demo.bind('enterFrame', myHandler);
Returns: The Kapi object (for chaining).

unbind()

kapiInstance.unbind( eventName, handler )

Remove an event handler from the Kapi instance. You can either remove a specific handler from the event, or all of them - this is determined by the presence of the handler parameter. If present, this method will only unbind the specified event handler. If the handler parameter is omitted, all of the event handlers for this event are unbound. Valid event names are the same as those listed in .bind().

eventName:

A string. The name of the event to unbind an event handler from.

handler:

A function. A reference to the handler function to unbind.

function myHandler () {
	console.log('Hello there.');
}
				
var demo = kapi(document.getElementById('myCanvas'));

demo
	.bind('enterFrame', myHandler)
	.unbind('enterFrame', myHandler);
Returns: The Kapi object (for chaining).

Examples: