OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Crop mode. | 8 * Crop mode. |
9 * | 9 * |
10 * @extends {ImageEditor.Mode} | 10 * @extends {ImageEditor.Mode} |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 this.middleBox_.appendChild(this.cropFrame_); | 47 this.middleBox_.appendChild(this.cropFrame_); |
48 | 48 |
49 this.shadowRight_ = doc.createElement('div'); | 49 this.shadowRight_ = doc.createElement('div'); |
50 this.shadowRight_.className = 'shadow'; | 50 this.shadowRight_.className = 'shadow'; |
51 this.middleBox_.appendChild(this.shadowRight_); | 51 this.middleBox_.appendChild(this.shadowRight_); |
52 | 52 |
53 this.shadowBottom_ = doc.createElement('div'); | 53 this.shadowBottom_ = doc.createElement('div'); |
54 this.shadowBottom_.className = 'shadow'; | 54 this.shadowBottom_.className = 'shadow'; |
55 this.domOverlay_.appendChild(this.shadowBottom_); | 55 this.domOverlay_.appendChild(this.shadowBottom_); |
56 | 56 |
| 57 this.toolBar_ = null; |
| 58 |
57 var cropFrame = this.cropFrame_; | 59 var cropFrame = this.cropFrame_; |
58 function addCropFrame(className) { | 60 function addCropFrame(className) { |
59 var div = doc.createElement('div'); | 61 var div = doc.createElement('div'); |
60 div.className = className; | 62 div.className = className; |
61 cropFrame.appendChild(div); | 63 cropFrame.appendChild(div); |
62 } | 64 } |
63 | 65 |
64 addCropFrame('left top corner'); | 66 addCropFrame('left top corner'); |
65 addCropFrame('top horizontal'); | 67 addCropFrame('top horizontal'); |
66 addCropFrame('right top corner'); | 68 addCropFrame('right top corner'); |
(...skipping 28 matching lines...) Expand all Loading... |
95 if (button.classList.contains('selected')) { | 97 if (button.classList.contains('selected')) { |
96 button.classList.remove('selected'); | 98 button.classList.remove('selected'); |
97 this.cropRect_.fixedAspectRatio = null; | 99 this.cropRect_.fixedAspectRatio = null; |
98 } else { | 100 } else { |
99 var selectedButtons = | 101 var selectedButtons = |
100 toolbar.element.querySelectorAll('button.selected'); | 102 toolbar.element.querySelectorAll('button.selected'); |
101 for (var i = 0; i < selectedButtons.length; i++) { | 103 for (var i = 0; i < selectedButtons.length; i++) { |
102 selectedButtons[i].classList.remove('selected'); | 104 selectedButtons[i].classList.remove('selected'); |
103 } | 105 } |
104 button.classList.add('selected'); | 106 button.classList.add('selected'); |
| 107 var clipRect = this.viewport_.screenToImageRect( |
| 108 this.viewport_.getImageBoundsOnScreenClipped()); |
105 this.cropRect_.fixedAspectRatio = aspect; | 109 this.cropRect_.fixedAspectRatio = aspect; |
| 110 this.cropRect_.forceAspectRatio(aspect, clipRect); |
| 111 this.markUpdated(); |
| 112 this.positionDOM(); |
| 113 this.toolbar_.element.classList.remove('dimmable'); |
| 114 this.toolbar_.element.removeAttribute('dimmed'); |
106 } | 115 } |
107 }.bind(this, aspects[name])); | 116 }.bind(this, aspects[name])); |
108 } | 117 } |
| 118 this.toolbar_ = toolbar; |
109 }; | 119 }; |
110 | 120 |
111 /** | 121 /** |
112 * Handles resizing of the window and updates the crop rectangle. | 122 * Handles resizing of the window and updates the crop rectangle. |
113 * @private | 123 * @private |
114 */ | 124 */ |
115 ImageEditor.Mode.Crop.prototype.onResized_ = function() { | 125 ImageEditor.Mode.Crop.prototype.onResized_ = function() { |
116 this.positionDOM(); | 126 this.positionDOM(); |
117 }; | 127 }; |
118 | 128 |
119 /** | 129 /** |
120 * Resets the mode. | 130 * Resets the mode. |
121 */ | 131 */ |
122 ImageEditor.Mode.Crop.prototype.reset = function() { | 132 ImageEditor.Mode.Crop.prototype.reset = function() { |
123 ImageEditor.Mode.prototype.reset.call(this); | 133 ImageEditor.Mode.prototype.reset.call(this); |
124 this.createDefaultCrop(); | 134 this.createDefaultCrop(); |
| 135 if (this.toolbar_) { |
| 136 this.toolbar_.element.classList.add('dimmable'); |
| 137 this.toolbar_ = null; |
| 138 } |
125 }; | 139 }; |
126 | 140 |
127 /** | 141 /** |
128 * Updates the position of DOM elements. | 142 * Updates the position of DOM elements. |
129 */ | 143 */ |
130 ImageEditor.Mode.Crop.prototype.positionDOM = function() { | 144 ImageEditor.Mode.Crop.prototype.positionDOM = function() { |
131 var screenClipped = this.viewport_.getImageBoundsOnScreenClipped(); | 145 var screenClipped = this.viewport_.getImageBoundsOnScreenClipped(); |
132 | 146 |
133 var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect()); | 147 var screenCrop = this.viewport_.imageToScreenRect(this.cropRect_.getRect()); |
134 var delta = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; | 148 var delta = ImageEditor.Mode.Crop.MOUSE_GRAB_RADIUS; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 * @return {function(number,number,boolean)} A function to be called on mouse | 233 * @return {function(number,number,boolean)} A function to be called on mouse |
220 * drag. It takes x coordinate value, y coordinate value, and shift key | 234 * drag. It takes x coordinate value, y coordinate value, and shift key |
221 * flag. | 235 * flag. |
222 */ | 236 */ |
223 ImageEditor.Mode.Crop.prototype.getDragHandler = function(x, y, touch) { | 237 ImageEditor.Mode.Crop.prototype.getDragHandler = function(x, y, touch) { |
224 var cropDragHandler = this.cropRect_.getDragHandler(x, y, touch); | 238 var cropDragHandler = this.cropRect_.getDragHandler(x, y, touch); |
225 if (!cropDragHandler) | 239 if (!cropDragHandler) |
226 return null; | 240 return null; |
227 | 241 |
228 return function(x, y, shiftKey) { | 242 return function(x, y, shiftKey) { |
| 243 if (this.toolbar_) |
| 244 this.toolbar_.element.classList.add('dimmable'); |
229 cropDragHandler(x, y, shiftKey); | 245 cropDragHandler(x, y, shiftKey); |
230 this.markUpdated(); | 246 this.markUpdated(); |
231 this.positionDOM(); | 247 this.positionDOM(); |
232 }.bind(this); | 248 }.bind(this); |
233 }; | 249 }; |
234 | 250 |
235 /** | 251 /** |
236 * Obtains the double tap action depending on the coordinate. | 252 * Obtains the double tap action depending on the coordinate. |
237 * | 253 * |
238 * @param {number} x X coordinate of the event. | 254 * @param {number} x X coordinate of the event. |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 * Forces the aspect ratio. | 586 * Forces the aspect ratio. |
571 * | 587 * |
572 * @param {number} aspectRatio Aspect ratio. | 588 * @param {number} aspectRatio Aspect ratio. |
573 * @param {Object} clipRect Clip rect. | 589 * @param {Object} clipRect Clip rect. |
574 */ | 590 */ |
575 DraggableRect.prototype.forceAspectRatio = function(aspectRatio, clipRect) { | 591 DraggableRect.prototype.forceAspectRatio = function(aspectRatio, clipRect) { |
576 // Get current rectangle scale. | 592 // Get current rectangle scale. |
577 var width = this.bounds_.right - this.bounds_.left; | 593 var width = this.bounds_.right - this.bounds_.left; |
578 var height = this.bounds_.bottom - this.bounds_.top; | 594 var height = this.bounds_.bottom - this.bounds_.top; |
579 var currentScale; | 595 var currentScale; |
580 if (this.dragMode_.xSide === 'none') | 596 if (!this.dragMode_) |
| 597 currentScale = ((width / aspectRatio) + height) / 2; |
| 598 else if (this.dragMode_.xSide === 'none') |
581 currentScale = height; | 599 currentScale = height; |
582 else if (this.dragMode_.ySide === 'none') | 600 else if (this.dragMode_.ySide === 'none') |
583 currentScale = width / aspectRatio; | 601 currentScale = width / aspectRatio; |
584 else | 602 else |
585 currentScale = Math.max(width / aspectRatio, height); | 603 currentScale = Math.max(width / aspectRatio, height); |
586 | 604 |
587 // Get maximum width/height scale. | 605 // Get maximum width/height scale. |
588 var maxWidth; | 606 var maxWidth; |
589 var maxHeight; | 607 var maxHeight; |
590 var center = (this.bounds_.left + this.bounds_.right) / 2; | 608 var center = (this.bounds_.left + this.bounds_.right) / 2; |
591 var middle = (this.bounds_.top + this.bounds_.bottom) / 2; | 609 var middle = (this.bounds_.top + this.bounds_.bottom) / 2; |
592 switch (this.dragMode_.xSide) { | 610 var xSide = this.dragMode_ ? this.dragMode_.xSide : 'none'; |
| 611 var ySide = this.dragMode_ ? this.dragMode_.ySide : 'none'; |
| 612 switch (xSide) { |
593 case 'left': | 613 case 'left': |
594 maxWidth = this.bounds_.right - clipRect.left; | 614 maxWidth = this.bounds_.right - clipRect.left; |
595 break; | 615 break; |
596 case 'right': | 616 case 'right': |
597 maxWidth = clipRect.left + clipRect.width - this.bounds_.left; | 617 maxWidth = clipRect.left + clipRect.width - this.bounds_.left; |
598 break; | 618 break; |
599 case 'none': | 619 case 'none': |
600 maxWidth = Math.min( | 620 maxWidth = Math.min( |
601 clipRect.left + clipRect.width - center, | 621 clipRect.left + clipRect.width - center, |
602 center - clipRect.left) * 2; | 622 center - clipRect.left) * 2; |
603 break; | 623 break; |
604 } | 624 } |
605 switch (this.dragMode_.ySide) { | 625 switch (ySide) { |
606 case 'top': | 626 case 'top': |
607 maxHeight = this.bounds_.bottom - clipRect.top; | 627 maxHeight = this.bounds_.bottom - clipRect.top; |
608 break; | 628 break; |
609 case 'bottom': | 629 case 'bottom': |
610 maxHeight = clipRect.top + clipRect.height - this.bounds_.top; | 630 maxHeight = clipRect.top + clipRect.height - this.bounds_.top; |
611 break; | 631 break; |
612 case 'none': | 632 case 'none': |
613 maxHeight = Math.min( | 633 maxHeight = Math.min( |
614 clipRect.top + clipRect.height - middle, | 634 clipRect.top + clipRect.height - middle, |
615 middle - clipRect.top) * 2; | 635 middle - clipRect.top) * 2; |
616 break; | 636 break; |
617 } | 637 } |
618 | 638 |
619 // Obtains target scale. | 639 // Obtains target scale. |
620 var targetScale = Math.min( | 640 var targetScale = Math.min( |
621 currentScale, | 641 currentScale, |
622 maxWidth / aspectRatio, | 642 maxWidth / aspectRatio, |
623 maxHeight); | 643 maxHeight); |
624 | 644 |
625 // Update bounds. | 645 // Update bounds. |
626 var newWidth = targetScale * aspectRatio; | 646 var newWidth = targetScale * aspectRatio; |
627 var newHeight = targetScale; | 647 var newHeight = targetScale; |
628 switch (this.dragMode_.xSide) { | 648 switch (xSide) { |
629 case 'left': | 649 case 'left': |
630 this.bounds_.left = this.bounds_.right - newWidth; | 650 this.bounds_.left = this.bounds_.right - newWidth; |
631 break; | 651 break; |
632 case 'right': | 652 case 'right': |
633 this.bounds_.right = this.bounds_.left + newWidth; | 653 this.bounds_.right = this.bounds_.left + newWidth; |
634 break; | 654 break; |
635 case 'none': | 655 case 'none': |
636 this.bounds_.left = center - newWidth / 2; | 656 this.bounds_.left = center - newWidth / 2; |
637 this.bounds_.right = center + newWidth / 2; | 657 this.bounds_.right = center + newWidth / 2; |
638 break; | 658 break; |
639 } | 659 } |
640 switch (this.dragMode_.ySide) { | 660 switch (ySide) { |
641 case 'top': | 661 case 'top': |
642 this.bounds_.top = this.bounds_.bottom - newHeight; | 662 this.bounds_.top = this.bounds_.bottom - newHeight; |
643 break; | 663 break; |
644 case 'bottom': | 664 case 'bottom': |
645 this.bounds_.bottom = this.bounds_.top + newHeight; | 665 this.bounds_.bottom = this.bounds_.top + newHeight; |
646 break; | 666 break; |
647 case 'none': | 667 case 'none': |
648 this.bounds_.top = middle - newHeight / 2; | 668 this.bounds_.top = middle - newHeight / 2; |
649 this.bounds_.bottom = middle + newHeight / 2; | 669 this.bounds_.bottom = middle + newHeight / 2; |
650 break; | 670 break; |
651 } | 671 } |
652 }; | 672 }; |
OLD | NEW |