1 | <!DOCTYPE html>
|
---|
2 | <html>
|
---|
3 | <head>
|
---|
4 | <meta charset="UTF-8">
|
---|
5 | <title>Square Rounded</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 | // Values
|
---|
11 |
|
---|
12 | var values = {
|
---|
13 | radius: 10,
|
---|
14 | tolerance: 5
|
---|
15 | };
|
---|
16 |
|
---|
17 | checkValues();
|
---|
18 |
|
---|
19 | /////////////////////////////////////////////////////////////////////
|
---|
20 | // Mouse handling
|
---|
21 |
|
---|
22 | var handle;
|
---|
23 | function checkValues() {
|
---|
24 | var min = values.radius * 2;
|
---|
25 | if (values.tolerance < min) values.tolerance = min;
|
---|
26 | handle = values.radius * Numerical.KAPPA;
|
---|
27 | }
|
---|
28 |
|
---|
29 | var path;
|
---|
30 | function onMouseDown(event) {
|
---|
31 | path = new Path({
|
---|
32 | segments: [event.point, event.point],
|
---|
33 | strokeColor: 'black',
|
---|
34 | strokeWidth: 5,
|
---|
35 | strokeCap: 'round'
|
---|
36 | });
|
---|
37 | prevPoint = path.firstSegment.point;
|
---|
38 | curPoint = path.lastSegment.point;
|
---|
39 | curHandleSeg = null;
|
---|
40 | }
|
---|
41 |
|
---|
42 | var curPoint, prevPoint, curHandleSeg;
|
---|
43 | function onMouseDrag(event) {
|
---|
44 | var point = event.point;
|
---|
45 | var diff = (point - prevPoint).abs();
|
---|
46 | if (diff.x < diff.y) {
|
---|
47 | curPoint.x = prevPoint.x;
|
---|
48 | curPoint.y = point.y;
|
---|
49 | } else {
|
---|
50 | curPoint.x = point.x;
|
---|
51 | curPoint.y = prevPoint.y;
|
---|
52 | }
|
---|
53 | var normal = curPoint - prevPoint;
|
---|
54 | normal.length = 1;
|
---|
55 | if (curHandleSeg) {
|
---|
56 | curHandleSeg.point = prevPoint + (normal * values.radius);
|
---|
57 | curHandleSeg.handleIn = normal * -handle;
|
---|
58 | }
|
---|
59 | var minDiff = Math.min(diff.x, diff.y);
|
---|
60 | if (minDiff > values.tolerance) {
|
---|
61 | var point = curPoint - (normal * values.radius);
|
---|
62 | var segment = new Segment(point, null, normal * handle);
|
---|
63 | path.insert(path.segments.length - 1, segment);
|
---|
64 | curHandleSeg = path.lastSegment;
|
---|
65 | // clone as we want the unmodified one:
|
---|
66 | prevPoint = curHandleSeg.point.clone();
|
---|
67 | path.add(curHandleSeg);
|
---|
68 | curPoint = path.lastSegment.point;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | </script>
|
---|
72 | </head>
|
---|
73 | <body>
|
---|
74 | <canvas id="canvas" resize></canvas>
|
---|
75 | </body>
|
---|
76 | </html> |
---|