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.

141 lines
5.0 KiB

  1. /* Drawing palette to display a drawing onto the canvas */
  2. define([
  3. 'sugar-web/graphics/palette',
  4. 'mustache'
  5. ], function(palette, mustache) {
  6. var drawingspalette = {};
  7. function onDrawingSelect(event) {
  8. /* Calculation of the drawing url */
  9. var url = window.location.href.split('/');
  10. url.pop();
  11. url = url.join('/') + '/' + event.detail.drawings;
  12. var ctx = PaintApp.elements.canvas.getContext('2d');
  13. /* Http request to load the drawing */
  14. var request = new XMLHttpRequest();
  15. request.open('GET', url, true);
  16. request.onload = function(e) {
  17. if (request.status === 200 || request.status === 0) {
  18. var imgSRC = request.responseText;
  19. //We load the drawing inside an image element
  20. var element = document.createElement('img');
  21. element.src = 'data:image/svg+xml;base64,' + btoa(imgSRC);
  22. element.onload = function() {
  23. //We draw the drawing to the canvas
  24. var ctx = PaintApp.elements.canvas.getContext('2d');
  25. var imgWidth = element.width;
  26. var imgHeight = element.height;
  27. var maxWidth = PaintApp.elements.canvas.getBoundingClientRect().width;
  28. var maxHeight = PaintApp.elements.canvas.getBoundingClientRect().height;
  29. var ratio = Math.min(maxWidth / imgWidth, maxHeight / imgHeight);
  30. var newWidth = ratio * imgWidth;
  31. var newHeight = ratio * imgHeight;
  32. ctx.clearRect(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height);
  33. ctx.drawImage(element, 0, 0, newWidth, newHeight);
  34. PaintApp.saveCanvas();
  35. /* If the activity is shared we send the element to everyone */
  36. if (PaintApp.data.isShared) {
  37. try {
  38. PaintApp.collaboration.sendMessage({
  39. action: 'toDataURL',
  40. data: {
  41. width: PaintApp.elements.canvas.width / window.devicePixelRatio,
  42. height: PaintApp.elements.canvas.height / window.devicePixelRatio,
  43. src: PaintApp.collaboration.compress(PaintApp.elements.canvas.toDataURL())
  44. }
  45. });
  46. } catch (e) {}
  47. }
  48. };
  49. }
  50. };
  51. request.send(null);
  52. }
  53. function initGui() {
  54. var drawingsButton = document.getElementById('drawings-button');
  55. var drawingsPalette = new PaintApp.palettes.drawingsPalette.DrawingsPalette(drawingsButton, undefined);
  56. drawingsPalette.addEventListener('drawingsChange', onDrawingSelect);
  57. }
  58. drawingspalette.initGui = initGui;
  59. /* We setup the palette with drawings */
  60. drawingspalette.DrawingsPalette = function(invoker, primaryText) {
  61. palette.Palette.call(this, invoker, primaryText);
  62. this.drawingsChangeEvent = document.createEvent('CustomEvent');
  63. this.drawingsChangeEvent.initCustomEvent('drawingsChange', true, true, {
  64. 'drawings': 'icons/size-1.svg'
  65. });
  66. this.template = '<tbody>' + '{{#rows}}' + '<tr>' + '{{#.}}' + '<td>' + '<button value="{{drawings}}" style="height:55px; width:55px; background-size: 55px 55px; background-image: url({{ drawings }}); background-repeat: no-repeat; background-position: center; "></button>' + '</td>' + '{{/.}}' + '</tr>' + '{{/rows}}' + '</tbody>';
  67. var drawingssElem = document.createElement('table');
  68. drawingssElem.className = 'drawingss';
  69. var drawingssData = {
  70. rows: [
  71. [{
  72. drawings: 'drawings/woodpecker.svg'
  73. }, {
  74. drawings: 'drawings/tortoise.svg'
  75. }, {
  76. drawings: 'drawings/manatee.svg'
  77. }],
  78. [{
  79. drawings: 'drawings/dog.svg'
  80. }, {
  81. drawings: 'drawings/goldfinch.svg'
  82. }, {
  83. drawings: 'drawings/mammoth.svg'
  84. }]
  85. ]
  86. };
  87. drawingssElem.innerHTML = mustache.render(this.template, drawingssData);
  88. this.setContent([drawingssElem]);
  89. // Pop-down the palette when a item in the menu is clicked.
  90. this.buttons = drawingssElem.querySelectorAll('button');
  91. var that = this;
  92. function popDownOnButtonClick(event) {
  93. that.drawingsChangeEvent.detail.drawings = event.target.value;
  94. that.getPalette().dispatchEvent(that.drawingsChangeEvent);
  95. that.popDown();
  96. }
  97. for (var i = 0; i < this.buttons.length; i++) {
  98. this.buttons[i].addEventListener('click', popDownOnButtonClick);
  99. }
  100. };
  101. var setdrawings = function(index) {
  102. // Click the nth button
  103. var event = document.createEvent('MouseEvents');
  104. event.initEvent('click', true, true);
  105. this.buttons[index].dispatchEvent(event);
  106. };
  107. var addEventListener = function(type, listener, useCapture) {
  108. return this.getPalette().addEventListener(type, listener, useCapture);
  109. };
  110. drawingspalette.DrawingsPalette.prototype = Object.create(palette.Palette.prototype, {
  111. setdrawings: {
  112. value: setdrawings,
  113. enumerable: true,
  114. configurable: true,
  115. writable: true
  116. },
  117. addEventListener: {
  118. value: addEventListener,
  119. enumerable: true,
  120. configurable: true,
  121. writable: true
  122. }
  123. });
  124. return drawingspalette;
  125. });