Carena

A lightweight scene-tree for use with html5's canvas. fork me!

Core Features

Carena's core feature-set is currently very minimal, providing only a small set methods:

Dynamic Features

Dynamic features are used to augment the features of an existing object.
Utilize the carena.build method to augment objects with new features; this is generally done by calling carena.build(obj, [carena.feature.{feature1}, carena.feature.{feature2}]). This will return you your original obj augmented with feature1 and feature2

Built objects contain a node.dehydrate() method, which makes it really simple to serialize the tree.

Features are functions that take 3 arguments:

Node

The Node is the carena's core feature which allows nodes to positioned, styled, organized into trees, and traversed.

Getters / Setters

Methods

Eventable

The Eventable feature provides a means for objects to communicate via events.

Event Flow

All events that are triggered are handled first by the current node's bound event handlers, and then the event flows upward towards the root of the scene in a "bubbling" fashion. Bubbling is typically achieved by calling trigger on a parent node, if the parent node is Eventable.

An event handler (fn parameter to the node.event.bind event below) that returns false will cause the "bubbling" to stop immediately. No other event handlers will be called.

Feature Overview

Eventable is attached to objects by way of a namespace property "event". All methods useful to event capabilities are nested under the event namespace.

Many events are namespaced for organization's sake. Examples of namespaced events are "node.x", "node.parent", "drag.start", etc..

All functions exposed by the Eventable feature take a name parameter, which may contain a 'namespace.*' for applying the operation to the entire namespace and any children namespaces (recursively).

Eventable adds the following capabilities to an object:

When binding or triggering events, you may trigger by the entire name or by namespace. For example, If you bound to "node.*" the following events would be caught:

Renderer

The Renderer feature is used to hold an instance of the rendering context, manage when and how a tree is partially re-rendered, and when to clear the context and start rendering from a blank context.

NOTE: Renderer requires you provide an options.canvas object which implements getContext("2d"), and options.canvas.getContext() returns an object compatible with the html5 canvas element's 2d context.

Getters / Setters

Methods

Camera

The job of this feature is to provide a way to view branches of the scene tree using the Renderer feature. The camera is attached to a node by setting its camera.target property. While attached to a target the Camera will forward all events triggered or bubbling through its target to itself.

NOTE: This feature requires an options.render be set to a build Renderer object.

This allows for some interesting behavior. If you made a camera a Node you could attach children to the camera and use them as a menuing system that is not affected by the render order of the scene tree (they are always rendered last).

Events

Aside from proxying events, there are some events that the camera does trigger.

Getters / Setters

Methods

Draggable

This feature allows nodes to be "picked up" and "dragged" around the canvas. It works by binding to the "mouse.move", "mouse.down", and "mouse.up" events, and triggering it's own events.

Events

Methods

DropTarget

Enables dropping of draggable elements onto the target node. This feature emits a various events upon different states of the drop action.

Events

Methods

DropManager

Manages in-flight Draggable objects and attempts to link them up with DropTargets by utilizing node.nodesByPoint to collect nodes that the Draggable is currently residing over

Events

When a "drag.end" event is detected, the list of DropTargets is evaluated by calling node.dropFilter and finding the best match for the drop. If an acceptable DropTarget is found, a "drop.end" event is triggered on the found node

RelativeToParent

This feature moves a node whenever it's parent node moves. Re-parenting a node will unbind from the current parent and bind to the new parent. How the child reacts to the parent's movement is controlled by the positionChange method.

Style

Provider for evented CSS properties. Provides a node.style property which Currently supports:

Each of these properties emit a "style.<property name>" event when changed. The event payload includes: node, previous, current.

Box

Provider of a CSS box model. This feature is meant to be stacked on the Style feature, and in fact, it requires it. Currently supports:

Demos