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.

199 lines
8.6 KiB

  1. /* Stamp mode will paste a stamp onto the canvas */
  2. define([], function() {
  3. function initGui() {
  4. PaintApp.elements.stampsButton = document.getElementById('stamps-button');
  5. PaintApp.paletteModesButtons.push(PaintApp.elements.stampsButton);
  6. PaintApp.elements.stampsButton.addEventListener('click', function() {
  7. PaintApp.paletteRemoveActiveClass();
  8. PaintApp.addActiveClassToElement(PaintApp.elements.stampsButton);
  9. PaintApp.switchMode('Stamp');
  10. });
  11. /* We handle changes from the stamp palette */
  12. function onStampChange(event) {
  13. PaintApp.data.stamp = event.detail.stamp;
  14. PaintApp.data.stampBase = event.detail.stampBase;
  15. if (event.detail.proportionnal && event.detail.proportionnal === 'true') {
  16. PaintApp.data.stampProportionnal = true;
  17. } else {
  18. PaintApp.data.stampProportionnal = false;
  19. }
  20. }
  21. var stampPalette = new PaintApp.palettes.stampPalette.StampPalette(PaintApp.elements.stampsButton, undefined);
  22. stampPalette.addEventListener('stampChange', onStampChange);
  23. var stampInvoker = stampPalette.getPalette().querySelector('.palette-invoker');
  24. stampPalette.setStamp(0);
  25. }
  26. //Handle the coloring of a SVG (specific header required)
  27. function changeColors(iconData, fillColor, strokeColor) {
  28. var fillColorRegex = /<!ENTITY fill_color "(.*?)">/g;
  29. var strokeColorRegex = /<!ENTITY stroke_color "(.*?)">/g;
  30. iconData = iconData.replace(fillColorRegex, '<!ENTITY fill_color "' + fillColor + '">');
  31. iconData = iconData.replace(strokeColorRegex, '<!ENTITY stroke_color "' + strokeColor + '">');
  32. return iconData;
  33. }
  34. var Stamp = {
  35. initGui: initGui,
  36. changeColors: changeColors,
  37. defaultSize: 80,
  38. onMouseDown: function(event) {
  39. return function() {
  40. PaintApp.modes.Stamp.releasedFinger = false;
  41. PaintApp.handleCurrentFloatingElement();
  42. var width = PaintApp.modes.Stamp.defaultSize;
  43. var height = PaintApp.modes.Stamp.defaultSize;
  44. var left = event.point.x - width / 2 + 'px';
  45. var top = event.point.y + 55 - height / 2 + 'px';
  46. /* Calculation of the svg url */
  47. var url = window.location.href.split('/');
  48. url.pop();
  49. url = url.join('/') + '/' + PaintApp.data.stamp;
  50. var ctx = PaintApp.elements.canvas.getContext('2d');
  51. var p = event.point;
  52. /* Http request to load the stamp */
  53. var request = new XMLHttpRequest();
  54. request.open('GET', url, true);
  55. request.onload = function(e) {
  56. if (request.status === 200 || request.status === 0) {
  57. var imgSRC = request.responseText;
  58. /* We change the color of the stamp to match ours */
  59. imgSRC = changeColors(imgSRC, PaintApp.data.color.fill, PaintApp.data.color.stroke);
  60. /* We add a floating element image with the stamp */
  61. var element = document.createElement('img');
  62. element.style.backgroundRepeat = 'no-repeat';
  63. element.style.backgroundSize = 'contain';
  64. element.src = 'data:image/svg+xml;base64,' + btoa(imgSRC);
  65. element.style.width = width + 'px';
  66. element.style.height = height + 'px';
  67. element.style.position = 'absolute';
  68. element.style.left = left;
  69. element.style.padding = '0px';
  70. element.style.border = '5px dotted #500';
  71. element.style.resize = 'both';
  72. element.style.top = top;
  73. document.body.appendChild(element);
  74. /* We store the floating element */
  75. PaintApp.data.currentElement = {
  76. type: 'stamp',
  77. element: element,
  78. proportionnal: PaintApp.data.stampProportionnal,
  79. startPoint: {
  80. x: parseInt(element.style.left) + element.getBoundingClientRect().width / 2,
  81. y: parseInt(element.style.top) + element.getBoundingClientRect().height / 2
  82. }
  83. };
  84. /* This will prevent bugs for mobile platforms if the user just do a single tap we won't get onMouseDrag and onMouseUp correctly*/
  85. if (PaintApp.modes.Stamp.releasedFinger) {
  86. var ctx = PaintApp.elements.canvas.getContext('2d');
  87. /* We draw the stamp to the canvas where the user clicked */
  88. ctx.drawImage(PaintApp.data.currentElement.element, 5 + PaintApp.data.currentElement.element.getBoundingClientRect().left, PaintApp.data.currentElement.element.getBoundingClientRect().top - 55 + 5);
  89. /* If the activity is shared we send the element to everyone */
  90. if (PaintApp.data.isShared) {
  91. var drawImage = {
  92. stampBase: PaintApp.data.stampBase,
  93. color: {
  94. fill: PaintApp.data.color.fill,
  95. stroke: PaintApp.data.color.stroke
  96. },
  97. left: PaintApp.data.currentElement.element.getBoundingClientRect().left,
  98. top: PaintApp.data.currentElement.element.getBoundingClientRect().top - 55 + 5,
  99. width: PaintApp.data.currentElement.element.getBoundingClientRect().width,
  100. height: PaintApp.data.currentElement.element.getBoundingClientRect().height
  101. };
  102. PaintApp.collaboration.sendMessage({
  103. action: 'drawStamp',
  104. data: drawImage
  105. });
  106. }
  107. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  108. PaintApp.data.currentElement = undefined;
  109. PaintApp.saveCanvas();
  110. }
  111. }
  112. };
  113. request.send(null);
  114. }();
  115. },
  116. onMouseDrag: function(event) {
  117. if (!PaintApp.data.currentElement) {
  118. return;
  119. }
  120. /* We will resize the floating element with the mouse drag */
  121. var distanceX = Math.abs(event.point.x - PaintApp.data.currentElement.startPoint.x);
  122. var distanceY = Math.abs(event.point.y - PaintApp.data.currentElement.startPoint.y + 55);
  123. if (distanceX > distanceY) {
  124. distance = distanceX;
  125. } else {
  126. distance = distanceY;
  127. }
  128. /* The resize can be kept proportionnal or not (this is configured inside the stamp palette) */
  129. if (PaintApp.data.stampProportionnal) {
  130. PaintApp.data.currentElement.element.style.width = PaintApp.modes.Stamp.defaultSize + distance + 'px';
  131. PaintApp.data.currentElement.element.style.height = PaintApp.modes.Stamp.defaultSize + distance + 'px';
  132. } else {
  133. PaintApp.data.currentElement.element.style.width = PaintApp.modes.Stamp.defaultSize + distanceX + 'px';
  134. PaintApp.data.currentElement.element.style.height = PaintApp.modes.Stamp.defaultSize + distanceY + 'px';
  135. }
  136. PaintApp.data.currentElement.element.style.left = PaintApp.data.currentElement.startPoint.x - PaintApp.data.currentElement.element.getBoundingClientRect().width / 2 + 'px';
  137. PaintApp.data.currentElement.element.style.top = PaintApp.data.currentElement.startPoint.y - PaintApp.data.currentElement.element.getBoundingClientRect().height / 2 + 'px';
  138. },
  139. onMouseUp: function(event) {
  140. PaintApp.modes.Stamp.releasedFinger = true;
  141. if (!PaintApp.data.currentElement) {
  142. return;
  143. }
  144. var ctx = PaintApp.elements.canvas.getContext('2d');
  145. /* We draw the stamp to the canvas where the user clicked */
  146. ctx.drawImage(PaintApp.data.currentElement.element, PaintApp.data.currentElement.element.getBoundingClientRect().left, PaintApp.data.currentElement.element.getBoundingClientRect().top - 55, PaintApp.data.currentElement.element.getBoundingClientRect().width, PaintApp.data.currentElement.element.getBoundingClientRect().height);
  147. /* If the activity is shared we send the element to everyone */
  148. if (PaintApp.data.isShared) {
  149. var drawImage = {
  150. stampBase: PaintApp.data.stampBase,
  151. color: {
  152. fill: PaintApp.data.color.fill,
  153. stroke: PaintApp.data.color.stroke
  154. },
  155. left: PaintApp.data.currentElement.element.getBoundingClientRect().left,
  156. top: PaintApp.data.currentElement.element.getBoundingClientRect().top - 55,
  157. width: PaintApp.data.currentElement.element.getBoundingClientRect().width,
  158. height: PaintApp.data.currentElement.element.getBoundingClientRect().height
  159. };
  160. PaintApp.collaboration.sendMessage({
  161. action: 'drawStamp',
  162. data: drawImage
  163. });
  164. }
  165. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  166. PaintApp.data.currentElement = undefined;
  167. PaintApp.saveCanvas();
  168. }
  169. };
  170. return Stamp;
  171. });