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 12 matching lines...) Expand all Loading... |
23 */ | 23 */ |
24 this.screenBounds_ = new Rect(); | 24 this.screenBounds_ = new Rect(); |
25 | 25 |
26 /** | 26 /** |
27 * Scale from the full resolution image to the screen displayed image. This is | 27 * Scale from the full resolution image to the screen displayed image. This is |
28 * not zoom operated by users. | 28 * not zoom operated by users. |
29 * @type {number} | 29 * @type {number} |
30 * @private | 30 * @private |
31 */ | 31 */ |
32 this.scale_ = 1; | 32 this.scale_ = 1; |
| 33 |
| 34 /** |
| 35 * Index of zoom ratio. 0 is "zoom ratio = 1". |
| 36 * @type {number} |
| 37 * @private |
| 38 */ |
| 39 this.zoomIndex_ = 0; |
| 40 |
| 41 /** |
| 42 * Zoom ratio specified by user operations. |
| 43 * @type {number} |
| 44 * @private |
| 45 */ |
| 46 this.zoom_ = 1; |
| 47 |
33 this.offsetX_ = 0; | 48 this.offsetX_ = 0; |
34 this.offsetY_ = 0; | 49 this.offsetY_ = 0; |
35 | 50 |
36 this.generation_ = 0; | 51 this.generation_ = 0; |
37 | 52 |
38 this.update(); | 53 this.update(); |
39 } | 54 } |
40 | 55 |
41 /** | 56 /** |
| 57 * Zoom ratios. |
| 58 * |
| 59 * @type {Object.<string, number>} |
| 60 * @const |
| 61 */ |
| 62 Viewport.ZOOM_RATIOS = Object.freeze({ |
| 63 '3': 3, |
| 64 '2': 2, |
| 65 '1': 1.5, |
| 66 '0': 1, |
| 67 '-1': 0.75, |
| 68 '-2': 0.5, |
| 69 '-3': 0.25 |
| 70 }); |
| 71 |
| 72 /** |
42 * @param {number} width Image width. | 73 * @param {number} width Image width. |
43 * @param {number} height Image height. | 74 * @param {number} height Image height. |
44 */ | 75 */ |
45 Viewport.prototype.setImageSize = function(width, height) { | 76 Viewport.prototype.setImageSize = function(width, height) { |
46 this.imageBounds_ = new Rect(width, height); | 77 this.imageBounds_ = new Rect(width, height); |
47 this.invalidateCaches(); | 78 this.invalidateCaches(); |
48 }; | 79 }; |
49 | 80 |
50 /** | 81 /** |
51 * @param {number} width Screen width. | 82 * @param {number} width Screen width. |
52 * @param {number} height Screen height. | 83 * @param {number} height Screen height. |
53 */ | 84 */ |
54 Viewport.prototype.setScreenSize = function(width, height) { | 85 Viewport.prototype.setScreenSize = function(width, height) { |
55 this.screenBounds_ = new Rect(width, height); | 86 this.screenBounds_ = new Rect(width, height); |
56 this.invalidateCaches(); | 87 this.invalidateCaches(); |
57 }; | 88 }; |
58 | 89 |
59 /** | 90 /** |
| 91 * Sets the new zoom ratio. |
| 92 * @param {number} zoomIndex New zoom index. |
| 93 */ |
| 94 Viewport.prototype.setZoomIndex = function(zoomIndex) { |
| 95 // Ignore the invalid zoomIndex. |
| 96 if (!Viewport.ZOOM_RATIOS[zoomIndex.toString()]) |
| 97 return; |
| 98 |
| 99 this.zoomIndex_ = zoomIndex; |
| 100 this.zoom_ = Viewport.ZOOM_RATIOS[zoomIndex]; |
| 101 this.invalidateCaches(); |
| 102 }; |
| 103 |
| 104 /** |
| 105 * Returns the current zoom index. |
| 106 * @return {number} Zoon index. |
| 107 */ |
| 108 Viewport.prototype.getZoomIndex = function() { |
| 109 return this.zoomIndex_; |
| 110 }; |
| 111 |
| 112 /** |
60 * Set the size by an HTML element. | 113 * Set the size by an HTML element. |
61 * | 114 * |
62 * @param {HTMLElement} frame The element acting as the "screen". | 115 * @param {HTMLElement} frame The element acting as the "screen". |
63 */ | 116 */ |
64 Viewport.prototype.sizeByFrame = function(frame) { | 117 Viewport.prototype.sizeByFrame = function(frame) { |
65 this.setScreenSize(frame.clientWidth, frame.clientHeight); | 118 this.setScreenSize(frame.clientWidth, frame.clientHeight); |
66 }; | 119 }; |
67 | 120 |
68 /** | 121 /** |
69 * Set the size and scale to fit an HTML element. | 122 * Set the size and scale to fit an HTML element. |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 this.screenClipped_.top = this.imageOnScreen_.top; | 434 this.screenClipped_.top = this.imageOnScreen_.top; |
382 this.screenClipped_.height = this.imageOnScreen_.height; | 435 this.screenClipped_.height = this.imageOnScreen_.height; |
383 } | 436 } |
384 }; | 437 }; |
385 | 438 |
386 /** | 439 /** |
387 * Obtains CSS transformation for the screen image. | 440 * Obtains CSS transformation for the screen image. |
388 * @return {string} Transformation description. | 441 * @return {string} Transformation description. |
389 */ | 442 */ |
390 Viewport.prototype.getTransformation = function() { | 443 Viewport.prototype.getTransformation = function() { |
391 return 'scale(' + (1 / window.devicePixelRatio) + ')'; | 444 return 'scale(' + (1 / window.devicePixelRatio * this.zoom_) + ')'; |
392 }; | 445 }; |
393 | 446 |
394 /** | 447 /** |
395 * Obtains shift CSS transformation for the screen image. | 448 * Obtains shift CSS transformation for the screen image. |
396 * @param {number} dx Amount of shift. | 449 * @param {number} dx Amount of shift. |
397 * @return {string} Transformation description. | 450 * @return {string} Transformation description. |
398 */ | 451 */ |
399 Viewport.prototype.getShiftTransformation = function(dx) { | 452 Viewport.prototype.getShiftTransformation = function(dx) { |
400 return 'translateX(' + dx + 'px) ' + this.getTransformation(); | 453 return 'translateX(' + dx + 'px) ' + this.getTransformation(); |
401 }; | 454 }; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 var screenWidth = this.screenBounds_.width; | 520 var screenWidth = this.screenBounds_.width; |
468 var screenHeight = this.screenBounds_.height; | 521 var screenHeight = this.screenBounds_.height; |
469 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; | 522 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; |
470 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; | 523 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; |
471 return [ | 524 return [ |
472 'translate(' + dx + 'px,' + dy + 'px)', | 525 'translate(' + dx + 'px,' + dy + 'px)', |
473 'scale(' + scaleX + ',' + scaleY + ')', | 526 'scale(' + scaleX + ',' + scaleY + ')', |
474 this.getTransformation() | 527 this.getTransformation() |
475 ].join(' '); | 528 ].join(' '); |
476 }; | 529 }; |
OLD | NEW |