You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

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