From 95692e5da837bb82316b0260dcaa7b8a6eca4614 Mon Sep 17 00:00:00 2001 From: Ruslan Getmansky Date: Wed, 5 Jul 2017 10:23:27 +0300 Subject: [PATCH] P5-1492: Fix for image cropping moves to center after switching between inch and cm --- src/js/includes/Viewport.js | 15 ++++++++++----- src/js/includes/utils/calc-crop.js | 13 ++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/js/includes/Viewport.js b/src/js/includes/Viewport.js index fa76bae..110d4e9 100644 --- a/src/js/includes/Viewport.js +++ b/src/js/includes/Viewport.js @@ -131,15 +131,20 @@ function Viewport(canvas) { * @return {Viewport} */ this.set = function (width, height, units) { - _data = util.calcCrop(canvas.getImage(), width, height); - _data.dim.units = units; + var oldData = _data; + + _data = util.calcCrop(canvas.getImage(), width, height, units); + + var keepCropping = (oldData && (Math.abs(oldData.dim.width / oldData.dim.height - width / height) <= 0.01)); + if (keepCropping) { + canvas.getImageData().setCropData(); + } + apply(); this.getGores().reset(); this.getRulers().reset(); - if (canvas.getImage().__cropped) { - - } else { + if (!canvas.getImage().__cropped && !keepCropping) { canvas.getApertures().setTransparent(true); canvas.getImageData().removeCropData(); diff --git a/src/js/includes/utils/calc-crop.js b/src/js/includes/utils/calc-crop.js index a34a9f0..5facce8 100644 --- a/src/js/includes/utils/calc-crop.js +++ b/src/js/includes/utils/calc-crop.js @@ -1,8 +1,10 @@ 'use strict'; /** - * @param {Integer} width - * @param {Integer} height + * @param {fabric.Image} obj + * @param {int} width + * @param {int} height + * @param {string} units * @return {Object} * axis: STRING, * dim: OBJECT( width: NUMBER, height: NUMBER), @@ -10,7 +12,7 @@ * height: NUMBER, * ppmm: NUMBER */ -function calcCrop(obj, width, height) { +function calcCrop(obj, width, height, units) { var cropRatio = width / height; var objRatio = obj.width / obj.height; @@ -19,7 +21,8 @@ function calcCrop(obj, width, height) { axis: objRatio > cropRatio ? 'x' : 'y', dim: { width: Number(width), - height: Number(height) + height: Number(height), + units: units }, width: obj.width * obj.scaleX, height: obj.height * obj.scaleY, @@ -33,7 +36,7 @@ function calcCrop(obj, width, height) { returnValue.height /= cropRatio / objRatio; } - // Pixels per milimeter + // Pixels per millimeter returnValue.ppmm = returnValue.width / ((returnValue.dim.width * obj.scaleX) * 10); return returnValue;