Source: CropComponent.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import {core} from 'metal';
  15. import Component from 'metal-component';
  16. import Soy from 'metal-soy';
  17. import componentTemplates from './CropComponent.soy';
  18. import './CropControls.soy';
  19. /**
  20. * Creates a Crop component.
  21. */
  22. class CropComponent extends Component {
  23. /**
  24. * Applies the brightness filter to generate a final version of the image.
  25. *
  26. * @param {Object} imageData The image representations.
  27. * @return {Promise} A promise that resolves when the webworker
  28. * finishes processing the image for preview.
  29. */
  30. process(imageData) {
  31. const imageCanvas = this.getImageEditorCanvas();
  32. var horizontalRatio = imageData.width / imageCanvas.offsetWidth;
  33. var verticalRatio = imageData.height / imageCanvas.offsetHeight;
  34. const cropHandles = this.components[this.ref + 'CropHandles'];
  35. const selection = {
  36. height: cropHandles.element.offsetHeight,
  37. left: cropHandles.element.offsetLeft - imageCanvas.offsetLeft,
  38. top: cropHandles.element.offsetTop - imageCanvas.offsetTop,
  39. width: cropHandles.element.offsetWidth,
  40. };
  41. const rawCanvas = document.createElement('canvas');
  42. rawCanvas.width = imageData.width;
  43. rawCanvas.height = imageData.height;
  44. rawCanvas.getContext('2d').putImageData(imageData, 0, 0);
  45. const canvas = document.createElement('canvas');
  46. const normalizedLeft = selection.left * horizontalRatio;
  47. const normalizedWidth = selection.width * horizontalRatio;
  48. const normalizedTop = selection.top * verticalRatio;
  49. const normalizedHeight = selection.height * verticalRatio;
  50. canvas.width = normalizedWidth;
  51. canvas.height = normalizedHeight;
  52. const context = canvas.getContext('2d');
  53. context.drawImage(
  54. rawCanvas,
  55. normalizedLeft,
  56. normalizedTop,
  57. normalizedWidth,
  58. normalizedHeight,
  59. 0,
  60. 0,
  61. normalizedWidth,
  62. normalizedHeight
  63. );
  64. cropHandles.dispose();
  65. return Promise.resolve(
  66. context.getImageData(0, 0, canvas.width, canvas.height)
  67. );
  68. }
  69. }
  70. /**
  71. * State definition.
  72. *
  73. * @static
  74. * @type {!Object}
  75. */
  76. CropComponent.STATE = {
  77. /**
  78. * Injected helper that retrieves the editor canvas.
  79. *
  80. * @type {Function}
  81. */
  82. getImageEditorCanvas: {
  83. validator: core.isFunction,
  84. },
  85. };
  86. Soy.register(CropComponent, componentTemplates);
  87. export default CropComponent;