Chromium Code Reviews| Index: ui/file_manager/gallery/js/image_editor/viewport.js |
| diff --git a/ui/file_manager/gallery/js/image_editor/viewport.js b/ui/file_manager/gallery/js/image_editor/viewport.js |
| index 1250c179d614bde6c6e555e5eb43a301377fa6f7..782c12ab2daa00edbe053c8ddd00b2d703785a2d 100644 |
| --- a/ui/file_manager/gallery/js/image_editor/viewport.js |
| +++ b/ui/file_manager/gallery/js/image_editor/viewport.js |
| @@ -200,10 +200,13 @@ Viewport.prototype.getRotation = function() { |
| * displayed image size. |
| * @private |
| */ |
| -Viewport.prototype.getFittingScaleForImageSize_ = function(width, height) { |
| - var scaleX = this.screenBounds_.width / width; |
| - var scaleY = this.screenBounds_.height / height; |
| - return Math.min(scaleX, scaleY, 1); |
| +Viewport.prototype.getFittingScaleForImageSize_ = function( |
| + width, height, maxWidth, maxHeight) { |
| + return Math.min( |
| + maxWidth / width, |
| + maxHeight / height, |
| + this.screenBounds_.width / width, |
| + this.screenBounds_.height / height); |
| }; |
| /** |
| @@ -406,6 +409,7 @@ Viewport.prototype.resetView = function() { |
| Viewport.prototype.update_ = function() { |
| // Update scale. |
| this.scale_ = this.getFittingScaleForImageSize_( |
| + this.imageBounds_.width, this.imageBounds_.height, |
| this.imageBounds_.width, this.imageBounds_.height); |
| // Limit offset values. |
| @@ -416,6 +420,7 @@ Viewport.prototype.update_ = function() { |
| zoomedHeight = ~~(this.imageBounds_.height * this.scale_ * this.zoom_); |
| } else { |
| var scale = this.getFittingScaleForImageSize_( |
| + this.imageBounds_.height, this.imageBounds_.width, |
| this.imageBounds_.height, this.imageBounds_.width); |
| zoomedWidht = ~~(this.imageBounds_.height * scale * this.zoom_); |
| zoomedHeight = ~~(this.imageBounds_.width * scale * this.zoom_); |
| @@ -471,32 +476,55 @@ Viewport.prototype.clone = function() { |
| return viewport; |
| }; |
| -/** |
| - * Obtains CSS transformation for the screen image. |
| - * @return {string} Transformation description. |
| - */ |
| -Viewport.prototype.getTransformation = function() { |
| - var rotationScaleAdjustment; |
| - if (this.rotation_ % 2) { |
| - rotationScaleAdjustment = this.getFittingScaleForImageSize_( |
| - this.imageBounds_.height, this.imageBounds_.width) / this.scale_; |
| - } else { |
| - rotationScaleAdjustment = 1; |
| - } |
| +Viewport.prototype.getScreenRectTransformation = function( |
|
mtomasz
2015/05/14 00:32:32
nit: jsdoc missing
hirono
2015/05/14 01:57:21
Done.
|
| + width, height, screenRect) { |
| + var dx = screenRect.left + (screenRect.width - width) / 2; |
| + var dy = screenRect.top + (screenRect.height - height) / 2; |
| + |
| return [ |
|
mtomasz
2015/05/14 00:32:32
optional: I think simply concatenating is easier t
hirono
2015/05/14 01:57:21
I tries to create stringFormat helper function. Do
|
| - 'translate(' + this.offsetX_ + 'px, ' + this.offsetY_ + 'px) ', |
| - 'rotate(' + (this.rotation_ * 90) + 'deg)', |
| - 'scale(' + (this.zoom_ * rotationScaleAdjustment) + ')' |
| - ].join(' '); |
| + 'translate(', dx, 'px,', dy, 'px) ', |
| + 'scale(', screenRect.width / width, ',', screenRect.height / height, ') ' |
| + ].join(''); |
| +}; |
| + |
| +Viewport.prototype.getCroppingTransformation = function( |
|
mtomasz
2015/05/14 00:32:32
nit: jsdoc missing
hirono
2015/05/14 01:57:21
Done.
|
| + width, |
| + height, |
| + wholeWidthMax, |
| + wholeHeightMax, |
| + cropRect) { |
| + var fittingScale = this.getFittingScaleForImageSize_( |
| + wholeWidthMax, wholeHeightMax, wholeWidthMax, wholeHeightMax); |
| + var wholeWidth = wholeWidthMax * fittingScale; |
| + var wholeHeight = wholeHeightMax * fittingScale; |
| + var wholeLeft = (this.screenBounds_.width - wholeWidth) / 2; |
| + var wholeTop = (this.screenBounds_.height - wholeHeight) / 2; |
| + return this.getScreenRectTransformation( |
| + width, |
| + height, |
| + new ImageRect( |
| + wholeLeft + cropRect.left * fittingScale, |
| + wholeTop + cropRect.top * fittingScale, |
| + cropRect.width * fittingScale, |
| + cropRect.height * fittingScale)); |
| }; |
| /** |
| - * Obtains shift CSS transformation for the screen image. |
| - * @param {number} dx Amount of shift. |
| + * Obtains CSS transformation for the screen image. |
| + * @param {number} width Width of image. |
| + * @param {number} height Height of image. |
| + * @param {number=} opt_dx Amount of horizontal shift. |
| * @return {string} Transformation description. |
| */ |
| -Viewport.prototype.getShiftTransformation = function(dx) { |
| - return 'translateX(' + dx + 'px) ' + this.getTransformation(); |
| +Viewport.prototype.getTransformation = function(width, height, opt_dx) { |
| + return this.getTransformationInternal_( |
| + width, |
| + height, |
| + this.rotation_, |
| + this.zoom_, |
| + this.offsetX_ + (opt_dx || 0), |
| + this.offsetY_, |
| + null); |
| }; |
| /** |
| @@ -504,72 +532,46 @@ Viewport.prototype.getShiftTransformation = function(dx) { |
| * image. The new rotated image that the transformation is applied to looks the |
| * same with original image. |
| * |
| - * @param {boolean} orientation Orientation of the rotation from the original |
| + * @param {number} rotation90 Orientation of the rotation from the original |
|
mtomasz
2015/05/14 00:32:32
nit: Why the name is rotation90? It's hard for me
hirono
2015/05/14 01:57:21
It was number of times for 90 degrees. e.g. 180 de
|
| * image to the rotated image. True is for clockwise and false is for |
|
mtomasz
2015/05/14 00:32:32
nit: Please update the comment, it's not bool anym
hirono
2015/05/14 01:57:21
Done.
|
| * counterclockwise. |
| * @return {string} Transformation description. |
| */ |
| -Viewport.prototype.getInverseTransformForRotatedImage = function(orientation) { |
| - var previousImageWidth = this.imageBounds_.height; |
| - var previousImageHeight = this.imageBounds_.width; |
| - var oldScale = this.getFittingScaleForImageSize_( |
| - previousImageWidth, previousImageHeight); |
| - var scaleRatio = oldScale / this.scale_; |
| - var degree = orientation ? '-90deg' : '90deg'; |
| - return [ |
| - 'scale(' + scaleRatio + ')', |
| - 'rotate(' + degree + ')', |
| - this.getTransformation() |
| - ].join(' '); |
| +Viewport.prototype.getRotatingTransformation = function( |
| + width, height, rotation90) { |
| + return this.getTransformationInternal_( |
| + width, height, rotation90, 1, 0, 0, null); |
| }; |
| /** |
| - * Obtains CSS transformation that makes the cropped image fit the original |
| - * image. The new cropped image that the transformation is applied to fits to |
| - * the cropped rectangle in the original image. |
| - * |
| - * @param {number} imageWidth Width of the original image. |
| - * @param {number} imageHeight Height of the original image. |
| - * @param {!ImageRect} imageCropRect Crop rectangle in the image's coordinate |
| - * system. |
| - * @return {string} Transformation description. |
| + * @private |
|
mtomasz
2015/05/14 00:32:32
nit: @jsdoc missing
hirono
2015/05/14 01:57:21
Done.
|
| */ |
| -Viewport.prototype.getInverseTransformForCroppedImage = |
| - function(imageWidth, imageHeight, imageCropRect) { |
| - var wholeScale = this.getFittingScaleForImageSize_( |
| - imageWidth, imageHeight); |
| - var croppedScale = this.getFittingScaleForImageSize_( |
| - imageCropRect.width, imageCropRect.height); |
| - var dx = |
| - (imageCropRect.left + imageCropRect.width / 2 - imageWidth / 2) * |
| - wholeScale; |
| - var dy = |
| - (imageCropRect.top + imageCropRect.height / 2 - imageHeight / 2) * |
| - wholeScale; |
| - return [ |
| - 'translate(' + dx + 'px,' + dy + 'px)', |
| - 'scale(' + wholeScale / croppedScale + ')', |
| - this.getTransformation() |
| - ].join(' '); |
| -}; |
| +Viewport.prototype.getTransformationInternal_ = function( |
| + width, |
| + height, |
| + rotation90, |
| + zoom, |
| + offsetX, |
| + offsetY, |
| + cropRect) { |
| + var rotatedWidth = rotation90 % 2 ? height : width; |
| + var rotatedHeight = rotation90 % 2 ? width : height; |
| + var rotatedMaxWidth = rotation90 % 2 ? |
| + this.imageBounds_.height : this.imageBounds_.width; |
| + var rotatedMaxHeight = rotation90 % 2 ? |
| + this.imageBounds_.width : this.imageBounds_.height; |
| + |
| + // Scale. |
| + var fittingScale = this.getFittingScaleForImageSize_( |
| + rotatedWidth, rotatedHeight, rotatedMaxWidth, rotatedMaxHeight); |
| + |
| + // Offset for centering. |
| + var dx = (this.screenBounds_.width - width) / 2; |
| + var dy = (this.screenBounds_.height - height) / 2; |
| -/** |
| - * Obtains CSS transformation that makes the image fit to the screen rectangle. |
| - * |
| - * @param {!ImageRect} screenRect Screen rectangle. |
| - * @return {string} Transformation description. |
| - */ |
| -Viewport.prototype.getScreenRectTransformForImage = function(screenRect) { |
| - var imageBounds = this.getImageElementBoundsOnScreen(); |
| - var scaleX = screenRect.width / imageBounds.width; |
| - var scaleY = screenRect.height / imageBounds.height; |
| - var screenWidth = this.screenBounds_.width; |
| - var screenHeight = this.screenBounds_.height; |
| - var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; |
| - var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; |
| return [ |
| - 'translate(' + dx + 'px,' + dy + 'px)', |
| - 'scale(' + scaleX + ',' + scaleY + ')', |
| - this.getTransformation() |
| - ].join(' '); |
| + 'translate(', dx + offsetX, 'px,', dy + offsetY, 'px) ', |
| + 'scale(', fittingScale * zoom, ') ', |
| + 'rotate(', rotation90 * 90, 'deg) ' |
| + ].join(''); |
| }; |