Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crop preset review #84

Merged
merged 11 commits into from
Oct 31, 2018
Prev Previous commit
Next Next commit
refactor: change argument name
  • Loading branch information
jinwoo-kim-nhn committed Oct 31, 2018
commit 8478bf76f5008f7a423d51e031d45529585cb572
18 changes: 9 additions & 9 deletions src/js/component/cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,48 +286,48 @@ class Cropper extends Component {

/**
* Set a cropzone square
* @param {number} [scale] - preset ratio
* @param {number} [presetRatio] - preset ratio
*/
setCropzoneRect(scale) {
setCropzoneRect(presetRatio) {
const canvas = this.getCanvas();
const cropzone = this._cropzone;

canvas.deactivateAll();
canvas.selection = false;
cropzone.remove();

cropzone.set(scale ? this._getPresetCropSizePosition(scale) : DEFAULT_OPTION);
cropzone.set(presetRatio ? this._getPresetCropSizePosition(presetRatio) : DEFAULT_OPTION);

canvas.add(cropzone);
canvas.selection = true;

if (scale) {
if (presetRatio) {
canvas.setActiveObject(cropzone);
}
}

/**
* Set a cropzone square
* @param {number} scale - preset ratio
* @param {number} presetRatio - preset ratio
* @returns {{left: number, top: number, width: number, height: number}}
* @private
*/
_getPresetCropSizePosition(scale) {
_getPresetCropSizePosition(presetRatio) {
const canvas = this.getCanvas();
const originalWidth = canvas.getWidth();
const originalHeight = canvas.getHeight();

const standardSize = (originalWidth >= originalHeight) ? originalWidth : originalHeight;
const getScale = (value, orignalValue) => (value > orignalValue) ? orignalValue / value : 1;
jungeun-cho marked this conversation as resolved.
Show resolved Hide resolved

let width = standardSize * scale;
let width = standardSize * presetRatio;
let height = standardSize;

const scaleWidth = getScale(width, originalWidth);
[width, height] = snippet.map([width, height], sizeValue => (sizeValue * scaleWidth));
[width, height] = snippet.map([width, height], sizeValue => sizeValue * scaleWidth);

const scaleHeight = getScale(height, originalHeight);
[width, height] = snippet.map([width, height], sizeValue => (sizeValue * scaleHeight));
[width, height] = snippet.map([width, height], sizeValue => sizeValue * scaleHeight);

return {
top: (originalHeight - height) / 2,
Expand Down