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.

115 lines
3.5 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Boolean Operations</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 text = new PointText({
  10. position: view.center + [0, 200],
  11. fillColor: 'black',
  12. justification: 'center',
  13. fontSize: 20
  14. });
  15. var originals = new Group({ insert: false }); // Don't insert in DOM.
  16. var square = new Path.Rectangle({
  17. position: view.center,
  18. size: 300,
  19. parent: originals,
  20. fillColor: 'white'
  21. });
  22. // Make a ring using subtraction of two circles:
  23. var inner = new Path.Circle({
  24. center: view.center,
  25. radius: 100,
  26. parent: originals,
  27. fillColor: 'white'
  28. });
  29. var outer = new Path.Circle({
  30. center: view.center,
  31. radius: 140,
  32. parent: originals,
  33. fillColor: 'white'
  34. });
  35. var ring = outer.subtract(inner);
  36. var operations = ['unite', 'intersect', 'subtract', 'exclude', 'divide'];
  37. var colors = ['red', 'green', 'blue', 'black'];
  38. var curIndex = -1;
  39. var operation, result, activeItem;
  40. // Change the mode every 3 seconds:
  41. setInterval(setMode, 3000);
  42. // Set the initial mode:
  43. setMode();
  44. function setMode() {
  45. curIndex++;
  46. if (curIndex == operations.length * 2)
  47. curIndex = 0;
  48. operation = operations[curIndex % operations.length];
  49. }
  50. function onMouseDown(event) {
  51. var hitResult = originals.hitTest(event.point);
  52. activeItem = hitResult && hitResult.item;
  53. }
  54. function onMouseDrag(event) {
  55. if (activeItem)
  56. activeItem.position = event.point;
  57. }
  58. function onMouseUp() {
  59. activeItem = null;
  60. square.position = view.center;
  61. }
  62. function onFrame(event) {
  63. if (activeItem != ring) {
  64. // Move the ring around:
  65. var offset = new Point(140, 80) * [Math.sin(event.count / 60), Math.sin(event.count / 40)];
  66. ring.position = view.center + offset;
  67. }
  68. // Remove the result of the last path operation:
  69. if (result)
  70. result.remove();
  71. // Perform the path operation on the ring:
  72. if (curIndex < operations.length) {
  73. result = square[operation](ring);
  74. text.content = 'square.' + operation + '(ring)';
  75. } else {
  76. result = ring[operation](square);
  77. text.content = 'ring.' + operation + '(square)';
  78. }
  79. result.selected = true;
  80. result.fillColor = colors[curIndex % colors.length];
  81. result.moveBelow(text);
  82. // If the result is a group, color each of its children differently:
  83. if (result instanceof Group) {
  84. for (var i = 0; i < result.children.length; i++) {
  85. result.children[i].fillColor = colors[i];
  86. }
  87. }
  88. };
  89. function onResize() {
  90. text.position = view.center + [0, 200];
  91. square.position = view.center;
  92. }
  93. </script>
  94. </head>
  95. <body>
  96. <canvas id="canvas" resize stats></canvas>
  97. </body>
  98. </html>