Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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