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.

hand.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * WHITEBOPHIR
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2013 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. */
  26. (function hand() { //Code isolation
  27. const selectorStates = {
  28. pointing: 0,
  29. selecting: 1,
  30. moving: 2
  31. }
  32. var selected = null;
  33. var selected_els = [];
  34. var selectionRect = createSelectorRect();
  35. var selectionRectTranslation;
  36. var translation_elements = [];
  37. var selectorState = selectorStates.pointing;
  38. var last_sent = 0;
  39. function inRect(x, y, rect) {
  40. return (x >= rect.x && x <= rect.x + rect.width) &&
  41. (y >= rect.y && y >= rect.w + rect.height)
  42. }
  43. function intersectRect(rect1, rect2) {
  44. return !(
  45. (rect1.x + rect1.width <= rect2.x) ||
  46. (rect2.x + rect2.width <= rect1.x) ||
  47. (rect1.y + rect1.height <= rect2.y) ||
  48. (rect2.y + rect2.height <= rect1.y)
  49. )
  50. }
  51. function getParentMathematics(el) {
  52. var target
  53. var a = el
  54. var els = [];
  55. while (a) {
  56. els.unshift(a);
  57. a = a.parentElement;
  58. }
  59. var parentMathematics = els.find(el => el.getAttribute("class") === "MathElement");
  60. if ((parentMathematics) && parentMathematics.tagName === "svg") {
  61. target = parentMathematics;
  62. }
  63. return target ?? el;
  64. }
  65. function createSelectorRect() {
  66. var shape = Tools.createSVGElement("rect");
  67. shape.id = "selectionRect";
  68. shape.x.baseVal.value = 0;
  69. shape.y.baseVal.value = 0;
  70. shape.width.baseVal.value = 0;
  71. shape.height.baseVal.value = 0;
  72. shape.setAttribute("stroke", "black");
  73. shape.setAttribute("stroke-width", 1);
  74. shape.setAttribute("vector-effect", "non-scaling-stroke");
  75. shape.setAttribute("fill", "none");
  76. shape.setAttribute("stroke-dasharray", "5 5");
  77. shape.setAttribute("opacity", 1);
  78. Tools.svg.appendChild(shape);
  79. return shape;
  80. }
  81. function startMovingElements(x, y, evt) {
  82. evt.preventDefault();
  83. selectorState = selectorStates.moving;
  84. selected = { x: x, y: y };
  85. // Some of the selected elements could have been deleted
  86. selected_els = selected_els.filter(el => {
  87. return Tools.svg.getElementById(el.id) !== null
  88. });
  89. translation_elements = selected_els.map(el => {
  90. let tmatrix = get_translate_matrix(el);
  91. return { x: tmatrix.e, y: tmatrix.f }
  92. });
  93. {
  94. let tmatrix = get_translate_matrix(selectionRect);
  95. selectionRectTranslation = { x: tmatrix.e, y: tmatrix.f };
  96. }
  97. }
  98. function startSelector(x, y, evt) {
  99. evt.preventDefault();
  100. selected = { x: x, y: y };
  101. selected_els = [];
  102. selectorState = selectorStates.selecting;
  103. selectionRect.x.baseVal.value = x;
  104. selectionRect.y.baseVal.value = y;
  105. selectionRect.width.baseVal.value = 0;
  106. selectionRect.height.baseVal.value = 0;
  107. selectionRect.style.display = "";
  108. tmatrix = get_translate_matrix(selectionRect);
  109. tmatrix.e = 0;
  110. tmatrix.f = 0;
  111. }
  112. function calculateSelection() {
  113. var scale = Tools.drawingArea.getCTM().a;
  114. var selectionTBBox = selectionRect.transformedBBox(scale);
  115. return Array.from(Tools.drawingArea.children).filter(el => {
  116. return transformedBBoxIntersects(
  117. selectionTBBox,
  118. el.transformedBBox(scale)
  119. )
  120. });
  121. }
  122. function moveSelection(x, y) {
  123. var dx = x - selected.x;
  124. var dy = y - selected.y;
  125. var msgs = selected_els.map((el, i) => {
  126. return {
  127. type: "update",
  128. id: el.id,
  129. deltax: dx + translation_elements[i].x,
  130. deltay: dy + translation_elements[i].y
  131. }
  132. })
  133. var msg = {
  134. _children: msgs
  135. };
  136. {
  137. let tmatrix = get_translate_matrix(selectionRect);
  138. tmatrix.e = dx + selectionRectTranslation.x;
  139. tmatrix.f = dy + selectionRectTranslation.y;
  140. }
  141. var now = performance.now();
  142. if (now - last_sent > 70) {
  143. last_sent = now;
  144. Tools.drawAndSend(msg);
  145. } else {
  146. draw(msg);
  147. }
  148. }
  149. function updateRect(x, y, rect) {
  150. rect.x.baseVal.value = Math.min(x, selected.x);
  151. rect.y.baseVal.value = Math.min(y, selected.y);
  152. rect.width.baseVal.value = Math.abs(x - selected.x);
  153. rect.height.baseVal.value = Math.abs(y - selected.y);
  154. }
  155. function get_translate_matrix(elem) {
  156. // Returns the first translate or transform matrix or makes one
  157. var translate = null;
  158. for (var i = 0; i < elem.transform.baseVal.numberOfItems; ++i) {
  159. var baseVal = elem.transform.baseVal[i];
  160. // quick tests showed that even if one changes only the fields e and f or uses createSVGTransformFromMatrix
  161. // the brower may add a SVG_TRANSFORM_MATRIX instead of a SVG_TRANSFORM_TRANSLATE
  162. if (baseVal.type === SVGTransform.SVG_TRANSFORM_TRANSLATE || baseVal.type === SVGTransform.SVG_TRANSFORM_MATRIX) {
  163. translate = baseVal;
  164. break;
  165. }
  166. }
  167. if (translate == null) {
  168. translate = elem.transform.baseVal.createSVGTransformFromMatrix(Tools.svg.createSVGMatrix());
  169. elem.transform.baseVal.appendItem(translate);
  170. }
  171. return translate.matrix;
  172. }
  173. function draw(data) {
  174. if (data._children) {
  175. batchCall(draw, data._children);
  176. }
  177. else {
  178. switch (data.type) {
  179. case "update":
  180. var elem = Tools.svg.getElementById(data.id);
  181. if (!elem) throw new Error("Mover: Tried to move an element that does not exist.");
  182. var tmatrix = get_translate_matrix(elem);
  183. tmatrix.e = data.deltax || 0;
  184. tmatrix.f = data.deltay || 0;
  185. break;
  186. default:
  187. throw new Error("Mover: 'move' instruction with unknown type. ", data);
  188. }
  189. }
  190. }
  191. function clickSelector(x, y, evt) {
  192. var scale = Tools.drawingArea.getCTM().a
  193. selectionRect = selectionRect ?? createSelectorRect();
  194. if (pointInTransformedBBox([x, y], selectionRect.transformedBBox(scale))) {
  195. startMovingElements(x, y, evt);
  196. } else if (Tools.drawingArea.contains(evt.target)) {
  197. selectionRect.style.display = "none";
  198. selected_els = [getParentMathematics(evt.target)];
  199. startMovingElements(x, y, evt);
  200. } else {
  201. startSelector(x, y, evt);
  202. }
  203. }
  204. function releaseSelector(x, y, evt) {
  205. if (selectorState == selectorStates.selecting) {
  206. selected_els = calculateSelection();
  207. if (selected_els.length == 0) {
  208. selectionRect.style.display = "none";
  209. }
  210. }
  211. translation_elements = [];
  212. selectorState = selectorStates.pointing;
  213. }
  214. function moveSelector(x, y, evt) {
  215. if (selectorState == selectorStates.selecting) {
  216. updateRect(x, y, selectionRect);
  217. } else if (selectorState == selectorStates.moving) {
  218. moveSelection(x, y, selectionRect);
  219. }
  220. }
  221. function startHand(x, y, evt, isTouchEvent) {
  222. if (!isTouchEvent) {
  223. selected = {
  224. x: document.documentElement.scrollLeft + evt.clientX,
  225. y: document.documentElement.scrollTop + evt.clientY,
  226. }
  227. }
  228. }
  229. function moveHand(x, y, evt, isTouchEvent) {
  230. if (selected && !isTouchEvent) { //Let the browser handle touch to scroll
  231. window.scrollTo(selected.x - evt.clientX, selected.y - evt.clientY);
  232. }
  233. }
  234. function press(x, y, evt, isTouchEvent) {
  235. if (!handTool.secondary.active) startHand(x, y, evt, isTouchEvent);
  236. else clickSelector(x, y, evt, isTouchEvent);
  237. }
  238. function move(x, y, evt, isTouchEvent) {
  239. if (!handTool.secondary.active) moveHand(x, y, evt, isTouchEvent);
  240. else moveSelector(x, y, evt, isTouchEvent);
  241. }
  242. function release(x, y, evt, isTouchEvent) {
  243. move(x, y, evt, isTouchEvent);
  244. if (handTool.secondary.active) releaseSelector(x, y, evt, isTouchEvent);
  245. selected = null;
  246. }
  247. function switchTool() {
  248. selected = null;
  249. }
  250. var handTool = { //The new tool
  251. "name": "Hand",
  252. "shortcut": "h",
  253. "listeners": {
  254. "press": press,
  255. "move": move,
  256. "release": release,
  257. },
  258. "secondary": {
  259. "name": "Selector",
  260. "icon": "tools/hand/selector.svg",
  261. "active": false,
  262. "switch": switchTool,
  263. },
  264. "draw": draw,
  265. "icon": "tools/hand/hand.svg",
  266. "mouseCursor": "move",
  267. "showMarker": true,
  268. };
  269. Tools.add(handTool);
  270. Tools.change("Hand"); // Use the hand tool by default
  271. })(); //End of code isolation