not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.8 KiB

  1. /* PaintApp is a big object containing UI stuff, libraries and helpers */
  2. define([], function() {
  3. /* Function to disable all active class inside the toolbar */
  4. function paletteRemoveActiveClass() {
  5. for (var i = 0; i < PaintApp.paletteModesButtons.length; i++) {
  6. PaintApp.paletteModesButtons[i].className = PaintApp.paletteModesButtons[i].className.replace(/(?:^|\s)active(?!\S)/g, '');
  7. }
  8. }
  9. /* Function to add active class to a specific element */
  10. function addActiveClassToElement(element) {
  11. element.className += ' active';
  12. }
  13. /* Switch current drawing mode */
  14. function switchMode(newMode) {
  15. saveCanvas();
  16. PaintApp.data.mode = newMode;
  17. PaintApp.handleCurrentFloatingElement();
  18. PaintApp.data.tool.onMouseDown = PaintApp.modes[newMode].onMouseDown;
  19. PaintApp.data.tool.onMouseDrag = PaintApp.modes[newMode].onMouseDrag;
  20. PaintApp.data.tool.onMouseUp = PaintApp.modes[newMode].onMouseUp;
  21. }
  22. /* Clear the canvas */
  23. function clearCanvas() {
  24. var ctx = PaintApp.elements.canvas.getContext('2d');
  25. ctx.fillStyle = "#ffffff"
  26. ctx.fillRect(0, 0, parseInt(PaintApp.elements.canvas.style.width), parseInt(PaintApp.elements.canvas.style.height));
  27. PaintApp.saveCanvas();
  28. }
  29. /* Trigger undo click, undo using the history */
  30. function undoCanvas() {
  31. //Removing any floating element
  32. PaintApp.handleCurrentFloatingElement();
  33. if (PaintApp.data.history.undo.length < 2) {
  34. return;
  35. }
  36. PaintApp.modes.Pen.point = undefined;
  37. var canvas = PaintApp.elements.canvas;
  38. var ctx = canvas.getContext('2d');
  39. var img = new Image();
  40. var imgSrc = PaintApp.data.history.undo[PaintApp.data.history.undo.length - 2];
  41. var imgSrc2 = PaintApp.data.history.undo[PaintApp.data.history.undo.length - 1];
  42. /* Loading of the image stored in history */
  43. img.onload = function() {
  44. ctx.save();
  45. ctx.setTransform(1, 0, 0, 1, 0, 0);
  46. ctx.clearRect(0, 0, canvas.width, canvas.height);
  47. ctx.drawImage(img, 0, 0);
  48. ctx.restore();
  49. /* If the activity is shared, will send the instruction to everyone */
  50. if (PaintApp.data.isShared) {
  51. try {
  52. PaintApp.collaboration.sendMessage({
  53. action: 'toDataURL',
  54. data: {
  55. width: PaintApp.elements.canvas.width / window.devicePixelRatio,
  56. height: PaintApp.elements.canvas.height / window.devicePixelRatio,
  57. src: PaintApp.collaboration.compress(PaintApp.elements.canvas.toDataURL())
  58. }
  59. });
  60. } catch (e) {
  61. return;
  62. }
  63. }
  64. };
  65. img.src = imgSrc;
  66. PaintApp.data.history.redo.push(imgSrc2);
  67. PaintApp.data.history.undo.pop();
  68. /* Update the availability of undo/redo */
  69. displayUndoRedoButtons();
  70. return imgSrc;
  71. }
  72. /* Trigger redo click, undo using the history */
  73. function redoCanvas() {
  74. //Removing any floating element
  75. handleCurrentFloatingElement();
  76. if (PaintApp.data.history.redo.length === 0) {
  77. return;
  78. }
  79. PaintApp.modes.Pen.point = undefined;
  80. var canvas = PaintApp.elements.canvas;
  81. var ctx = canvas.getContext('2d');
  82. var img = new Image();
  83. var imgSrc = PaintApp.data.history.redo[PaintApp.data.history.redo.length - 1];
  84. /* Loading of the image stored in history */
  85. img.onload = function() {
  86. ctx.save();
  87. ctx.setTransform(1, 0, 0, 1, 0, 0);
  88. ctx.clearRect(0, 0, canvas.width, canvas.height);
  89. ctx.drawImage(img, 0, 0);
  90. ctx.restore();
  91. /* If the activity is shared, will send the instruction to everyone */
  92. if (PaintApp.data.isShared) {
  93. try {
  94. PaintApp.collaboration.sendMessage({
  95. action: 'toDataURL',
  96. data: {
  97. width: PaintApp.elements.canvas.width / window.devicePixelRatio,
  98. height: PaintApp.elements.canvas.height / window.devicePixelRatio,
  99. src: PaintApp.collaboration.compress(PaintApp.elements.canvas.toDataURL())
  100. }
  101. });
  102. } catch (e) {}
  103. }
  104. };
  105. img.src = imgSrc;
  106. PaintApp.data.history.undo.push(imgSrc);
  107. PaintApp.data.history.redo.pop();
  108. /* Update the availability of undo/redo */
  109. displayUndoRedoButtons();
  110. return imgSrc;
  111. }
  112. /* Update the availability of undo/redo */
  113. function displayUndoRedoButtons() {
  114. var notAvailableOpacity = '0.4';
  115. var availableOpacity = '1';
  116. /* If activity is shared and we are not the host we cannot do undo/redo */
  117. if (PaintApp.data.isShared && !PaintApp.data.isHost) {
  118. PaintApp.elements.redoButton.style.opacity = notAvailableOpacity;
  119. PaintApp.elements.undoButton.style.opacity = notAvailableOpacity;
  120. return;
  121. }
  122. /* Check of the ability to use redo */
  123. if (PaintApp.data.history.redo.length === 0) {
  124. PaintApp.elements.redoButton.style.opacity = notAvailableOpacity;
  125. } else {
  126. PaintApp.elements.redoButton.style.opacity = availableOpacity;
  127. }
  128. /* Check of the ability to do use undo */
  129. if (PaintApp.data.history.undo.length <= 1) {
  130. PaintApp.elements.undoButton.style.opacity = notAvailableOpacity;
  131. } else {
  132. PaintApp.elements.undoButton.style.opacity = availableOpacity;
  133. }
  134. }
  135. /* Storing canvas onto history */
  136. function saveCanvas() {
  137. var canvas = PaintApp.elements.canvas;
  138. try {
  139. var image = canvas.toDataURL();
  140. } catch (e) {}
  141. /* If doing a new action, setting redo to an empty list */
  142. if ((PaintApp.data.history.undo.length > 0 && PaintApp.data.history.undo[PaintApp.data.history.undo.length - 1] !== image) || (PaintApp.data.history.undo.length === 0)) {
  143. PaintApp.data.history.undo.push(image);
  144. PaintApp.data.history.redo = [];
  145. }
  146. /* Limiting history size*/
  147. if (PaintApp.data.history.redo.length > PaintApp.data.history.limit) {
  148. PaintApp.data.history.redo = PaintApp.data.history.redo.slice(1);
  149. }
  150. /* Limiting history size*/
  151. if (PaintApp.data.history.undo.length > PaintApp.data.history.limit) {
  152. PaintApp.data.history.undo = PaintApp.data.history.undo.slice(1);
  153. }
  154. /* Refreshing undo / redo */
  155. displayUndoRedoButtons();
  156. /* If sharedActivity and not the host, tell the host to save his copy of the canvas */
  157. if (PaintApp.data.isShared && !PaintApp.data.isHost) {
  158. PaintApp.collaboration.sendMessage({
  159. action: 'saveCanvas'
  160. });
  161. }
  162. }
  163. /* Removing floating elements used by copy/paste, stamps, text */
  164. function handleCurrentFloatingElement() {
  165. if (PaintApp.data.currentElement !== undefined) {
  166. if (PaintApp.data.currentElement.type != 'copy/paste') {
  167. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  168. PaintApp.data.currentElement = undefined;
  169. } else {
  170. if (PaintApp.data.mode != 'Copy' && PaintApp.data.mode != 'Paste') {
  171. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  172. PaintApp.data.currentElement = undefined;
  173. }
  174. }
  175. }
  176. }
  177. /* PaintApp, contains the context of the application */
  178. var PaintApp = {
  179. libs: {},
  180. palettes: {},
  181. elements: {},
  182. buttons: {},
  183. paletteModesButtons: [],
  184. data: {
  185. isCompressEnabled: true,
  186. isHost: true,
  187. history: {
  188. limit: 15,
  189. undo: [],
  190. redo: []
  191. },
  192. size: 5,
  193. color: {
  194. stroke: '#1500A7',
  195. fill: '#ff0000'
  196. },
  197. tool: undefined
  198. },
  199. modes: {},
  200. switchMode: switchMode,
  201. undoCanvas: undoCanvas,
  202. redoCanvas: redoCanvas,
  203. displayUndoRedoButtons: displayUndoRedoButtons,
  204. saveCanvas: saveCanvas,
  205. clearCanvas: clearCanvas,
  206. paletteRemoveActiveClass: paletteRemoveActiveClass,
  207. addActiveClassToElement: addActiveClassToElement,
  208. handleCurrentFloatingElement: handleCurrentFloatingElement
  209. };
  210. return PaintApp;
  211. });