PaperScript: Improve tool detection code.

And implement Multiple Tools example.
This commit is contained in:
Jürg Lehni 2016-04-13 15:21:35 -07:00
parent eceb133686
commit e0a0cd58d5
2 changed files with 48 additions and 2 deletions

View file

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mulitple Tools</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
// Create two drawing tools.
// tool1 will draw straight lines, tool2 will draw clouds.
// Both share the mouseDown event:
var path;
function onMouseDown(event) {
path = new Path();
path.strokeColor = 'black';
path.add(event.point);
}
window.app = {
tool1: new Tool({
onMouseDown: onMouseDown,
onMouseDrag: function(event) {
path.add(event.point);
}
}),
tool2: new Tool({
minDistance: 20,
onMouseDown: onMouseDown,
onMouseDrag: function(event) {
// Use the arcTo command to draw cloudy lines
path.arcTo(event.point);
}
})
};
</script>
</head>
<body>
<a href="#" onclick="app.tool1.activate(); return false;">Lines</a>
<a href="#" onclick="app.tool2.activate(); return false;">Clouds</a>
<canvas id="canvas" resize></canvas>
</body>
</html>

View file

@ -387,9 +387,11 @@ Base.exports.PaperScript = (function() {
paper = scope;
var view = scope.getView(),
// Only create a tool if the tool object is accessed or something
// resembling a global tool handler is contained in the code.
// resembling a global tool handler is contained in the code, but
// no tool objects are actually created.
tool = /\btool\.\w+|\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/
.test(code) ? new Tool() : null,
.test(code) && !/\bnew\s+Tool\b/.test(code)
? new Tool() : null,
toolHandlers = tool ? tool._events : [],
// Compile a list of all handlers that can be defined globally
// inside the PaperScript. These are passed on to the function as