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.

90 lines
3.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Bezier Tool</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 types = ['point', 'handleIn', 'handleOut'];
  11. function findHandle(point) {
  12. for (var i = 0, l = path.segments.length; i < l; i++) {
  13. for (var j = 0; j < 3; j++) {
  14. var type = types[j];
  15. var segment = path.segments[i];
  16. var segmentPoint = type == 'point'
  17. ? segment.point
  18. : segment.point + segment[type];
  19. var distance = (point - segmentPoint).length;
  20. if (distance < 3) {
  21. return {
  22. type: type,
  23. segment: segment
  24. };
  25. }
  26. }
  27. }
  28. return null;
  29. }
  30. var currentSegment, mode, type;
  31. function onMouseDown(event) {
  32. if (currentSegment)
  33. currentSegment.selected = false;
  34. mode = type = currentSegment = null;
  35. if (!path) {
  36. path = new Path();
  37. path.fillColor = {
  38. hue: 360 * Math.random(),
  39. saturation: 1,
  40. brightness: 1,
  41. alpha: 0.5
  42. };
  43. }
  44. var result = findHandle(event.point);
  45. if (result) {
  46. currentSegment = result.segment;
  47. type = result.type;
  48. if (path.segments.length > 1 && result.type == 'point'
  49. && result.segment.index == 0) {
  50. mode = 'close';
  51. path.closed = true;
  52. path.selected = false;
  53. path = null;
  54. }
  55. }
  56. if (mode != 'close') {
  57. mode = currentSegment ? 'move' : 'add';
  58. if (!currentSegment)
  59. currentSegment = path.add(event.point);
  60. currentSegment.selected = true;
  61. }
  62. }
  63. function onMouseDrag(event) {
  64. if (mode == 'move' && type == 'point') {
  65. currentSegment.point = event.point;
  66. } else if (mode != 'close') {
  67. var delta = event.delta.clone();
  68. if (type == 'handleOut' || mode == 'add')
  69. delta = -delta;
  70. currentSegment.handleIn += delta;
  71. currentSegment.handleOut -= delta;
  72. }
  73. }
  74. </script>
  75. </head>
  76. <body>
  77. <p>
  78. An emulation of a vector pen tool.
  79. Click and drag to add a points.<br/>
  80. Drag segment handles and points to manipulate them.
  81. Close the path to start a new one.
  82. </p>
  83. <canvas id="canvas" resize></canvas>
  84. </body>
  85. </html>