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.

eraser.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 eraser() { //Code isolation
  27. var erasing = false;
  28. function startErasing(x, y, evt) {
  29. //Prevent the press from being interpreted by the browser
  30. evt.preventDefault();
  31. erasing = true;
  32. erase(x, y, evt);
  33. }
  34. var msg = {
  35. "type": "delete",
  36. "id": ""
  37. };
  38. function inDrawingArea(elem) {
  39. return Tools.drawingArea.contains(elem);
  40. }
  41. function erase(x, y, evt) {
  42. // evt.target should be the element over which the mouse is...
  43. var target = evt.target;
  44. if (evt.type === "touchmove") {
  45. // ... the target of touchmove events is the element that was initially touched,
  46. // not the one **currently** being touched
  47. var touch = evt.touches[0];
  48. target = document.elementFromPoint(touch.clientX, touch.clientY);
  49. }
  50. if (erasing && target !== Tools.svg && target !== Tools.drawingArea && inDrawingArea(target)) {
  51. msg.id = target.id;
  52. Tools.drawAndSend(msg);
  53. }
  54. }
  55. function stopErasing() {
  56. erasing = false;
  57. }
  58. function draw(data) {
  59. var elem;
  60. switch (data.type) {
  61. //TODO: add the ability to erase only some points in a line
  62. case "delete":
  63. elem = svg.getElementById(data.id);
  64. if (elem === null) console.error("Eraser: Tried to delete an element that does not exist.");
  65. else Tools.drawingArea.removeChild(elem);
  66. break;
  67. default:
  68. console.error("Eraser: 'delete' instruction with unknown type. ", data);
  69. break;
  70. }
  71. }
  72. var svg = Tools.svg;
  73. Tools.add({ //The new tool
  74. "name": "Eraser",
  75. "shortcut": "e",
  76. "listeners": {
  77. "press": startErasing,
  78. "move": erase,
  79. "release": stopErasing,
  80. },
  81. "draw": draw,
  82. "icon": "tools/eraser/icon.svg",
  83. "mouseCursor": "crosshair",
  84. "showMarker": true,
  85. });
  86. })(); //End of code isolation