1 | <!DOCTYPE html>
|
---|
2 | <html>
|
---|
3 | <head>
|
---|
4 | <meta charset="UTF-8">
|
---|
5 | <title>Path Editing</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 | var values = {
|
---|
10 | paths: 100,
|
---|
11 | minPoints: 5,
|
---|
12 | maxPoints: 15,
|
---|
13 | minRadius: 30,
|
---|
14 | maxRadius: 90
|
---|
15 | };
|
---|
16 |
|
---|
17 | var hitOptions = {
|
---|
18 | segments: true,
|
---|
19 | stroke: true,
|
---|
20 | fill: true,
|
---|
21 | tolerance: 5
|
---|
22 | };
|
---|
23 |
|
---|
24 | var radiusDelta = values.maxRadius - values.minRadius;
|
---|
25 | var pointsDelta = values.maxPoints - values.minPoints;
|
---|
26 | for (var i = 0; i < values.paths; i++) {
|
---|
27 | var radius = values.minRadius + Math.random() * radiusDelta;
|
---|
28 | var points = values.minPoints + Math.floor(Math.random() * pointsDelta);
|
---|
29 | var path = createBlob(view.size * Point.random(), radius, points);
|
---|
30 | var lightness = (Math.random() - 0.5) * 0.4 + 0.4;
|
---|
31 | var hue = Math.random() * 360;
|
---|
32 | path.fillColor = { hue: hue, saturation: 1, lightness: lightness };
|
---|
33 | path.strokeColor = 'black';
|
---|
34 | };
|
---|
35 |
|
---|
36 | function createBlob(center, maxRadius, points) {
|
---|
37 | var path = new Path();
|
---|
38 | path.closed = true;
|
---|
39 | for (var i = 0; i < points; i++) {
|
---|
40 | var delta = new Point({
|
---|
41 | length: (maxRadius * 0.5) + (Math.random() * maxRadius * 0.5),
|
---|
42 | angle: (360 / points) * i
|
---|
43 | });
|
---|
44 | path.add(center + delta);
|
---|
45 | }
|
---|
46 | path.smooth();
|
---|
47 | return path;
|
---|
48 | }
|
---|
49 |
|
---|
50 | var segment, path;
|
---|
51 | var movePath = false;
|
---|
52 | function onMouseDown(event) {
|
---|
53 | segment = path = null;
|
---|
54 | var hitResult = project.hitTest(event.point, hitOptions);
|
---|
55 |
|
---|
56 | if (event.modifiers.shift) {
|
---|
57 | if (hitResult.type == 'segment') {
|
---|
58 | hitResult.segment.remove();
|
---|
59 | };
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (hitResult) {
|
---|
64 | path = hitResult.item;
|
---|
65 | if (hitResult.type == 'segment') {
|
---|
66 | segment = hitResult.segment;
|
---|
67 | } else if (hitResult.type == 'stroke') {
|
---|
68 | var location = hitResult.location;
|
---|
69 | segment = path.insert(location.index + 1, event.point);
|
---|
70 | path.smooth();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | movePath = hitResult.type == 'fill';
|
---|
74 | if (movePath)
|
---|
75 | project.activeLayer.addChild(hitResult.item);
|
---|
76 | }
|
---|
77 |
|
---|
78 | function onMouseMove(event) {
|
---|
79 | var hitResult = project.hitTest(event.point, hitOptions);
|
---|
80 | project.activeLayer.selected = false;
|
---|
81 | if (hitResult && hitResult.item)
|
---|
82 | hitResult.item.selected = true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | function onMouseDrag(event) {
|
---|
86 | if (segment) {
|
---|
87 | segment.point = event.point;
|
---|
88 | path.smooth();
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (movePath)
|
---|
92 | path.position += event.delta;
|
---|
93 | }
|
---|
94 | </script>
|
---|
95 | </head>
|
---|
96 | <body style="background:black">
|
---|
97 | <canvas id="canvas" resize></canvas>
|
---|
98 | </body>
|
---|
99 | </html> |
---|