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 {Map.<number, number>} | |
yoshiki
2014/07/14 07:27:20
<string, number>?
hirono
2014/07/14 07:30:05
Done.
| |
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]) | |
yoshiki
2014/07/14 07:34:58
nit: Using toString() method for zoomIndex is bett
hirono
2014/07/14 08:10:00
Done.
| |
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. | |
yoshiki
2014/07/14 07:27:20
@return
hirono
2014/07/14 07:30:06
Done.
| |
106 */ | |
107 Viewport.prototype.getZoomIndex = function() { | |
108 return this.zoomIndex_; | |
109 }; | |
110 | |
111 /** | |
60 * Set the size by an HTML element. | 112 * Set the size by an HTML element. |
61 * | 113 * |
62 * @param {HTMLElement} frame The element acting as the "screen". | 114 * @param {HTMLElement} frame The element acting as the "screen". |
63 */ | 115 */ |
64 Viewport.prototype.sizeByFrame = function(frame) { | 116 Viewport.prototype.sizeByFrame = function(frame) { |
65 this.setScreenSize(frame.clientWidth, frame.clientHeight); | 117 this.setScreenSize(frame.clientWidth, frame.clientHeight); |
66 }; | 118 }; |
67 | 119 |
68 /** | 120 /** |
69 * Set the size and scale to fit an HTML element. | 121 * 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; | 433 this.screenClipped_.top = this.imageOnScreen_.top; |
382 this.screenClipped_.height = this.imageOnScreen_.height; | 434 this.screenClipped_.height = this.imageOnScreen_.height; |
383 } | 435 } |
384 }; | 436 }; |
385 | 437 |
386 /** | 438 /** |
387 * Obtains CSS transformation for the screen image. | 439 * Obtains CSS transformation for the screen image. |
388 * @return {string} Transformation description. | 440 * @return {string} Transformation description. |
389 */ | 441 */ |
390 Viewport.prototype.getTransformation = function() { | 442 Viewport.prototype.getTransformation = function() { |
391 return 'scale(' + (1 / window.devicePixelRatio) + ')'; | 443 return 'scale(' + (1 / window.devicePixelRatio * this.zoom_) + ')'; |
392 }; | 444 }; |
393 | 445 |
394 /** | 446 /** |
395 * Obtains shift CSS transformation for the screen image. | 447 * Obtains shift CSS transformation for the screen image. |
396 * @param {number} dx Amount of shift. | 448 * @param {number} dx Amount of shift. |
397 * @return {string} Transformation description. | 449 * @return {string} Transformation description. |
398 */ | 450 */ |
399 Viewport.prototype.getShiftTransformation = function(dx) { | 451 Viewport.prototype.getShiftTransformation = function(dx) { |
400 return 'translateX(' + dx + 'px) ' + this.getTransformation(); | 452 return 'translateX(' + dx + 'px) ' + this.getTransformation(); |
401 }; | 453 }; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 var screenWidth = this.screenBounds_.width; | 519 var screenWidth = this.screenBounds_.width; |
468 var screenHeight = this.screenBounds_.height; | 520 var screenHeight = this.screenBounds_.height; |
469 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; | 521 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; |
470 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; | 522 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; |
471 return [ | 523 return [ |
472 'translate(' + dx + 'px,' + dy + 'px)', | 524 'translate(' + dx + 'px,' + dy + 'px)', |
473 'scale(' + scaleX + ',' + scaleY + ')', | 525 'scale(' + scaleX + ',' + scaleY + ')', |
474 this.getTransformation() | 526 this.getTransformation() |
475 ].join(' '); | 527 ].join(' '); |
476 }; | 528 }; |
OLD | NEW |