unbind()
Remove a handler that was previously added to an event on the canvas or a display object.
Description
Remove a handler that was previously added to an event on the canvas or a display object.
Arguments
- type : String
- Event types that the handler will be removed for. If multiple types are specified, separate them by a space.
- handler : Function
- The function that was added to the event using bind().
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. When the canvas is clicked, the handler is removed and clicks after this point will not trigger anything.
View Examplevar canvas = oCanvas.create({
canvas: "#canvas",
background: "#ccc"
});
canvas.bind("click tap", function handler () {
canvas.background.set("hsl(" + Math.random() * 360 + ", 50%, 50%)");
canvas.unbind("click tap", handler);
});
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. It will also remove the event handler, so only the first click will change the fill color.
View Examplevar 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 handler () {
this.fill = "hsl(" + Math.random() * 360 + ", 50%, 50%)";
canvas.redraw();
this.unbind("click tap", handler);
});
Output