Tool
The Tool object refers to a script that the user can interact with by using the mouse and keyboard and can be accessed through the global tool variable. All its properties are also available in the paper scope.
The global tool variable only exists in scripts that contain mouse handler functions (onMouseMove, onMouseDown, onMouseDrag, onMouseUp) or a keyboard handler function (onKeyDown, onKeyUp).
Example:
var path;
// Only execute onMouseDrag when the mouse
// has moved at least 10 points:
tool.minDistance = 10;
tool.onMouseDown = function(event) {
// Create a new path every time the mouse is clicked
path = new Path();
path.add(event.point);
path.strokeColor = 'black';
}
tool.onMouseDrag = function(event) {
// Add a point to the path every time the mouse is dragged
path.add(event.point);
}Properties
minDistanceThe minimum distance the mouse has to drag before firing the onMouseDrag event, since the last onMouseDrag event.
Type:
Number
maxDistanceThe maximum distance the mouse has to drag before firing the onMouseDrag event, since the last onMouseDrag event.
Type:
Number
fixedDistanceType:
Number
Mouse Event Handlers
onMouseDownThe function to be called when the mouse button is pushed down. The function receives a
ToolEventobject which contains information about the tool event.Type:
Function⟋null
Example:Creating circle shaped paths where the user presses the mouse button:
onMouseDragThe function to be called when the mouse position changes while the mouse is being dragged. The function receives a
ToolEventobject which contains information about the tool event.Type:
Function⟋null
Example:Draw a line by adding a segment to a path on every mouse drag event:
onMouseMoveThe function to be called the mouse moves within the project view. The function receives a
ToolEventobject which contains information about the tool event.Type:
Function⟋null
Example:Moving a path to the position of the mouse:
onMouseUpThe function to be called when the mouse button is released. The function receives a
ToolEventobject which contains information about the tool event.Type:
Function⟋null
Example:Creating circle shaped paths where the user releases the mouse:
Keyboard Event Handlers
onKeyDownThe function to be called when the user presses a key on the keyboard. The function receives a
KeyEventobject which contains information about the keyboard event.If the function returns
false, the keyboard event will be prevented from bubbling up. This can be used for example to stop the window from scrolling, when you need the user to interact with arrow keys.Type:
Function⟋null
Example:Scaling a path whenever the user presses the space bar:
onKeyUpThe function to be called when the user releases a key on the keyboard. The function receives a
KeyEventobject which contains information about the keyboard event.If the function returns
false, the keyboard event will be prevented from bubbling up. This can be used for example to stop the window from scrolling, when you need the user to interact with arrow keys.Type:
Function⟋null
Example:
Methods
activate()Activates this tool, meaning
paperScope.toolwill point to it and it will be the one that receives tool events.remove()Removes this tool from the
paperScope.toolslist.
Event Handling
on(type, function)Attach an event handler to the tool.
Parameters:
type:String— the event type:‘mousedown’,‘mouseup’,‘mousedrag’,‘mousemove’,‘keydown’,‘keyup’function:Function— the function to be called when the event occurs, receiving aToolEventobject as its sole argumentReturns:
Tool— this tool itself, so calls can be chained
on(param)Attach one or more event handlers to the tool.
Parameters:
param:Object— an object literal containing one or more of the following properties:mousedown,mouseup,mousedrag,mousemove,keydown,keyupReturns:
Tool— this tool itself, so calls can be chained
off(type, function)Detach an event handler from the tool.
Parameters:
type:String— the event type:‘mousedown’,‘mouseup’,‘mousedrag’,‘mousemove’,‘keydown’,‘keyup’function:Function— the function to be detachedReturns:
Tool— this tool itself, so calls can be chained
off(param)Detach one or more event handlers from the tool.
Parameters:
param:Object— an object literal containing one or more of the following properties:mousedown,mouseup,mousedrag,mousemove,keydown,keyupReturns:
Tool— this tool itself, so calls can be chained
emit(type, event)Emit an event on the tool.
Parameters:
type:String— the event type:‘mousedown’,‘mouseup’,‘mousedrag’,‘mousemove’,‘keydown’,‘keyup’event:Object— an object literal containing properties describing the eventReturns:
Boolean—trueif the event had listeners,falseotherwise
responds(type)Check if the tool has one or more event handlers of the specified type.
Parameters:
type:String— the event type:‘mousedown’,‘mouseup’,‘mousedrag’,‘mousemove’,‘keydown’,‘keyup’Returns:
Boolean—trueif the tool has one or more event handlers of the specified type,falseotherwise
Was this helpful?