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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 is the element over which the mouse is.*/
  40. if (erasing && evt.target !== Tools.svg) {
  41. msg.id = evt.target.id;
  42. Tools.drawAndSend(msg);
  43. }
  44. }
  45. function stopErasing (){
  46. erasing = false;
  47. }
  48. function draw(data) {
  49. var elem;
  50. switch(data.type) {
  51. //TODO: add the ability to erase only some points in a line
  52. case "delete":
  53. elem = svg.getElementById(data.id);
  54. if (elem === null) console.error("Eraser: Tried to delete an element that does not exist.");
  55. else svg.removeChild(elem);
  56. break;
  57. default:
  58. console.error("Eraser: 'delete' instruction with unknown type. ", data);
  59. break;
  60. }
  61. }
  62. var svg = Tools.svg;
  63. Tools.add({ //The new tool
  64. "name" : "Eraser",
  65. "listeners" : {
  66. "press" : startErasing,
  67. "move" : erase,
  68. "release" : stopErasing,
  69. },
  70. "draw" : draw,
  71. "mouseCursor" : "crosshair",
  72. });
  73. })(); //End of code isolation