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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 line the user is currently drawing or an empty string while the user is not drawing
  28. var curLineId = "",
  29. curPoint = { //The data of the message that will be sent for every new point
  30. 'type' : 'point',
  31. 'line' : "",
  32. 'x' : 0,
  33. 'y' : 0
  34. },
  35. lastTime = performance.now(); //The time at which the last point was drawn
  36. function startLine (x,y, evt) {
  37. //Prevent the press from being interpreted by the browser
  38. evt.preventDefault();
  39. curLineId = Tools.generateUID("l"); //"l" for line
  40. Tools.drawAndSend({
  41. 'type' : 'line',
  42. 'id' : curLineId,
  43. 'color' : Tools.getColor(),
  44. 'size' : Tools.getSize()
  45. });
  46. //Update the current point
  47. curPoint.line = curLineId;
  48. //Immediatly add a point to the line
  49. continueLine(x,y);
  50. }
  51. function continueLine (x,y){
  52. /*Wait 50ms before adding any point to the currently drawing line.
  53. This allows the animation to be smother*/
  54. if (curLineId !== "" &&
  55. performance.now() - lastTime > 50) {
  56. curPoint.x = x; curPoint.y = y;
  57. Tools.drawAndSend(curPoint);
  58. lastTime = performance.now();
  59. }
  60. }
  61. function stopLine (x,y){
  62. //Add a last point to the line
  63. continueLine(x+1,y+1);
  64. curLineId = "";
  65. }
  66. var renderingLine = {};
  67. function draw(data) {
  68. switch(data.type) {
  69. case "line":
  70. renderingLine = createLine(data);
  71. break;
  72. case "point":
  73. var line = (renderingLine.id == data.line) ? renderingLine : svg.getElementById(data.line);
  74. if (!line) {
  75. console.log("Pencil: Hmmm... I received a point of a line I don't know...");
  76. line = renderingLine = createLine(data.id);
  77. }
  78. addPoint(line, data.x, data.y);
  79. break;
  80. case "endline":
  81. //TODO?
  82. break;
  83. default:
  84. console.log("Pencil: Draw instruction with unknown type. ", data);
  85. break;
  86. }
  87. }
  88. var svg = Tools.svg;
  89. function addPoint (line, x,y) {
  90. var point = svg.createSVGPoint();
  91. point.x = x; point.y = y;
  92. line.points.appendItem(point);
  93. }
  94. function createLine(lineData) {
  95. var line = Tools.createSVGElement("polyline");
  96. line.id = lineData.id;
  97. line.setAttribute("stroke", lineData.color);
  98. line.setAttribute("stroke-width", lineData.size);
  99. svg.appendChild(line);
  100. return line;
  101. }
  102. Tools.add({ //The new tool
  103. "name" : "Pencil",
  104. "listeners" : {
  105. "press" : startLine,
  106. "move" : continueLine,
  107. "release" : stopLine,
  108. },
  109. "draw" : draw,
  110. "mouseCursor" : "crosshair",
  111. "stylesheet" : "tools/pencil/pencil.css"
  112. });
  113. //The pencil tool is selected by default
  114. Tools.change("Pencil");
  115. })(); //End of code isolation