Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

rect.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. 'opacity': Tools.getOpacity(),
  48. 'x': x,
  49. 'y': y,
  50. 'x2': x,
  51. 'y2': y
  52. });
  53. curUpdate.id = curId;
  54. curUpdate.x = x;
  55. curUpdate.y = y;
  56. }
  57. function move(x, y, evt) {
  58. /*Wait 70ms before adding any point to the currently drawing shape.
  59. This allows the animation to be smother*/
  60. if (curId !== "") {
  61. curUpdate['x2'] = x; curUpdate['y2'] = y;
  62. if (performance.now() - lastTime > 70) {
  63. Tools.drawAndSend(curUpdate);
  64. lastTime = performance.now();
  65. } else {
  66. draw(curUpdate);
  67. }
  68. }
  69. if (evt) evt.preventDefault();
  70. }
  71. function stop(x, y) {
  72. //Add a last point to the shape
  73. move(x, y);
  74. curId = "";
  75. }
  76. function draw(data) {
  77. switch (data.type) {
  78. case "rect":
  79. createShape(data);
  80. break;
  81. case "update":
  82. var shape = svg.getElementById(data['id']);
  83. if (!shape) {
  84. console.error("Straight shape: Hmmm... I received a point of a rect that has not been created (%s).", data['id']);
  85. createShape({ //create a new shape in order not to loose the points
  86. "id": data['id'],
  87. "x": data['x2'],
  88. "y": data['y2']
  89. });
  90. }
  91. updateShape(shape, data);
  92. break;
  93. default:
  94. console.error("Straight shape: Draw instruction with unknown type. ", data);
  95. break;
  96. }
  97. }
  98. var svg = Tools.svg;
  99. function createShape(data) {
  100. //Creates a new shape on the canvas, or update a shape that already exists with new information
  101. var shape = svg.getElementById(data.id) || Tools.createSVGElement("rect");
  102. shape.id = data.id;
  103. updateShape(shape, data);
  104. //If some data is not provided, choose default value. The shape may be updated later
  105. shape.setAttribute("stroke", data.color || "black");
  106. shape.setAttribute("stroke-width", data.size || 10);
  107. shape.setAttribute("opacity", Math.max(0.1, Math.min(1, data.opacity)) || 1);
  108. svg.appendChild(shape);
  109. return shape;
  110. }
  111. function updateShape(shape, data) {
  112. shape.x.baseVal.value = Math.min(data['x2'], data['x']);
  113. shape.y.baseVal.value = Math.min(data['y2'], data['y']);
  114. shape.width.baseVal.value = Math.abs(data['x2'] - data['x']);
  115. shape.height.baseVal.value = Math.abs(data['y2'] - data['y']);
  116. }
  117. Tools.add({ //The new tool
  118. "name": "Rectangle",
  119. "icon": "▢",
  120. "shortcut": "r",
  121. "listeners": {
  122. "press": start,
  123. "move": move,
  124. "release": stop,
  125. },
  126. "draw": draw,
  127. "mouseCursor": "crosshair",
  128. "stylesheet": "tools/rect/rect.css"
  129. });
  130. })(); //End of code isolation