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

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