1 | <!DOCTYPE html>
|
---|
2 | <html>
|
---|
3 | <head>
|
---|
4 | <meta charset="UTF-8">
|
---|
5 | <title>Simplify</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 path;
|
---|
10 |
|
---|
11 | var textItem = new PointText({
|
---|
12 | content: 'Click and drag to draw a line.',
|
---|
13 | point: new Point(20, 30),
|
---|
14 | fillColor: 'black',
|
---|
15 | });
|
---|
16 |
|
---|
17 | function onMouseDown(event) {
|
---|
18 | // If we produced a path before, deselect it:
|
---|
19 | if (path) {
|
---|
20 | path.selected = false;
|
---|
21 | }
|
---|
22 |
|
---|
23 | // Create a new path and set its stroke color to black:
|
---|
24 | path = new Path({
|
---|
25 | segments: [event.point],
|
---|
26 | strokeColor: 'black',
|
---|
27 | // Select the path, so we can see its segment points:
|
---|
28 | fullySelected: true
|
---|
29 | });
|
---|
30 | }
|
---|
31 |
|
---|
32 | // While the user drags the mouse, points are added to the path
|
---|
33 | // at the position of the mouse:
|
---|
34 | function onMouseDrag(event) {
|
---|
35 | path.add(event.point);
|
---|
36 |
|
---|
37 | // Update the content of the text item to show how many
|
---|
38 | // segments it has:
|
---|
39 | textItem.content = 'Segment count: ' + path.segments.length;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // When the mouse is released, we simplify the path:
|
---|
43 | function onMouseUp(event) {
|
---|
44 | var segmentCount = path.segments.length;
|
---|
45 |
|
---|
46 | // When the mouse is released, simplify it:
|
---|
47 | path.simplify(10);
|
---|
48 |
|
---|
49 | // Select the path, so we can see its segments:
|
---|
50 | path.fullySelected = true;
|
---|
51 |
|
---|
52 | var newSegmentCount = path.segments.length;
|
---|
53 | var difference = segmentCount - newSegmentCount;
|
---|
54 | var percentage = 100 - Math.round(newSegmentCount / segmentCount * 100);
|
---|
55 | textItem.content = difference + ' of the ' + segmentCount + ' segments were removed. Saving ' + percentage + '%';
|
---|
56 |
|
---|
57 | }
|
---|
58 | </script>
|
---|
59 | </head>
|
---|
60 | <body>
|
---|
61 | <canvas id="canvas" resize></canvas>
|
---|
62 | </body>
|
---|
63 | </html> |
---|