Chromium Code Reviews| 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 * Viewport class controls the way the image is displayed (scale, offset etc). | 8 * Viewport class controls the way the image is displayed (scale, offset etc). |
| 9 * @constructor | 9 * @constructor |
| 10 */ | 10 */ |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Scale from the full resolution image to the screen displayed image. This is | 48 * Scale from the full resolution image to the screen displayed image. This is |
| 49 * not zoom operated by users. | 49 * not zoom operated by users. |
| 50 * @type {number} | 50 * @type {number} |
| 51 * @private | 51 * @private |
| 52 */ | 52 */ |
| 53 this.scale_ = 1; | 53 this.scale_ = 1; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Index of zoom ratio. 0 is "zoom ratio = 1". | |
| 57 * @type {number} | |
| 58 * @private | |
| 59 */ | |
| 60 this.zoomIndex_ = 0; | |
| 61 | |
| 62 /** | |
| 63 * Zoom ratio specified by user operations. | 56 * Zoom ratio specified by user operations. |
| 64 * @type {number} | 57 * @type {number} |
| 65 * @private | 58 * @private |
| 66 */ | 59 */ |
| 67 this.zoom_ = 1; | 60 this.zoom_ = 1; |
| 68 | 61 |
| 69 /** | 62 /** |
| 70 * Offset specified by user operations. | 63 * Offset specified by user operations. |
| 71 * @type {number} | 64 * @type {number} |
| 72 */ | 65 */ |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 86 */ | 79 */ |
| 87 this.generation_ = 0; | 80 this.generation_ = 0; |
| 88 | 81 |
| 89 this.update_(); | 82 this.update_(); |
| 90 Object.seal(this); | 83 Object.seal(this); |
| 91 } | 84 } |
| 92 | 85 |
| 93 /** | 86 /** |
| 94 * Zoom ratios. | 87 * Zoom ratios. |
| 95 * | 88 * |
| 96 * @type {Object.<string, number>} | 89 * @type {Array.<number>} |
| 97 * @const | 90 * @const |
| 98 */ | 91 */ |
| 99 Viewport.ZOOM_RATIOS = Object.freeze({ | 92 Viewport.ZOOM_RATIOS = Object.freeze([1, 1.5, 2, 3]); |
| 100 '3': 3, | |
| 101 '2': 2, | |
| 102 '1': 1.5, | |
| 103 '0': 1, | |
| 104 '-1': 0.75, | |
| 105 '-2': 0.5, | |
| 106 '-3': 0.25 | |
| 107 }); | |
| 108 | 93 |
| 109 /** | 94 /** |
| 110 * @param {number} width Image width. | 95 * @param {number} width Image width. |
| 111 * @param {number} height Image height. | 96 * @param {number} height Image height. |
| 112 */ | 97 */ |
| 113 Viewport.prototype.setImageSize = function(width, height) { | 98 Viewport.prototype.setImageSize = function(width, height) { |
| 114 this.imageBounds_ = new Rect(width, height); | 99 this.imageBounds_ = new Rect(width, height); |
| 115 this.update_(); | 100 this.update_(); |
| 116 }; | 101 }; |
| 117 | 102 |
| 118 /** | 103 /** |
| 119 * @param {number} width Screen width. | 104 * @param {number} width Screen width. |
| 120 * @param {number} height Screen height. | 105 * @param {number} height Screen height. |
| 121 */ | 106 */ |
| 122 Viewport.prototype.setScreenSize = function(width, height) { | 107 Viewport.prototype.setScreenSize = function(width, height) { |
| 123 this.screenBounds_ = new Rect(width, height); | 108 this.screenBounds_ = new Rect(width, height); |
| 124 this.update_(); | 109 this.update_(); |
| 125 }; | 110 }; |
| 126 | 111 |
| 127 /** | 112 /** |
| 128 * Sets the new zoom ratio. | 113 * Sets zoom value directly. |
| 129 * @param {number} zoomIndex New zoom index. | 114 * @param {number} zoom New zoom value. |
| 130 */ | 115 */ |
| 131 Viewport.prototype.setZoomIndex = function(zoomIndex) { | 116 Viewport.prototype.setZoom = function(zoom) { |
| 132 // Ignore the invalid zoomIndex. | 117 var zoomMin = Viewport.ZOOM_RATIOS[0]; |
| 133 if (!Viewport.ZOOM_RATIOS[zoomIndex.toString()]) | 118 var zoomMax = Viewport.ZOOM_RATIOS[Viewport.ZOOM_RATIOS.length - 1]; |
| 134 return; | 119 var adjustedZoom = Math.max(zoomMin, Math.min(zoom, zoomMax)); |
| 135 this.zoomIndex_ = zoomIndex; | 120 this.zoom_ = adjustedZoom; |
| 136 this.zoom_ = Viewport.ZOOM_RATIOS[zoomIndex]; | |
| 137 this.update_(); | 121 this.update_(); |
| 138 }; | 122 }; |
| 139 | 123 |
| 140 /** | 124 /** |
| 141 * Returns the current zoom index. | |
| 142 * @return {number} Zoon index. | |
| 143 */ | |
| 144 Viewport.prototype.getZoomIndex = function() { | |
| 145 return this.zoomIndex_; | |
| 146 }; | |
| 147 | |
| 148 /** | |
| 149 * Returns the value of zoom. | 125 * Returns the value of zoom. |
| 150 * @return {number} Zoom value. | 126 * @return {number} Zoom value. |
| 151 */ | 127 */ |
| 152 Viewport.prototype.getZoom = function() { | 128 Viewport.prototype.getZoom = function() { |
| 153 return this.zoomIndex_; | 129 return this.zoom_; |
| 154 }; | 130 }; |
| 155 | 131 |
| 156 /** | 132 /** |
| 133 * Sets the nearset larger value of ZOOM_RATIOS. | |
| 134 */ | |
| 135 Viewport.prototype.zoomIn = function() { | |
| 136 var zoom; | |
| 137 for (var i = 0; i < Viewport.ZOOM_RATIOS.length; i++) { | |
| 138 zoom = Viewport.ZOOM_RATIOS[i]; | |
| 139 if (zoom > this.zoom_) | |
| 140 break; | |
| 141 } | |
| 142 this.setZoom(zoom); | |
|
mtomasz
2014/07/23 08:15:08
nit: How about initializing zoom in #136 or adding
hirono
2014/07/23 09:27:22
Done.
| |
| 143 }; | |
| 144 | |
| 145 /** | |
| 146 * Sets the nearest smaller value of ZOOM_RATIOS. | |
| 147 */ | |
| 148 Viewport.prototype.zoomOut = function() { | |
| 149 var zoom; | |
| 150 for (var i = Viewport.ZOOM_RATIOS.length - 1; i >= 0; i--) { | |
| 151 zoom = Viewport.ZOOM_RATIOS[i]; | |
| 152 if (zoom < this.zoom_) | |
| 153 break; | |
| 154 } | |
| 155 this.setZoom(zoom); | |
| 156 }; | |
| 157 | |
| 158 /** | |
| 159 * Obtains whether the picture is zoomed or not. | |
| 160 * @return {boolean} | |
| 161 */ | |
| 162 Viewport.prototype.isZooming = function() { | |
|
mtomasz
2014/07/23 08:15:08
nit: How about just isZoomed?
hirono
2014/07/23 09:27:23
Done.
| |
| 163 return this.zoom_ !== 1.0; | |
| 164 }; | |
| 165 | |
| 166 /** | |
| 157 * Obtains the scale for the specified image size. | 167 * Obtains the scale for the specified image size. |
| 158 * | 168 * |
| 159 * @param {number} width Width of the full resolution image. | 169 * @param {number} width Width of the full resolution image. |
| 160 * @param {number} height Height of the full resolution image. | 170 * @param {number} height Height of the full resolution image. |
| 161 * @return {number} The ratio of the fullresotion image size and the calculated | 171 * @return {number} The ratio of the fullresotion image size and the calculated |
| 162 * displayed image size. | 172 * displayed image size. |
| 163 */ | 173 */ |
| 164 Viewport.prototype.getFittingScaleForImageSize_ = function(width, height) { | 174 Viewport.prototype.getFittingScaleForImageSize_ = function(width, height) { |
| 165 var scaleX = this.screenBounds_.width / width; | 175 var scaleX = this.screenBounds_.width / width; |
| 166 var scaleY = this.screenBounds_.height / height; | 176 var scaleY = this.screenBounds_.height / height; |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 ~~((this.screenBounds_.width - width) / 2) + offsetX, | 382 ~~((this.screenBounds_.width - width) / 2) + offsetX, |
| 373 ~~((this.screenBounds_.height - height) / 2) + offsetY, | 383 ~~((this.screenBounds_.height - height) / 2) + offsetY, |
| 374 width, | 384 width, |
| 375 height); | 385 height); |
| 376 }; | 386 }; |
| 377 | 387 |
| 378 /** | 388 /** |
| 379 * Resets zoom and offset. | 389 * Resets zoom and offset. |
| 380 */ | 390 */ |
| 381 Viewport.prototype.resetView = function() { | 391 Viewport.prototype.resetView = function() { |
| 382 this.zoomIndex_ = 0; | |
| 383 this.zoom_ = 1; | 392 this.zoom_ = 1; |
| 384 this.offsetX_ = 0; | 393 this.offsetX_ = 0; |
| 385 this.offsetY_ = 0; | 394 this.offsetY_ = 0; |
| 386 this.update_(); | 395 this.update_(); |
| 387 }; | 396 }; |
| 388 | 397 |
| 389 /** | 398 /** |
| 390 * Recalculate the viewport parameters. | 399 * Recalculate the viewport parameters. |
| 391 * @private | 400 * @private |
| 392 */ | 401 */ |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 var screenWidth = this.screenBounds_.width; | 524 var screenWidth = this.screenBounds_.width; |
| 516 var screenHeight = this.screenBounds_.height; | 525 var screenHeight = this.screenBounds_.height; |
| 517 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; | 526 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; |
| 518 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; | 527 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; |
| 519 return [ | 528 return [ |
| 520 'translate(' + dx + 'px,' + dy + 'px)', | 529 'translate(' + dx + 'px,' + dy + 'px)', |
| 521 'scale(' + scaleX + ',' + scaleY + ')', | 530 'scale(' + scaleX + ',' + scaleY + ')', |
| 522 this.getTransformation() | 531 this.getTransformation() |
| 523 ].join(' '); | 532 ].join(' '); |
| 524 }; | 533 }; |
| OLD | NEW |