paper.js/examples/Tools/Fancy Brush.html
2011-03-06 15:47:55 +01:00

113 lines
No EOL
4.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<script type="text/javascript" src="../../lib/bootstrap.js"></script>
<script type="text/javascript" src="../../lib/parse-js.js"></script>
<script type="text/javascript" src="../../src/Paper.js"></script>
<script type="text/javascript" src="../../src/tool/ToolEvent.js"></script>
<script type="text/javascript" src="../../src/tool/ToolHandler.js"></script>
<script type="text/javascript" src="../../src/tool/Tool.js"></script>
<script type="text/javascript" src="../../src/basic/Point.js"></script>
<script type="text/javascript" src="../../src/basic/Rectangle.js"></script>
<script type="text/javascript" src="../../src/basic/Size.js"></script>
<script type="text/javascript" src="../../src/basic/Matrix.js"></script>
<script type="text/javascript" src="../../src/document/DocumentView.js"></script>
<script type="text/javascript" src="../../src/document/Document.js"></script>
<script type="text/javascript" src="../../src/document/Symbol.js"></script>
<script type="text/javascript" src="../../src/item/Item.js"></script>
<script type="text/javascript" src="../../src/item/PlacedSymbol.js"></script>
<script type="text/javascript" src="../../src/item/Group.js"></script>
<script type="text/javascript" src="../../src/item/Layer.js"></script>
<script type="text/javascript" src="../../src/item/Raster.js"></script>
<script type="text/javascript" src="../../src/item/PathStyle.js"></script>
<script type="text/javascript" src="../../src/path/Segment.js"></script>
<script type="text/javascript" src="../../src/path/PathItem.js"></script>
<script type="text/javascript" src="../../src/path/Path.js"></script>
<script type="text/javascript" src="../../src/path/CompoundPath.js"></script>
<script type="text/javascript" src="../../src/path/Path.Constructors.js"></script>
<script type="text/javascript" src="../../src/color/Color.js"></script>
<script type="text/javascript" src="../../src/color/RGBColor.js"></script>
<script type="text/javascript" src="../../src/color/GrayColor.js"></script>
<script type="text/javascript" src="../../src/color/GradientColor.js"></script>
<script type="text/javascript" src="../../src/color/Gradient.js"></script>
<script type="text/javascript" src="../../src/color/GradientStop.js"></script>
<script type="text/javascript" src="../../src/util/BlendMode.js"></script>
<script type="text/javascript" src="../../src/util/CanvasProvider.js"></script>
<script type="text/javascript" src="../../src/util/Events.js"></script>
<script type="text/javascript" src="../../src/util/PaperScript.js"></script>
<script type="text/paperscript" canvas="canvas">
////////////////////////////////////////////////////////////////////////////////
// This script belongs to the following tutorial:
//
// http://scriptographer.org/tutorials/geometry/working-with-mouse-vectors/#adding-brush-stroke-ends
tool.fixedDistance = 80;
var path;
var strokeEnds = 6;
function onMouseDown(event) {
path = new Path();
path.fillColor = event.count % 2 ? 'red' : 'black';
}
var lastPoint;
function onMouseDrag(event) {
// If this is the first drag event,
// add the strokes at the start:
if (event.count == 1) {
addStrokes(event.middlePoint, event.delta * -1);
} else {
var step = event.delta / 2;
step.angle += 90;
// The top point: the middle point + the step rotated by 90 degrees:
// -----*
// |
// ------
var top = event.middlePoint + step;
// The bottom point: the middle point - the step rotated by 90 degrees:
// ------
// |
// -----*
var bottom = event.middlePoint - step;
path.add(top);
path.insert(0, bottom);
}
path.smooth();
lastPoint = event.middlePoint;
}
function onMouseUp(event) {
var delta = event.point - lastPoint;
delta.length = tool.maxDistance;
addStrokes(event.point, delta);
path.closed = true;
path.smooth();
}
function addStrokes(point, delta) {
var step = delta.rotate(90);
var strokePoints = strokeEnds * 2 + 1;
point -= step / 2;
step /= strokePoints - 1;
for (var i = 0; i < strokePoints; i++) {
var strokePoint = point + step * i;
var offset = delta * (Math.random() * 0.3 + 0.1);
if (i % 2) {
offset *= -1;
}
strokePoint += offset;
path.insert(0, strokePoint);
}
}
</script>
</head>
<body>
<canvas id='canvas' width=1024 height=768></canvas>
</body>