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.

103 lines
3.3 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hit Testing</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 values = {
  10. paths: 50,
  11. minPoints: 5,
  12. maxPoints: 15,
  13. minRadius: 30,
  14. maxRadius: 90
  15. };
  16. var hitOptions = {
  17. segments: true,
  18. stroke: true,
  19. fill: true,
  20. tolerance: 5
  21. };
  22. createPaths();
  23. function createPaths() {
  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. var segment, path;
  50. var movePath = false;
  51. function onMouseDown(event) {
  52. segment = path = null;
  53. var hitResult = project.hitTest(event.point, hitOptions);
  54. if (!hitResult)
  55. return;
  56. if (event.modifiers.shift) {
  57. if (hitResult.type == 'segment') {
  58. hitResult.segment.remove();
  59. };
  60. return;
  61. }
  62. if (hitResult) {
  63. path = hitResult.item;
  64. if (hitResult.type == 'segment') {
  65. segment = hitResult.segment;
  66. } else if (hitResult.type == 'stroke') {
  67. var location = hitResult.location;
  68. segment = path.insert(location.index + 1, event.point);
  69. path.smooth();
  70. }
  71. }
  72. movePath = hitResult.type == 'fill';
  73. if (movePath)
  74. project.activeLayer.addChild(hitResult.item);
  75. }
  76. function onMouseMove(event) {
  77. project.activeLayer.selected = false;
  78. if (event.item)
  79. event.item.selected = true;
  80. }
  81. function onMouseDrag(event) {
  82. if (segment) {
  83. segment.point += event.delta;
  84. path.smooth();
  85. } else if (path) {
  86. path.position += event.delta;
  87. }
  88. }
  89. </script>
  90. </head>
  91. <body>
  92. <canvas id="canvas" resize style="background:black"></canvas>
  93. </body>
  94. </html>