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.

88 lines
2.7 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Worm Farm</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. /////////////////////////////////////////////////////////////////////
  10. // Values
  11. var values = {
  12. minDistance: 10,
  13. maxDistance: 30,
  14. varyThickness: true
  15. };
  16. // All newly created items will inherit the following styles:
  17. project.currentStyle = {
  18. fillColor: 'white',
  19. strokeColor: 'black'
  20. };
  21. /////////////////////////////////////////////////////////////////////
  22. // Mouse handling
  23. tool.minDistance = values.minDistance;
  24. tool.maxDistance = values.maxDistance;
  25. var worm;
  26. // Every time the user clicks the mouse to drag we create a path
  27. // and when a user drags the mouse we add points to it
  28. function onMouseDown(event) {
  29. worm = new Path();
  30. worm.add(event.point, event.point);
  31. worm.closed = true;
  32. }
  33. function onMouseDrag(event) {
  34. // the vector in the direction that the mouse moved
  35. var step = event.delta;
  36. // if the vary thickness checkbox is marked
  37. // divide the length of the step vector by two:
  38. if (values.varyThickness) {
  39. step.length = step.length / 2;
  40. } else {
  41. // otherwise set the length of the step vector to half of
  42. // minDistance
  43. step.length = values.minDistance / 2;
  44. }
  45. // the top point: the middle point + the step rotated by -90
  46. // degrees
  47. // -----*
  48. // |
  49. // ------
  50. var top = event.middlePoint + step.rotate(-90);
  51. // the bottom point: the middle point + the step rotated by 90
  52. // degrees
  53. // ------
  54. // |
  55. // -----*
  56. var bottom = event.middlePoint + step.rotate(90);
  57. // add the top point to the end of the path
  58. worm.add(top);
  59. // insert the bottom point after the first segment of the path
  60. worm.insert(1, bottom);
  61. // make a new line path from top to bottom
  62. new Path(top, bottom);
  63. // This is the point at the front of the worm:
  64. worm.firstSegment.point = event.point;
  65. // smooth the segments of the path
  66. worm.smooth();
  67. }
  68. </script>
  69. </head>
  70. <body>
  71. <canvas id="canvas" resize></canvas>
  72. </body>
  73. </html>