您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

board.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. var Tools = {};
  2. Tools.svg = document.getElementById("canvas");
  3. Tools.socket = io.connect('');
  4. Tools.HTML = {
  5. template : new Minitpl("#tools > .tool"),
  6. addTool : function(toolName) {
  7. var callback = function () {
  8. Tools.change(toolName);
  9. };
  10. return this.template.add({
  11. "a" : function (elem) {
  12. elem.addEventListener("click", callback);
  13. elem.id = "toolID-"+toolName;
  14. return toolName;
  15. }
  16. });
  17. }
  18. }
  19. Tools.list = {}; // An array of all known tools. {"toolName" : {toolObject}}
  20. Tools.add = function (newTool) {
  21. if (newTool.name in Tools.list) {
  22. console.log("Tools.add: The tool '"+newTool.name+"' is already" +
  23. "in the list. Updating it...");
  24. }
  25. //Add the tool to the list
  26. Tools.list[newTool.name] = newTool;
  27. //Add the tool to the GUI
  28. Tools.HTML.addTool(newTool.name);
  29. }
  30. Tools.change = function (toolName){
  31. if (! (toolName in Tools.list)) {
  32. throw "Trying to select a tool that has never been added!";
  33. }
  34. var newtool = Tools.list[toolName];
  35. //There is not necessarily already a curTool
  36. if (Tools.curTool) {
  37. //It's useless to do anything if the new tool is already selected
  38. if (newtool === curTool) return;
  39. //Remove the old event listeners
  40. for (var event in Tools.curTool.listeners) {
  41. var listener = Tools.curTool.listeners[event];
  42. Tools.svg.removeEventListener(event, listener);
  43. }
  44. }
  45. //Add the new event listeners
  46. for (var event in newtool.listeners) {
  47. var listener = newtool.listeners[event];
  48. Tools.svg.addEventListener(event, listener);
  49. }
  50. Tools.curTool = newtool;
  51. }
  52. Tools.drawAndSend = function (data) {
  53. Tools.curTool.draw(data);
  54. Tools.socket.emit('broadcast', {
  55. 'tool' : curTool.name,
  56. 'data' : data
  57. });
  58. }
  59. Tools.socket.on("broadcast", function (message){
  60. var tool = Tools.list[message.tool];
  61. if (tool) {
  62. tool.draw(message.data);
  63. }
  64. });
  65. /**
  66. What does a "tool" object look like?
  67. newtool = {
  68. "name" : "SuperTool",
  69. "listeners" : {
  70. "mousedown" : function(x){...},
  71. "mousemove" : function(x){...}
  72. },
  73. "draw" : function(data){
  74. //Print the data on Tools.svg
  75. }
  76. }
  77. */
  78. (function(){
  79. var pen = {
  80. "name" : "pen"
  81. }; //pen is a tool
  82. })();
  83. var socket = io.connect('');
  84. socket.on('broadcast', function (data) {
  85. switch(data.type) {
  86. case "newLine":
  87. createLine(data.id);
  88. break;
  89. case "point":
  90. var line = svg.getElementById(data.line);
  91. if (!line) {
  92. throw "Hmmm... I received a point of a line I don't know...";
  93. }
  94. addPoint(line, data.x, data.y);
  95. break;
  96. }
  97. });
  98. var svg = document.getElementById("canvas");
  99. curLine = null;
  100. svg.width.baseVal.value = document.body.clientWidth;
  101. svg.height.baseVal.value = document.body.clientHeight;
  102. var lastTime = performance.now();
  103. function pressHandler (ev) {
  104. curLine = createLine();
  105. socket.emit('broadcast', {
  106. 'type' : 'newLine',
  107. 'id' : curLine.id
  108. });
  109. ev.preventDefault();
  110. return false;
  111. }
  112. svg.addEventListener("mousedown", pressHandler);
  113. //svg.addEventListener("touchstart", pressHandler);
  114. function moveHandler (ev){
  115. if (curLine !== null &&
  116. performance.now() - lastTime > 50) {
  117. var x = ev.clientX + window.scrollX;
  118. y = ev.clientY + window.scrollY;
  119. socket.emit('broadcast', {
  120. 'type' : 'point',
  121. 'line' : curLine.id,
  122. 'x' : x,
  123. 'y' : y
  124. });
  125. addPoint(curLine, x,y);
  126. lastTime = performance.now();
  127. }
  128. ev.preventDefault();
  129. return false;
  130. }
  131. svg.addEventListener("mousemove", moveHandler);
  132. //svg.addEventListener("touchmove", moveHandler);
  133. function releaseHandler (){
  134. curLine = null;
  135. return false;
  136. }
  137. svg.addEventListener("mouseup", releaseHandler);
  138. svg.addEventListener("mouseleave", releaseHandler);
  139. //svg.addEventListener("touchend", releaseHandler);
  140. function addPoint (line, x,y) {
  141. var point = svg.createSVGPoint();
  142. point.x = x; point.y = y;
  143. line.points.appendItem(point);
  144. if (x > svg.width.baseVal.value - 100) {
  145. svg.width.baseVal.value = x + 1000;
  146. }
  147. if (y > svg.height.baseVal.value - 100) {
  148. svg.height.baseVal.value = y + 1000;
  149. }
  150. }
  151. function createLine(id) {
  152. var line = document.createElementNS(svg.namespaceURI, "polyline");
  153. if (!id) id = generateUID("line");
  154. line.id = id;
  155. svg.appendChild(line);
  156. return line;
  157. }
  158. function generateUID(prefix, suffix) {
  159. var rndStr = (Math.round(Math.random()*1e19)).toString(36);
  160. if (prefix) rndStr = prefix + rndStr;
  161. if (suffix) rndStr = rndStr + suffix;
  162. return rndStr;
  163. }