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.5KB

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