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.

rect.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(){ //Code isolation
  27. //Indicates the id of the shape the user is currently drawing or an empty string while the user is not drawing
  28. var curId = "",
  29. curUpdate = { //The data of the message that will be sent for every new point
  30. 'type' : 'update',
  31. 'id' : "",
  32. 'x' : 0,
  33. 'y' : 0,
  34. 'x2' : 0,
  35. 'y2' : 0
  36. },
  37. lastTime = performance.now(); //The time at which the last point was drawn
  38. function start (x,y, evt) {
  39. //Prevent the press from being interpreted by the browser
  40. evt.preventDefault();
  41. curId = Tools.generateUID("r"); //"r" for rectangle
  42. Tools.drawAndSend({
  43. 'type' : 'rect',
  44. 'id' : curId,
  45. 'color' : Tools.getColor(),
  46. 'size' : Tools.getSize(),
  47. 'x' : x,
  48. 'y' : y,
  49. 'x2': x,
  50. 'y2': y
  51. });
  52. curUpdate.id = curId;
  53. curUpdate.x = x;
  54. curUpdate.y = y;
  55. }
  56. function move (x,y, evt){
  57. /*Wait 70ms before adding any point to the currently drawing shape.
  58. This allows the animation to be smother*/
  59. if (curId !== "") {
  60. curUpdate['x2'] = x; curUpdate['y2'] = y;
  61. if (performance.now() - lastTime > 70) {
  62. Tools.drawAndSend(curUpdate);
  63. lastTime = performance.now();
  64. } else {
  65. draw(curUpdate);
  66. }
  67. }
  68. if (evt) evt.preventDefault();
  69. }
  70. function stop (x,y){
  71. //Add a last point to the shape
  72. move(x,y);
  73. curId = "";
  74. }
  75. function draw(data) {
  76. switch(data.type) {
  77. case "rect":
  78. createShape(data);
  79. break;
  80. case "update":
  81. var shape = svg.getElementById(data['id']);
  82. if (!shape) {
  83. console.error("Straight shape: Hmmm... I received a point of a rect that has not been created (%s).", data['id']);
  84. createShape({ //create a new shape in order not to loose the points
  85. "id": data['id'],
  86. "x": data['x2'],
  87. "y": data['y2']
  88. });
  89. }
  90. updateShape(shape, data);
  91. break;
  92. default:
  93. console.error("Straight shape: Draw instruction with unknown type. ", data);
  94. break;
  95. }
  96. }
  97. var svg = Tools.svg;
  98. function createShape(data) {
  99. //Creates a new shape on the canvas, or update a shape that already exists with new information
  100. var shape = svg.getElementById(data.id) || Tools.createSVGElement("rect");
  101. shape.id = data.id;
  102. updateShape(shape, data);
  103. //If some data is not provided, choose default value. The shape may be updated later
  104. shape.setAttribute("stroke", data.color || "black" );
  105. shape.setAttribute("stroke-width", data.size || 10 );
  106. svg.appendChild(shape);
  107. return shape;
  108. }
  109. function updateShape(shape, data) {
  110. shape.x.baseVal.value = Math.min(data['x2'], data['x']);
  111. shape.y.baseVal.value = Math.min(data['y2'], data['y']);
  112. shape.width.baseVal.value = Math.abs(data['x2'] - data['x']);
  113. shape.height.baseVal.value = Math.abs(data['y2'] - data['y']);
  114. }
  115. Tools.add({ //The new tool
  116. "name" : "Rectangle",
  117. "icon" : "▢",
  118. "listeners" : {
  119. "press" : start,
  120. "move" : move,
  121. "release" : stop,
  122. },
  123. "draw" : draw,
  124. "mouseCursor" : "crosshair",
  125. "stylesheet" : "tools/rect/rect.css"
  126. });
  127. })(); //End of code isolation