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.

115 lines
4.2 KiB

  1. /* Copy mode will also to copy an area, this mode is intended to work with the paste made */
  2. define([], function() {
  3. function initGui() {
  4. PaintApp.elements.copyButton = document.getElementById('copy-button');
  5. PaintApp.paletteModesButtons.push(PaintApp.elements.copyButton);
  6. PaintApp.elements.copyButton.addEventListener('click', function() {
  7. PaintApp.paletteRemoveActiveClass();
  8. PaintApp.addActiveClassToElement(PaintApp.elements.copyButton);
  9. PaintApp.switchMode('Copy');
  10. });
  11. }
  12. var Copy = {
  13. initGui: initGui,
  14. begin: undefined,
  15. end: undefined,
  16. onMouseDown: function(event) {
  17. if (PaintApp.data.currentElement) {
  18. PaintApp.data.currentElement.element.parentElement.removeChild(PaintApp.data.currentElement.element);
  19. PaintApp.data.currentElement = undefined;
  20. }
  21. begin = event.point;
  22. var left = event.point.x + 'px';
  23. var top = event.point.y + 55 + 'px';
  24. /* We are creating a floating div where the user clicked */
  25. var element = document.createElement('div');
  26. element.style.position = 'absolute';
  27. element.style.padding = '0px';
  28. element.style.width = '1px';
  29. element.style.height = '1px';
  30. element.style.opacity = '0.5';
  31. element.style.pointerEvents = 'none';
  32. element.style.borderRadius = '0px';
  33. element.style.border = '5px dotted #500';
  34. element.style.left = left;
  35. element.style.top = top;
  36. element.style.verticalAlign = 'bottom';
  37. document.body.appendChild(element);
  38. /* Left and top relies on getBoundingClientRect so we need to set values after the DOM append */
  39. element.style.left = parseInt(left) - element.getBoundingClientRect().width / 2 + 'px';
  40. element.style.top = parseInt(top) - element.getBoundingClientRect().height / 2 + 'px';
  41. /* We store the currentElement */
  42. PaintApp.data.currentElement = {
  43. type: 'copy/paste',
  44. element: element,
  45. startPoint: {
  46. x: parseInt(element.style.left) + element.getBoundingClientRect().width / 2,
  47. y: parseInt(element.style.top) + element.getBoundingClientRect().height / 2
  48. }
  49. };
  50. },
  51. onMouseDrag: function(event) {
  52. /* We resize the floating element with the user drag */
  53. var end = event.point;
  54. if (begin.x <= end.x) {
  55. PaintApp.data.currentElement.element.style.width = end.x - begin.x + 'px';
  56. } else {
  57. PaintApp.data.currentElement.element.style.width = 0 + 'px';
  58. }
  59. if (begin.y <= end.y) {
  60. PaintApp.data.currentElement.element.style.height = Math.abs(begin.y - end.y) + 'px';
  61. } else {
  62. PaintApp.data.currentElement.element.style.height = 0 + 'px';
  63. }
  64. },
  65. onMouseUp: function(event) {
  66. /* When the user stops clicking we will do a calculation of the selected area */
  67. var end = event.point;
  68. end.y = end.y + 55;
  69. var width = parseInt(PaintApp.data.currentElement.element.style.width);
  70. var height = parseInt(PaintApp.data.currentElement.element.style.height);
  71. var canvas = PaintApp.elements.canvas;
  72. var ctx = PaintApp.elements.canvas.getContext('2d');
  73. if (begin.x < end.x && begin.y < end.y) {
  74. /* We save the whole canvas and then cut the selection corresponding to the floating selection */
  75. try {
  76. PaintApp.modes.Paste.data = canvas.toDataURL();
  77. } catch (e) {
  78. return;
  79. }
  80. var imgData = ctx.getImageData(begin.x * window.devicePixelRatio, begin.y * window.devicePixelRatio, width * window.devicePixelRatio, height * window.devicePixelRatio);
  81. canvas = document.createElement('canvas');
  82. canvas.width = width * window.devicePixelRatio;
  83. canvas.height = height * window.devicePixelRatio;
  84. canvas.getContext('2d').putImageData(imgData, 0, 0);
  85. try {
  86. imgData = canvas.toDataURL();
  87. } catch (e) {
  88. return;
  89. }
  90. /* We fill the paste mode with the image, width and height and then switch to paste mode */
  91. PaintApp.modes.Paste.dataImage = {
  92. width: width / window.devicePixelRatio,
  93. height: height / window.devicePixelRatio,
  94. data: imgData
  95. };
  96. PaintApp.elements.pasteButton.click();
  97. }
  98. }
  99. };
  100. return Copy;
  101. });