<p>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 <code>tool</code> variable. All its properties are also available in the paper scope.</p>
<p>The global <code>tool</code> variable only exists in scripts that contain mouse handler functions (<ahref="../classes/Tool.html#onmousemove"><tt>onMouseMove</tt></a>, <ahref="../classes/Tool.html#onmousedown"><tt>onMouseDown</tt></a>, <ahref="../classes/Tool.html#onmousedrag"><tt>onMouseDrag</tt></a>, <ahref="../classes/Tool.html#onmouseup"><tt>onMouseUp</tt></a>) or a keyboard handler function (<ahref="../classes/Tool.html#onkeydown"><tt>onKeyDown</tt></a>, <ahref="../classes/Tool.html#onkeyup"><tt>onKeyUp</tt></a>).</p>
<h4>Example:</h4>
<divclass="paperscript split">
<divclass="buttons">
<divclass="button run">Run</div>
</div>
<scripttype="text/paperscript"canvas="canvas-0">
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
<p>The function to be called when the mouse button is pushed down. The function receives a <ahref="../classes/ToolEvent.html"><tt>ToolEvent</tt></a> object which contains information about the tool event.</p>
<ulclass="member-list">
<h4>Type:</h4>
<li>
<tt>Function</tt>⟋<tt>null</tt>
</li>
</ul>
<h4>Example:<spanclass="description">Creating circle shaped paths where the user presses the mouse button:</span></h4>
<divclass="paperscript split">
<divclass="buttons">
<divclass="button run">Run</div>
</div>
<scripttype="text/paperscript"canvas="canvas-1">
tool.onMouseDown = function(event) {
// Create a new circle shaped path with a radius of 10
<p>The function to be called when the mouse position changes while the mouse is being dragged. The function receives a <ahref="../classes/ToolEvent.html"><tt>ToolEvent</tt></a> object which contains information about the tool event.</p>
<ulclass="member-list">
<h4>Type:</h4>
<li>
<tt>Function</tt>⟋<tt>null</tt>
</li>
</ul>
<h4>Example:<spanclass="description">Draw a line by adding a segment to a path on every mouse drag event:</span></h4>
<divclass="paperscript split">
<divclass="buttons">
<divclass="button run">Run</div>
</div>
<scripttype="text/paperscript"canvas="canvas-2">
// Create an empty path:
var path = new Path({
strokeColor: 'black'
});
tool.onMouseDrag = function(event) {
// Add a segment to the path at the position of the mouse:
<p>The function to be called the mouse moves within the project view. The function receives a <ahref="../classes/ToolEvent.html"><tt>ToolEvent</tt></a> object which contains information about the tool event.</p>
<ulclass="member-list">
<h4>Type:</h4>
<li>
<tt>Function</tt>⟋<tt>null</tt>
</li>
</ul>
<h4>Example:<spanclass="description">Moving a path to the position of the mouse:</span></h4>
<divclass="paperscript split">
<divclass="buttons">
<divclass="button run">Run</div>
</div>
<scripttype="text/paperscript"canvas="canvas-3">
// Create a circle shaped path with a radius of 10 at {x: 0, y: 0}:
var path = new Path.Circle({
center: [0, 0],
radius: 10,
fillColor: 'black'
});
tool.onMouseMove = function(event) {
// Whenever the user moves the mouse, move the path
<p>The function to be called when the mouse button is released. The function receives a <ahref="../classes/ToolEvent.html"><tt>ToolEvent</tt></a> object which contains information about the tool event.</p>
<ulclass="member-list">
<h4>Type:</h4>
<li>
<tt>Function</tt>⟋<tt>null</tt>
</li>
</ul>
<h4>Example:<spanclass="description">Creating circle shaped paths where the user releases the mouse:</span></h4>
<divclass="paperscript split">
<divclass="buttons">
<divclass="button run">Run</div>
</div>
<scripttype="text/paperscript"canvas="canvas-4">
tool.onMouseUp = function(event) {
// Create a new circle shaped path with a radius of 10
<p>The function to be called when the user presses a key on the keyboard. The function receives a <ahref="../classes/KeyEvent.html"><tt>KeyEvent</tt></a> object which contains information about the keyboard event.</p>
<p>If the function returns <code>false</code>, 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.</p>
<ulclass="member-list">
<h4>Type:</h4>
<li>
<tt>Function</tt>⟋<tt>null</tt>
</li>
</ul>
<h4>Example:<spanclass="description">Scaling a path whenever the user presses the space bar:</span></h4>
<p>The function to be called when the user releases a key on the keyboard. The function receives a <ahref="../classes/KeyEvent.html"><tt>KeyEvent</tt></a> object which contains information about the keyboard event.</p>
<p>If the function returns <code>false</code>, 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.</p>
<p>Activates this tool, meaning <ahref="../classes/PaperScope.html#tool"><tt>paperScope.tool</tt></a> will point to it and it will be the one that receives tool events.</p>
— the function to be called when the event occurs, receiving a <ahref="../classes/ToolEvent.html"><tt>ToolEvent</tt></a> object as its sole argument
</li>
</ul>
<ulclass="member-list">
<h4>Returns:</h4>
<li>
<tt><ahref="../classes/Tool.html"><tt>Tool</tt></a></tt> — this tool itself, so calls can be chained
<p>Attach one or more event handlers to the tool.</p>
<ulclass="member-list">
<h4>Parameters:</h4>
<li>
<tt>param:</tt>
<tt>Object</tt>
— an object literal containing one or more of the following properties: <tt>mousedown</tt>, <tt>mouseup</tt>, <tt>mousedrag</tt>, <tt>mousemove</tt>, <tt>keydown</tt>, <tt>keyup</tt>
</li>
</ul>
<ulclass="member-list">
<h4>Returns:</h4>
<li>
<tt><ahref="../classes/Tool.html"><tt>Tool</tt></a></tt> — this tool itself, so calls can be chained
<p>Detach one or more event handlers from the tool.</p>
<ulclass="member-list">
<h4>Parameters:</h4>
<li>
<tt>param:</tt>
<tt>Object</tt>
— an object literal containing one or more of the following properties: <tt>mousedown</tt>, <tt>mouseup</tt>, <tt>mousedrag</tt>, <tt>mousemove</tt>, <tt>keydown</tt>, <tt>keyup</tt>
</li>
</ul>
<ulclass="member-list">
<h4>Returns:</h4>
<li>
<tt><ahref="../classes/Tool.html"><tt>Tool</tt></a></tt> — this tool itself, so calls can be chained
Copyright © 2011—2020 <ahref="http://www.lehni.org"target="_blank">Jürg Lehni</a>&<ahref="http://www.jonathanpuckey.com"target="_blank">Jonathan Puckey</a>. All Rights Reserved.</p>