| 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 */ |
| 11 function Viewport() { | 11 function Viewport() { |
| 12 this.imageBounds_ = new Rect(); | 12 this.imageBounds_ = new Rect(); |
| 13 this.screenBounds_ = new Rect(); | 13 this.screenBounds_ = new Rect(); |
| 14 | 14 |
| 15 this.scale_ = 1; | 15 this.scale_ = 1; |
| 16 this.offsetX_ = 0; | 16 this.offsetX_ = 0; |
| 17 this.offsetY_ = 0; | 17 this.offsetY_ = 0; |
| 18 | 18 |
| 19 this.generation_ = 0; | 19 this.generation_ = 0; |
| 20 | 20 |
| 21 this.scaleControl_ = null; | |
| 22 this.repaintCallbacks_ = []; | 21 this.repaintCallbacks_ = []; |
| 23 this.update(); | 22 this.update(); |
| 24 } | 23 } |
| 25 | 24 |
| 26 /* | 25 /* |
| 27 * Viewport modification. | 26 * Viewport modification. |
| 28 */ | 27 */ |
| 29 | 28 |
| 30 /** | 29 /** |
| 31 * @param {Object} scaleControl The UI object responsible for scaling. | |
| 32 */ | |
| 33 Viewport.prototype.setScaleControl = function(scaleControl) { | |
| 34 this.scaleControl_ = scaleControl; | |
| 35 }; | |
| 36 | |
| 37 /** | |
| 38 * @param {number} width Image width. | 30 * @param {number} width Image width. |
| 39 * @param {number} height Image height. | 31 * @param {number} height Image height. |
| 40 */ | 32 */ |
| 41 Viewport.prototype.setImageSize = function(width, height) { | 33 Viewport.prototype.setImageSize = function(width, height) { |
| 42 this.imageBounds_ = new Rect(width, height); | 34 this.imageBounds_ = new Rect(width, height); |
| 43 if (this.scaleControl_) this.scaleControl_.displayImageSize(width, height); | |
| 44 this.invalidateCaches(); | 35 this.invalidateCaches(); |
| 45 }; | 36 }; |
| 46 | 37 |
| 47 /** | 38 /** |
| 48 * @param {number} width Screen width. | 39 * @param {number} width Screen width. |
| 49 * @param {number} height Screen height. | 40 * @param {number} height Screen height. |
| 50 */ | 41 */ |
| 51 Viewport.prototype.setScreenSize = function(width, height) { | 42 Viewport.prototype.setScreenSize = function(width, height) { |
| 52 this.screenBounds_ = new Rect(width, height); | 43 this.screenBounds_ = new Rect(width, height); |
| 53 if (this.scaleControl_) | |
| 54 this.scaleControl_.setMinScale(this.getFittingScale()); | |
| 55 this.invalidateCaches(); | 44 this.invalidateCaches(); |
| 56 }; | 45 }; |
| 57 | 46 |
| 58 /** | 47 /** |
| 59 * Set the size by an HTML element. | 48 * Set the size by an HTML element. |
| 60 * | 49 * |
| 61 * @param {HTMLElement} frame The element acting as the "screen". | 50 * @param {HTMLElement} frame The element acting as the "screen". |
| 62 */ | 51 */ |
| 63 Viewport.prototype.sizeByFrame = function(frame) { | 52 Viewport.prototype.sizeByFrame = function(frame) { |
| 64 this.setScreenSize(frame.clientWidth, frame.clientHeight); | 53 this.setScreenSize(frame.clientWidth, frame.clientHeight); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 83 */ | 72 */ |
| 84 Viewport.prototype.getScale = function() { return this.scale_; }; | 73 Viewport.prototype.getScale = function() { return this.scale_; }; |
| 85 | 74 |
| 86 /** | 75 /** |
| 87 * @param {number} scale The new scale. | 76 * @param {number} scale The new scale. |
| 88 * @param {boolean} notify True if the change should be reflected in the UI. | 77 * @param {boolean} notify True if the change should be reflected in the UI. |
| 89 */ | 78 */ |
| 90 Viewport.prototype.setScale = function(scale, notify) { | 79 Viewport.prototype.setScale = function(scale, notify) { |
| 91 if (this.scale_ == scale) return; | 80 if (this.scale_ == scale) return; |
| 92 this.scale_ = scale; | 81 this.scale_ = scale; |
| 93 if (notify && this.scaleControl_) this.scaleControl_.displayScale(scale); | |
| 94 this.invalidateCaches(); | 82 this.invalidateCaches(); |
| 95 }; | 83 }; |
| 96 | 84 |
| 97 /** | 85 /** |
| 98 * @return {number} Best scale to fit the current image into the current screen. | 86 * @return {number} Best scale to fit the current image into the current screen. |
| 99 */ | 87 */ |
| 100 Viewport.prototype.getFittingScale = function() { | 88 Viewport.prototype.getFittingScale = function() { |
| 101 var scaleX = this.screenBounds_.width / this.imageBounds_.width; | 89 var scaleX = this.screenBounds_.width / this.imageBounds_.width; |
| 102 var scaleY = this.screenBounds_.height / this.imageBounds_.height; | 90 var scaleY = this.screenBounds_.height / this.imageBounds_.height; |
| 103 // Scales > (1 / this.getDevicePixelRatio()) do not look good. Also they are | 91 // Scales > (1 / this.getDevicePixelRatio()) do not look good. Also they are |
| 104 // not really useful as we do not have any pixel-level operations. | 92 // not really useful as we do not have any pixel-level operations. |
| 105 return Math.min(1 / Viewport.getDevicePixelRatio(), scaleX, scaleY); | 93 return Math.min(1 / Viewport.getDevicePixelRatio(), scaleX, scaleY); |
| 106 }; | 94 }; |
| 107 | 95 |
| 108 /** | 96 /** |
| 109 * Set the scale to fit the image into the screen. | 97 * Set the scale to fit the image into the screen. |
| 110 */ | 98 */ |
| 111 Viewport.prototype.fitImage = function() { | 99 Viewport.prototype.fitImage = function() { |
| 112 var scale = this.getFittingScale(); | 100 var scale = this.getFittingScale(); |
| 113 if (this.scaleControl_) this.scaleControl_.setMinScale(scale); | |
| 114 this.setScale(scale, true); | 101 this.setScale(scale, true); |
| 115 }; | 102 }; |
| 116 | 103 |
| 117 /** | 104 /** |
| 118 * @return {number} X-offset of the viewport. | 105 * @return {number} X-offset of the viewport. |
| 119 */ | 106 */ |
| 120 Viewport.prototype.getOffsetX = function() { return this.offsetX_; }; | 107 Viewport.prototype.getOffsetX = function() { return this.offsetX_; }; |
| 121 | 108 |
| 122 /** | 109 /** |
| 123 * @return {number} Y-offset of the viewport. | 110 * @return {number} Y-offset of the viewport. |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 }; | 410 }; |
| 424 | 411 |
| 425 /** | 412 /** |
| 426 * Repaint all clients. | 413 * Repaint all clients. |
| 427 */ | 414 */ |
| 428 Viewport.prototype.repaint = function() { | 415 Viewport.prototype.repaint = function() { |
| 429 this.update(); | 416 this.update(); |
| 430 for (var i = 0; i != this.repaintCallbacks_.length; i++) | 417 for (var i = 0; i != this.repaintCallbacks_.length; i++) |
| 431 this.repaintCallbacks_[i](); | 418 this.repaintCallbacks_[i](); |
| 432 }; | 419 }; |
| OLD | NEW |