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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. var selected = null;
  28. var last_sent = 0;
  29. function startMovingElement(x, y, evt) {
  30. //Prevent the press from being interpreted by the browser
  31. evt.preventDefault();
  32. if (!evt.target || !Tools.drawingArea.contains(evt.target)) return;
  33. var tmatrix = get_translate_matrix(evt.target);
  34. selected = { x: x - tmatrix.e, y: y - tmatrix.f, elem: evt.target };
  35. }
  36. function moveElement(x, y) {
  37. if (!selected) return;
  38. var deltax = x - selected.x;
  39. var deltay = y - selected.y;
  40. var msg = { type: "update", id: selected.elem.id, deltax: deltax, deltay: deltay };
  41. var now = performance.now();
  42. if (now - last_sent > 70) {
  43. last_sent = now;
  44. Tools.drawAndSend(msg);
  45. } else {
  46. draw(msg);
  47. }
  48. }
  49. function get_translate_matrix(elem) {
  50. // Returns the first translate or transform matrix or makes one
  51. var translate = null;
  52. for (var i = 0; i < elem.transform.baseVal.numberOfItems; ++i) {
  53. var baseVal = elem.transform.baseVal[i];
  54. // quick tests showed that even if one changes only the fields e and f or uses createSVGTransformFromMatrix
  55. // the brower may add a SVG_TRANSFORM_MATRIX instead of a SVG_TRANSFORM_TRANSLATE
  56. if (baseVal.type === SVGTransform.SVG_TRANSFORM_TRANSLATE || baseVal.type === SVGTransform.SVG_TRANSFORM_MATRIX) {
  57. translate = baseVal;
  58. break;
  59. }
  60. }
  61. if (translate == null) {
  62. translate = elem.transform.baseVal.createSVGTransformFromMatrix(Tools.svg.createSVGMatrix());
  63. elem.transform.baseVal.appendItem(translate);
  64. }
  65. return translate.matrix;
  66. }
  67. function draw(data) {
  68. switch (data.type) {
  69. case "update":
  70. var elem = Tools.svg.getElementById(data.id);
  71. if (!elem) throw new Error("Mover: Tried to move an element that does not exist.");
  72. var tmatrix = get_translate_matrix(elem);
  73. tmatrix.e = data.deltax || 0;
  74. tmatrix.f = data.deltay || 0;
  75. break;
  76. default:
  77. throw new Error("Mover: 'move' instruction with unknown type. ", data);
  78. }
  79. }
  80. function startHand(x, y, evt, isTouchEvent) {
  81. if (!isTouchEvent) {
  82. selected = {
  83. x: document.documentElement.scrollLeft + evt.clientX,
  84. y: document.documentElement.scrollTop + evt.clientY,
  85. }
  86. }
  87. }
  88. function moveHand(x, y, evt, isTouchEvent) {
  89. if (selected && !isTouchEvent) { //Let the browser handle touch to scroll
  90. window.scrollTo(selected.x - evt.clientX, selected.y - evt.clientY);
  91. }
  92. }
  93. function press(x, y, evt, isTouchEvent) {
  94. if (!handTool.secondary.active) startHand(x, y, evt, isTouchEvent);
  95. else startMovingElement(x, y, evt, isTouchEvent);
  96. }
  97. function move(x, y, evt, isTouchEvent) {
  98. if (!handTool.secondary.active) moveHand(x, y, evt, isTouchEvent);
  99. else moveElement(x, y, evt, isTouchEvent);
  100. }
  101. function release(x, y, evt, isTouchEvent) {
  102. move(x, y, evt, isTouchEvent);
  103. selected = null;
  104. }
  105. function switchTool() {
  106. selected = null;
  107. }
  108. var handTool = { //The new tool
  109. "name": "Hand",
  110. "shortcut": "h",
  111. "listeners": {
  112. "press": press,
  113. "move": move,
  114. "release": release,
  115. },
  116. "secondary": {
  117. "name": "Mover",
  118. "icon": "tools/hand/mover.svg",
  119. "active": false,
  120. "switch": switchTool,
  121. },
  122. "draw": draw,
  123. "icon": "tools/hand/hand.svg",
  124. "mouseCursor": "move",
  125. "showMarker": true,
  126. };
  127. Tools.add(handTool);
  128. Tools.change("Hand"); // Use the hand tool by default
  129. })(); //End of code isolation