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.

200 lines
6.7 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Vektor</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. // Interface
  11. var values = {
  12. fixLength: false,
  13. fixAngle: false,
  14. showCircle: false,
  15. showAngleLength: true,
  16. showCoordinates: false
  17. };
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Vector
  20. var vectorStart, vector, vectorPrevious;
  21. var vectorItem, items, dashedItems;
  22. function processVector(event, drag) {
  23. vector = event.point - vectorStart;
  24. if (vectorPrevious) {
  25. if (values.fixLength && values.fixAngle) {
  26. vector = vectorPrevious;
  27. } else if (values.fixLength) {
  28. vector.length = vectorPrevious.length;
  29. } else if (values.fixAngle) {
  30. vector = vector.project(vectorPrevious);
  31. }
  32. }
  33. drawVector(drag);
  34. }
  35. function drawVector(drag) {
  36. if (items) {
  37. for (var i = 0, l = items.length; i < l; i++) {
  38. items[i].remove();
  39. }
  40. }
  41. if (vectorItem)
  42. vectorItem.remove();
  43. items = [];
  44. var arrowVector = vector.normalize(10);
  45. var end = vectorStart + vector;
  46. vectorItem = new Group(
  47. new Path(vectorStart, end),
  48. new Path(
  49. end + arrowVector.rotate(135),
  50. end,
  51. end + arrowVector.rotate(-135)
  52. )
  53. );
  54. vectorItem.strokeWidth = 0.75;
  55. vectorItem.strokeColor = '#e4141b';
  56. // Display:
  57. dashedItems = [];
  58. // Draw Circle
  59. if (values.showCircle) {
  60. dashedItems.push(new Path.Circle(vectorStart, vector.length));
  61. }
  62. // Draw Labels
  63. if (values.showAngleLength) {
  64. drawAngle(vectorStart, vector, !drag);
  65. if (!drag)
  66. drawLength(vectorStart, end, vector.angle < 0 ? -1 : 1, true);
  67. }
  68. var quadrant = vector.quadrant;
  69. if (values.showCoordinates && !drag) {
  70. drawLength(vectorStart, vectorStart + [vector.x, 0],
  71. [1, 3].indexOf(quadrant) != -1 ? -1 : 1, true, vector.x, 'x: ');
  72. drawLength(vectorStart, vectorStart + [0, vector.y],
  73. [1, 3].indexOf(quadrant) != -1 ? 1 : -1, true, vector.y, 'y: ');
  74. }
  75. for (var i = 0, l = dashedItems.length; i < l; i++) {
  76. var item = dashedItems[i];
  77. item.strokeColor = 'black';
  78. item.dashArray = [1, 2];
  79. items.push(item);
  80. }
  81. // Update palette
  82. values.x = vector.x;
  83. values.y = vector.y;
  84. values.length = vector.length;
  85. values.angle = vector.angle;
  86. }
  87. function drawAngle(center, vector, label) {
  88. var radius = 25, threshold = 10;
  89. if (vector.length < radius + threshold || Math.abs(vector.angle) < 15)
  90. return;
  91. var from = new Point(radius, 0);
  92. var through = from.rotate(vector.angle / 2);
  93. var to = from.rotate(vector.angle);
  94. var end = center + to;
  95. dashedItems.push(new Path.Line(center,
  96. center + new Point(radius + threshold, 0)));
  97. dashedItems.push(new Path.Arc(center + from, center + through, end));
  98. var arrowVector = to.normalize(7.5).rotate(vector.angle < 0 ? -90 : 90);
  99. dashedItems.push(new Path([
  100. end + arrowVector.rotate(135),
  101. end,
  102. end + arrowVector.rotate(-135)
  103. ]));
  104. if (label) {
  105. // Angle Label
  106. var text = new PointText(center
  107. + through.normalize(radius + 10) + new Point(0, 3));
  108. text.content = Math.floor(vector.angle * 100) / 100 + '\xb0';
  109. items.push(text);
  110. }
  111. }
  112. function drawLength(from, to, sign, label, value, prefix) {
  113. var lengthSize = 5;
  114. if ((to - from).length < lengthSize * 4)
  115. return;
  116. var vector = to - from;
  117. var awayVector = vector.normalize(lengthSize).rotate(90 * sign);
  118. var upVector = vector.normalize(lengthSize).rotate(45 * sign);
  119. var downVector = upVector.rotate(-90 * sign);
  120. var lengthVector = vector.normalize(
  121. vector.length / 2 - lengthSize * Math.SQRT2);
  122. var line = new Path();
  123. line.add(from + awayVector);
  124. line.lineBy(upVector);
  125. line.lineBy(lengthVector);
  126. line.lineBy(upVector);
  127. var middle = line.lastSegment.point;
  128. line.lineBy(downVector);
  129. line.lineBy(lengthVector);
  130. line.lineBy(downVector);
  131. dashedItems.push(line);
  132. if (label) {
  133. // Length Label
  134. var textAngle = Math.abs(vector.angle) > 90
  135. ? textAngle = 180 + vector.angle : vector.angle;
  136. // Label needs to move away by different amounts based on the
  137. // vector's quadrant:
  138. var away = (sign >= 0 ? [1, 4] : [2, 3]).indexOf(vector.quadrant) != -1
  139. ? 8 : 0;
  140. var text = new PointText(middle + awayVector.normalize(away + lengthSize));
  141. text.rotate(textAngle);
  142. text.justification = 'center';
  143. value = value || vector.length;
  144. text.content = (prefix || '') + Math.floor(value * 1000) / 1000;
  145. items.push(text);
  146. }
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. // Mouse Handling
  150. var dashItem;
  151. function onMouseDown(event) {
  152. var end = vectorStart + vector;
  153. var create = false;
  154. if (event.modifiers.shift && vectorItem) {
  155. vectorStart = end;
  156. create = true;
  157. } else if (vector && (event.modifiers.option
  158. || end && end.getDistance(event.point) < 10)) {
  159. create = false;
  160. } else {
  161. vectorStart = event.point;
  162. }
  163. if (create) {
  164. dashItem = vectorItem;
  165. vectorItem = null;
  166. }
  167. processVector(event, true);
  168. }
  169. function onMouseDrag(event) {
  170. if (!event.modifiers.shift && values.fixLength && values.fixAngle)
  171. vectorStart = event.point;
  172. processVector(event, event.modifiers.shift);
  173. }
  174. function onMouseUp(event) {
  175. processVector(event, false);
  176. if (dashItem) {
  177. dashItem.dashArray = [1, 2];
  178. dashItem = null;
  179. }
  180. vectorPrevious = vector;
  181. }
  182. </script>
  183. </head>
  184. <body>
  185. <canvas id="canvas" resize></canvas>
  186. </body>
  187. </html>