I am new to Paper.js, and I was wondered by the event system, while reading tutorial. Thats how event hanling described in tutorial:
var path;
function onMouseDown(event) {
// Create a path:
path = new Path();
path.strokeColor = 'black';
// Add the mouse down position:
path.add(event.point);
}
function onMouseUp(event) {
// Add the mouse up position:
path.add(event.point);
}
So, its just functions in global namespace...
Eventually I have a few questions about it, and I didnt found anything on the internet on this:
- How to bind event handler to particular canvas?
- How to bind event handler to particular "object" (raster image, rectangle, etc)?
- How to bind multiple event handlers to something?
I'm new to Paperjs, but here is what I think:
The scope is automatically associated to a canvas when you specify your canvas to a script:
Each instruction of this script will be associated to this canvas.
Depending on is type each "object" have is sets of events handlers available. The reference page will give you all the events handlers for each type of objects.
For example PathItem have a click event and a double click event.
You can bind multiple event handlers using the attach() method (or its jQuery-style alias, on()). You can remove them with detach() or off(). Here's a modified example from the event handling documentation:
If you want to set an event handler for all instances of a type of object, it's best to create a factory function, as per this answer.