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.

131 lines
6.1 KiB

  1. // Insert image button display
  2. define(['sugar-web/graphics/journalchooser','sugar-web/datastore'], function(chooser, datastore) {
  3. function initGui() {
  4. var insertImageButton = document.getElementById('insertimage-button');
  5. PaintApp.elements.insertImageButton = insertImageButton;
  6. insertImageButton.addEventListener('click', onInsertImageClick);
  7. }
  8. var printImage = false;
  9. var resizeImage = false;
  10. // onClick display dialog with journal content
  11. function onInsertImageClick() {
  12. // Display journal dialog popup
  13. chooser.show(function(entry) {
  14. // No selection
  15. if (!entry) {
  16. return;
  17. }
  18. // Get object content
  19. var dataentry = new datastore.DatastoreObject(entry.objectId);
  20. dataentry.loadAsText(function(err, metadata, text) {
  21. //We load the drawing inside an image element
  22. var element = document.createElement('img');
  23. element.src = text;
  24. element.onload = function() {
  25. printImage = true;
  26. resizeImage = false;
  27. //We draw the drawing to the canvas
  28. var ctx = PaintApp.elements.canvas.getContext('2d');
  29. var imagedata = ctx.getImageData(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height);
  30. var xds= true;
  31. var width;
  32. var height;
  33. var x;
  34. var y;
  35. var imageWoRectangle;
  36. var touchScreen = ("ontouchstart" in document.documentElement);
  37. ctx.putImageData(imagedata, 0, 0);
  38. x = 65;
  39. y = 130;
  40. width = element.width;
  41. height = element.height;
  42. ctx.drawImage(element, x - 60, y - 125, width + 60, height + 70);
  43. imageWoRectangle = ctx.getImageData(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height);
  44. ctx.stroke();
  45. PaintApp.saveCanvas();
  46. ctx.beginPath();
  47. ctx.setLineDash([1, 10]);
  48. ctx.strokeStyle = '#101010';
  49. ctx.rect(x - 65, y - 130, width + 70, height + 80);
  50. ctx.stroke();
  51. //Calculate the size and print the image
  52. var imageMousemove = function(event) {
  53. if (printImage || resizeImage) {
  54. if(xds) ctx.putImageData(imagedata, 0, 0);
  55. if (touchScreen) event = event.touches[0];
  56. if (printImage) {
  57. x = event.clientX - (width/2);
  58. y = event.clientY - (height/2);
  59. } else {
  60. width = event.clientX - x;
  61. height = event.clientY - y
  62. }
  63. ctx.drawImage(element, x - 60, y - 125, width + 60, height + 70);
  64. imageWoRectangle = ctx.getImageData(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height);
  65. ctx.beginPath();
  66. ctx.setLineDash([1, 10]);
  67. ctx.strokeStyle = '#101010';
  68. ctx.rect(x - 65, y - 130, width + 70, height + 80);
  69. ctx.stroke();
  70. }
  71. }
  72. var imageMouseup = function(event) {
  73. if (printImage) {
  74. printImage = false;
  75. resizeImage = true;
  76. } else if (resizeImage) {
  77. if (touchScreen) event = event.touches[0];
  78. ctx.putImageData(imageWoRectangle, 0, 0);
  79. ctx.setLineDash([1]);
  80. imagedata = ctx.getImageData(0, 0, PaintApp.elements.canvas.width, PaintApp.elements.canvas.height);
  81. PaintApp.saveCanvas();
  82. ctx=0;
  83. printImage = false;
  84. resizeImage = false;
  85. if (touchScreen) {
  86. removeEventListener("touchmove", imageMousemove);
  87. removeEventListener("touchend", imageMouseup);
  88. } else {
  89. removeEventListener("mousemove", imageMousemove);
  90. removeEventListener("mouseup", imageMouseup);
  91. }
  92. }
  93. }
  94. if (touchScreen) {
  95. addEventListener("touchmove", imageMousemove, false);
  96. addEventListener("touchend", imageMouseup, false);
  97. } else {
  98. addEventListener("mousemove", imageMousemove);
  99. addEventListener("mouseup", imageMouseup);
  100. }
  101. /* If the activity is shared we send the element to everyone */
  102. if (PaintApp.data.isShared) {
  103. try {
  104. PaintApp.collaboration.sendMessage({
  105. action: 'toDataURL',
  106. data: {
  107. width: PaintApp.elements.canvas.width / window.devicePixelRatio,
  108. height: PaintApp.elements.canvas.height / window.devicePixelRatio,
  109. src: PaintApp.collaboration.compress(PaintApp.elements.canvas.toDataURL())
  110. }
  111. });
  112. } catch (e) {}
  113. }
  114. }
  115. });
  116. }, {mimetype: 'image/png'}, {mimetype: 'image/jpeg'});
  117. }
  118. var insertImageButton = {
  119. initGui: initGui
  120. };
  121. return insertImageButton;
  122. });