1 | <!DOCTYPE html>
|
---|
2 | <html>
|
---|
3 | <head>
|
---|
4 | <meta charset="UTF-8">
|
---|
5 | <title>Fancy Brush</title>
|
---|
6 | <link rel="stylesheet" href="../css/style.css">
|
---|
7 | <script type="text/javascript" src="../../dist/paper.js"></script>
|
---|
8 | <script type="text/paperscript" canvas="canvas">
|
---|
9 | ////////////////////////////////////////////////////////////////////////////////
|
---|
10 | // This script belongs to the following tutorial:
|
---|
11 | //
|
---|
12 | // http://scriptographer.org/tutorials/geometry/working-with-mouse-vectors/#adding-brush-stroke-ends
|
---|
13 |
|
---|
14 | tool.fixedDistance = 80;
|
---|
15 |
|
---|
16 | var path;
|
---|
17 | var strokeEnds = 6;
|
---|
18 |
|
---|
19 | function onMouseDown(event) {
|
---|
20 | path = new Path();
|
---|
21 | path.fillColor = event.count % 2 ? 'red' : 'black';
|
---|
22 | }
|
---|
23 |
|
---|
24 | var lastPoint;
|
---|
25 | function onMouseDrag(event) {
|
---|
26 | // If this is the first drag event,
|
---|
27 | // add the strokes at the start:
|
---|
28 | if (event.count == 1) {
|
---|
29 | addStrokes(event.middlePoint, event.delta * -1);
|
---|
30 | } else {
|
---|
31 | var step = event.delta / 2;
|
---|
32 | step.angle += 90;
|
---|
33 |
|
---|
34 | // The top point: the middle point + the step rotated by 90 degrees:
|
---|
35 | // -----*
|
---|
36 | // |
|
---|
37 | // ------
|
---|
38 | var top = event.middlePoint + step;
|
---|
39 |
|
---|
40 | // The bottom point: the middle point - the step rotated by 90 degrees:
|
---|
41 | // ------
|
---|
42 | // |
|
---|
43 | // -----*
|
---|
44 | var bottom = event.middlePoint - step;
|
---|
45 |
|
---|
46 | path.add(top);
|
---|
47 | path.insert(0, bottom);
|
---|
48 | }
|
---|
49 | path.smooth();
|
---|
50 |
|
---|
51 | lastPoint = event.middlePoint;
|
---|
52 | }
|
---|
53 |
|
---|
54 | function onMouseUp(event) {
|
---|
55 | var delta = event.point - lastPoint;
|
---|
56 | delta.length = tool.maxDistance;
|
---|
57 | addStrokes(event.point, delta);
|
---|
58 | path.closed = true;
|
---|
59 | path.smooth();
|
---|
60 | }
|
---|
61 |
|
---|
62 | function addStrokes(point, delta) {
|
---|
63 | var step = delta.rotate(90);
|
---|
64 | var strokePoints = strokeEnds * 2 + 1;
|
---|
65 | point -= step / 2;
|
---|
66 | step /= strokePoints - 1;
|
---|
67 | for (var i = 0; i < strokePoints; i++) {
|
---|
68 | var strokePoint = point + step * i;
|
---|
69 | var offset = delta * (Math.random() * 0.3 + 0.1);
|
---|
70 | if (i % 2) {
|
---|
71 | offset *= -1;
|
---|
72 | }
|
---|
73 | strokePoint += offset;
|
---|
74 | path.insert(0, strokePoint);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | </script>
|
---|
78 | </head>
|
---|
79 | <body>
|
---|
80 | <canvas id="canvas" resize></canvas>
|
---|
81 | </body>
|
---|
82 | </html> |
---|