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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. var Tools = {};
  27. Tools.board = document.getElementById("board");
  28. Tools.svg = document.getElementById("canvas");
  29. Tools.socket = io.connect('', {
  30. "reconnection delay" : 1, //Make the xhr connections as fast as possible
  31. });
  32. Tools.curTool = null;
  33. //Get the board as soon as the page is loaded
  34. Tools.socket.emit("getboard");
  35. Tools.HTML = {
  36. template : new Minitpl("#tools > .tool"),
  37. addTool : function(toolName, toolIcon) {
  38. var callback = function () {
  39. Tools.change(toolName);
  40. };
  41. return this.template.add(function (elem) {
  42. elem.addEventListener("click", callback);
  43. elem.id = "toolID-"+toolName;
  44. elem.getElementsByClassName("tool-name")[0].textContent = toolName;
  45. elem.getElementsByClassName("tool-icon")[0].className += " " + toolIcon;
  46. }
  47. );
  48. },
  49. changeTool : function(oldToolName, newToolName) {
  50. var oldTool = document.getElementById("toolID-"+oldToolName);
  51. var newTool = document.getElementById("toolID-"+newToolName);
  52. if (oldTool) oldTool.classList.remove("curTool");
  53. if (newTool) newTool.classList.add("curTool");
  54. },
  55. addStylesheet : function(href) {
  56. //Adds a css stylesheet to the html or svg document
  57. var link = document.createElement("link");
  58. link.href = href;
  59. link.rel = "stylesheet";
  60. link.type = "text/css";
  61. document.head.appendChild(link);
  62. }
  63. };
  64. Tools.list = {}; // An array of all known tools. {"toolName" : {toolObject}}
  65. Tools.add = function (newTool) {
  66. if (newTool.name in Tools.list) {
  67. console.log("Tools.add: The tool '"+newTool.name+"' is already" +
  68. "in the list. Updating it...");
  69. }
  70. //Format the new tool correctly
  71. Tools.applyHooks(Tools.toolHooks, newTool);
  72. //Add the tool to the list
  73. Tools.list[newTool.name] = newTool;
  74. if (newTool.stylesheet) {
  75. Tools.HTML.addStylesheet(newTool.stylesheet);
  76. }
  77. //Add the tool to the GUI
  78. Tools.HTML.addTool(newTool.name, newTool.icon);
  79. //There may be pending messages for the tool
  80. var pending = Tools.pendingMessages[newTool.name];
  81. if (pending) {
  82. console.log("Drawing pending messages for '%s'.", newTool.name);
  83. var msg;
  84. while (msg = pending.shift()) {
  85. //Transmit the message to the tool (precising that it comes from the network)
  86. newTool.draw(msg, false);
  87. }
  88. }
  89. };
  90. Tools.change = function (toolName){
  91. if (! (toolName in Tools.list)) {
  92. throw "Trying to select a tool that has never been added!";
  93. }
  94. var newtool = Tools.list[toolName];
  95. //Update the GUI
  96. var curToolName = (Tools.curTool) ? Tools.curTool.name : "";
  97. try {
  98. Tools.HTML.changeTool(curToolName, toolName);
  99. } catch (e) {
  100. console.error("Unable to update the GUI with the new tool. "+e);
  101. }
  102. Tools.svg.style.cursor = newtool.mouseCursor || "auto";
  103. //There is not necessarily already a curTool
  104. if (Tools.curTool !== null) {
  105. //It's useless to do anything if the new tool is already selected
  106. if (newtool === Tools.curTool) return;
  107. //Remove the old event listeners
  108. for (var event in Tools.curTool.compiledListeners) {
  109. var listener = Tools.curTool.compiledListeners[event];
  110. Tools.board.removeEventListener(event, listener);
  111. }
  112. //Call the callbacks of the old tool
  113. Tools.curTool.onquit(newtool);
  114. }
  115. //Add the new event listeners
  116. for (var event in newtool.compiledListeners) {
  117. var listener = newtool.compiledListeners[event];
  118. Tools.board.addEventListener(event, listener);
  119. }
  120. //Call the start callback of the new tool
  121. newtool.onstart(Tools.curTool);
  122. Tools.curTool = newtool;
  123. };
  124. Tools.send = function(data, toolName){
  125. toolName = toolName || Tools.curTool.name;
  126. var message = data;
  127. message.tool = toolName;
  128. Tools.applyHooks(Tools.messageHooks, message);
  129. Tools.socket.emit('broadcast', message);
  130. };
  131. Tools.drawAndSend = function (data) {
  132. Tools.curTool.draw(data, true);
  133. Tools.send(data);
  134. };
  135. //Object containing the messages that have been received before the corresponding tool
  136. //is loaded. keys : the name of the tool, values : array of messages for this tool
  137. Tools.pendingMessages = {};
  138. //Receive draw instructions from the server
  139. Tools.socket.on("broadcast", function (message){
  140. //Check if the message is in the expected format
  141. if (message.tool) {
  142. var tool = Tools.list[message.tool];
  143. if (tool) {
  144. Tools.applyHooks(Tools.messageHooks, message);
  145. tool.draw(message, false); //draw the received data
  146. if (message._children) {
  147. for (var i=0; i<message._children.length; i++) {
  148. //Apply hooks on children too
  149. Tools.applyHooks(Tools.messageHooks, message._children[i]);
  150. tool.draw(message._children[i]);
  151. }
  152. }
  153. } else {
  154. //We received a message destinated to a tool that we don't have
  155. //So we add it to the pending messages
  156. if (Tools.pendingMessages[message.tool] === undefined) {
  157. Tools.pendingMessages[message.tool] = [];
  158. }
  159. Tools.pendingMessages[message.tool].push(message);
  160. }
  161. } else {
  162. console.error("Received a badly formatted message (no tool). ", message);
  163. }
  164. });
  165. //List of hook functions that will be applied to messages before sending or drawing them
  166. Tools.messageHooks = [
  167. function resizeCanvas (m) {
  168. //Enlarge the canvas is something is drawn near its border
  169. if (m.x && m.y) {
  170. var svg = Tools.svg, x=m.x, y=m.y;
  171. if (x > svg.width.baseVal.value - 1000) {
  172. svg.width.baseVal.value = x + 2000;
  173. }
  174. if (y > svg.height.baseVal.value - 500) {
  175. svg.height.baseVal.value = y + 2000;
  176. }
  177. }
  178. }
  179. ];
  180. //List of hook functions that will be applied to tools before adding them
  181. Tools.toolHooks = [
  182. function checkToolAttributes(tool) {
  183. if (typeof(tool.name)!=="string") throw "A tool must have a name";
  184. if (typeof(tool.listeners)!=="object") {
  185. tool.listeners = {};
  186. }
  187. if (typeof(tool.onstart)!=="function") {
  188. tool.onstart = function(){};
  189. }
  190. if (typeof(tool.onquit)!=="function") {
  191. tool.onquit = function(){};
  192. }
  193. },
  194. function compileListeners (tool) {
  195. //compile listeners into compiledListeners
  196. var listeners = tool.listeners;
  197. //A tool may provide precompiled listeners
  198. var compiled = tool.compiledListeners || {};
  199. tool.compiledListeners = compiled;
  200. function compile (listener) { //closure
  201. return (function listen (evt){
  202. var x = evt.pageX,
  203. y = evt.pageY;
  204. return listener(x,y,evt,false);
  205. });
  206. }
  207. function compileTouch (listener) { //closure
  208. return (function touchListen (evt) {
  209. //Currently, we don't handle multitouch
  210. if (evt.changedTouches.length === 1) {
  211. //evt.preventDefault();
  212. var touch = evt.changedTouches[0];
  213. var x = touch.pageX,
  214. y = touch.pageY;
  215. return listener(x,y,evt,true);
  216. }
  217. return true;
  218. });
  219. }
  220. if (listeners.press) {
  221. compiled["mousedown"] = compile(listeners.press);
  222. compiled["touchstart"] = compileTouch(listeners.press);
  223. }
  224. if (listeners.move) {
  225. compiled["mousemove"] = compile(listeners.move);
  226. compiled["touchmove"] = compileTouch(listeners.move);
  227. }
  228. if (listeners.release) {
  229. var release = compile(listeners.release),
  230. releaseTouch = compileTouch(listeners.release);
  231. compiled["mouseup"] = release;
  232. compiled["mouseleave"] = release;
  233. compiled["touchleave"] = releaseTouch;
  234. compiled["touchend"] = releaseTouch;
  235. compiled["touchcancel"] = releaseTouch;
  236. }
  237. }
  238. ];
  239. Tools.applyHooks = function(hooks, object) {
  240. //Apply every hooks on the object
  241. hooks.forEach(function(hook) {
  242. hook(object);
  243. });
  244. };
  245. // Utility functions
  246. Tools.generateUID = function (prefix, suffix) {
  247. var uid = Date.now().toString(36); //Create the uids in chronological order
  248. uid += (Math.round(Math.random()*36)).toString(36); //Add a random character at the end
  249. if (prefix) uid = prefix + uid;
  250. if (suffix) uid = uid + suffix;
  251. return uid;
  252. };
  253. Tools.createSVGElement = function (name) {
  254. return document.createElementNS(Tools.svg.namespaceURI, name);
  255. };
  256. Tools.positionElement = function (elem, x, y) {
  257. elem.style.top = y+"px";
  258. elem.style.left = x+"px";
  259. };
  260. (function color (){
  261. var chooser = document.getElementById("chooseColor");
  262. Tools.getColor = function(){
  263. return chooser.value;
  264. };
  265. })();
  266. (function size (){
  267. var chooser = document.getElementById("chooseSize");
  268. function update (){
  269. if (chooser.value<1 || chooser.value > 50) {
  270. chooser.value=3;
  271. }
  272. }
  273. update();
  274. chooser.onchange = update;
  275. Tools.getSize = function(){
  276. return chooser.value;
  277. };
  278. })();
  279. //Scale the canvas on load
  280. Tools.svg.width.baseVal.value = document.body.clientWidth;
  281. Tools.svg.height.baseVal.value = document.body.clientHeight;
  282. (function menu () {
  283. var menu = document.getElementById("menu");
  284. tog = document.getElementById("toggleMenu");
  285. tog.onclick = function(e){
  286. menu.classList.toggle("closed");
  287. };
  288. })();
  289. /*********** Polyfills ***********/
  290. if (!window.performance || !window.performance.now) {
  291. window.performance = {
  292. "now" : Date.now
  293. }
  294. }
  295. if (!Math.hypot) {
  296. Math.hypot = function (x,y) {
  297. //The true Math.hypot accepts any number of parameters
  298. return Math.sqrt(x*x+y*y);
  299. }
  300. }
  301. /**
  302. What does a "tool" object look like?
  303. newtool = {
  304. "name" : "SuperTool",
  305. "listeners" : {
  306. "press" : function(x,y,evt){...},
  307. "move" : function(x,y,evt){...},
  308. "release" : function(x,y,evt){...},
  309. },
  310. "draw" : function(data, isLocal){
  311. //Print the data on Tools.svg
  312. },
  313. "onstart" : function(oldTool){...},
  314. "onquit" : function(newTool){...},
  315. "stylesheet" : "style.css",
  316. }
  317. */