bind()

Bind a handler to an event on the canvas or a display object.

Syntax bind(type, handler)

Return Type Core / displayObject

Description

Bind a handler to an event on the canvas or a display object.

Arguments

type : String
Event types that will trigger the handler. If multiple types are specified, separate them by a space.
handler : Function
The function that will be triggered when the event is triggered.

Return Value

Core / displayObject. Returns the instance itself, which type depends on if the method was called on the core instance or a display object.

Examples

Example 1

First we create an instance of the core object. Then we add an event handler for the click event, that will change the background color.

View Example
Code
var canvas = oCanvas.create({
	canvas: "#canvas",
	background: "#ccc"
});

canvas.bind("click tap", function () {
	canvas.background.set("hsl(" + Math.random() * 360 + ", 50%, 50%)");
});
Output

Example 2

We create a new core instance and a rectangle that we add to the canvas. Then we add an event handler, that will change the fill color of the rectangle when it is clicked.

View Example
Code
var canvas = oCanvas.create({
	canvas: "#canvas",
	background: "#ccc"
});

var rectangle = canvas.display.rectangle({
	x: 77,
	y: 150,
	width: 200,
	height: 100,
	fill: "#000"
});

canvas.addChild(rectangle);

rectangle.bind("click tap", function () {
	this.fill = "hsl(" + Math.random() * 360 + ", 50%, 50%)";
	canvas.redraw();
});
Output