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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 this.offsetY_ = 0; | 79 this.offsetY_ = 0; |
80 | 80 |
81 /** | 81 /** |
82 * Generation of the screen size image cache. | 82 * Generation of the screen size image cache. |
83 * This is incremented every time when the size of image cache is changed. | 83 * This is incremented every time when the size of image cache is changed. |
84 * @type {number} | 84 * @type {number} |
85 * @private | 85 * @private |
86 */ | 86 */ |
87 this.generation_ = 0; | 87 this.generation_ = 0; |
88 | 88 |
89 this.update(); | 89 this.update_(); |
90 Object.seal(this); | 90 Object.seal(this); |
91 } | 91 } |
92 | 92 |
93 /** | 93 /** |
94 * Zoom ratios. | 94 * Zoom ratios. |
95 * | 95 * |
96 * @type {Object.<string, number>} | 96 * @type {Object.<string, number>} |
97 * @const | 97 * @const |
98 */ | 98 */ |
99 Viewport.ZOOM_RATIOS = Object.freeze({ | 99 Viewport.ZOOM_RATIOS = Object.freeze({ |
100 '3': 3, | 100 '3': 3, |
101 '2': 2, | 101 '2': 2, |
102 '1': 1.5, | 102 '1': 1.5, |
103 '0': 1, | 103 '0': 1, |
104 '-1': 0.75, | 104 '-1': 0.75, |
105 '-2': 0.5, | 105 '-2': 0.5, |
106 '-3': 0.25 | 106 '-3': 0.25 |
107 }); | 107 }); |
108 | 108 |
109 /** | 109 /** |
110 * @param {number} width Image width. | 110 * @param {number} width Image width. |
111 * @param {number} height Image height. | 111 * @param {number} height Image height. |
112 */ | 112 */ |
113 Viewport.prototype.setImageSize = function(width, height) { | 113 Viewport.prototype.setImageSize = function(width, height) { |
114 this.imageBounds_ = new Rect(width, height); | 114 this.imageBounds_ = new Rect(width, height); |
115 this.update(); | 115 this.update_(); |
116 this.invalidateCaches(); | |
117 }; | 116 }; |
118 | 117 |
119 /** | 118 /** |
120 * @param {number} width Screen width. | 119 * @param {number} width Screen width. |
121 * @param {number} height Screen height. | 120 * @param {number} height Screen height. |
122 */ | 121 */ |
123 Viewport.prototype.setScreenSize = function(width, height) { | 122 Viewport.prototype.setScreenSize = function(width, height) { |
124 this.screenBounds_ = new Rect(width, height); | 123 this.screenBounds_ = new Rect(width, height); |
125 this.update(); | 124 this.update_(); |
126 this.invalidateCaches(); | |
127 }; | 125 }; |
128 | 126 |
129 /** | 127 /** |
130 * Sets the new zoom ratio. | 128 * Sets the new zoom ratio. |
131 * @param {number} zoomIndex New zoom index. | 129 * @param {number} zoomIndex New zoom index. |
132 */ | 130 */ |
133 Viewport.prototype.setZoomIndex = function(zoomIndex) { | 131 Viewport.prototype.setZoomIndex = function(zoomIndex) { |
134 // Ignore the invalid zoomIndex. | 132 // Ignore the invalid zoomIndex. |
135 if (!Viewport.ZOOM_RATIOS[zoomIndex.toString()]) | 133 if (!Viewport.ZOOM_RATIOS[zoomIndex.toString()]) |
136 return; | 134 return; |
137 this.zoomIndex_ = zoomIndex; | 135 this.zoomIndex_ = zoomIndex; |
138 this.zoom_ = Viewport.ZOOM_RATIOS[zoomIndex]; | 136 this.zoom_ = Viewport.ZOOM_RATIOS[zoomIndex]; |
139 this.update(); | 137 this.update_(); |
140 }; | 138 }; |
141 | 139 |
142 /** | 140 /** |
143 * Returns the current zoom index. | 141 * Returns the current zoom index. |
144 * @return {number} Zoon index. | 142 * @return {number} Zoon index. |
145 */ | 143 */ |
146 Viewport.prototype.getZoomIndex = function() { | 144 Viewport.prototype.getZoomIndex = function() { |
147 return this.zoomIndex_; | 145 return this.zoomIndex_; |
148 }; | 146 }; |
149 | 147 |
150 /** | 148 /** |
151 * @return {number} Scale. | |
152 */ | |
153 Viewport.prototype.getScale = function() { return this.scale_; }; | |
154 | |
155 /** | |
156 * @param {number} scale The new scale. | |
157 */ | |
158 Viewport.prototype.setScale = function(scale) { | |
159 if (this.scale_ == scale) | |
160 return; | |
161 this.scale_ = scale; | |
162 this.update(); | |
163 this.invalidateCaches(); | |
164 }; | |
165 | |
166 /** | |
167 * @return {number} Best scale to fit the current image into the current screen. | |
168 */ | |
169 Viewport.prototype.getFittingScale = function() { | |
170 return this.getFittingScaleForImageSize_( | |
171 this.imageBounds_.width, this.imageBounds_.height); | |
172 }; | |
173 | |
174 /** | |
175 * Obtains the scale for the specified image size. | 149 * Obtains the scale for the specified image size. |
176 * | 150 * |
177 * @param {number} width Width of the full resolution image. | 151 * @param {number} width Width of the full resolution image. |
178 * @param {number} height Height of the full resolution image. | 152 * @param {number} height Height of the full resolution image. |
179 * @return {number} The ratio of the fullresotion image size and the calculated | 153 * @return {number} The ratio of the fullresotion image size and the calculated |
180 * displayed image size. | 154 * displayed image size. |
181 */ | 155 */ |
182 Viewport.prototype.getFittingScaleForImageSize_ = function(width, height) { | 156 Viewport.prototype.getFittingScaleForImageSize_ = function(width, height) { |
183 var scaleX = this.screenBounds_.width / width; | 157 var scaleX = this.screenBounds_.width / width; |
184 var scaleY = this.screenBounds_.height / height; | 158 var scaleY = this.screenBounds_.height / height; |
185 // Scales > (1 / devicePixelRatio) do not look good. Also they are | 159 // Scales > (1 / devicePixelRatio) do not look good. Also they are |
186 // not really useful as we do not have any pixel-level operations. | 160 // not really useful as we do not have any pixel-level operations. |
187 return Math.min(1 / window.devicePixelRatio, scaleX, scaleY); | 161 return Math.min(1 / window.devicePixelRatio, scaleX, scaleY); |
188 }; | 162 }; |
189 | 163 |
190 /** | 164 /** |
191 * Set the scale to fit the image into the screen. | |
192 */ | |
193 Viewport.prototype.fitImage = function() { | |
194 this.setScale(this.getFittingScale()); | |
195 }; | |
196 | |
197 /** | |
198 * @return {number} X-offset of the viewport. | 165 * @return {number} X-offset of the viewport. |
199 */ | 166 */ |
200 Viewport.prototype.getOffsetX = function() { return this.offsetX_; }; | 167 Viewport.prototype.getOffsetX = function() { return this.offsetX_; }; |
201 | 168 |
202 /** | 169 /** |
203 * @return {number} Y-offset of the viewport. | 170 * @return {number} Y-offset of the viewport. |
204 */ | 171 */ |
205 Viewport.prototype.getOffsetY = function() { return this.offsetY_; }; | 172 Viewport.prototype.getOffsetY = function() { return this.offsetY_; }; |
206 | 173 |
207 /** | 174 /** |
208 * Set the image offset in the viewport. | 175 * Set the image offset in the viewport. |
209 * @param {number} x X-offset. | 176 * @param {number} x X-offset. |
210 * @param {number} y Y-offset. | 177 * @param {number} y Y-offset. |
211 * @param {boolean} ignoreClipping True if no clipping should be applied. | |
212 */ | 178 */ |
213 Viewport.prototype.setOffset = function(x, y, ignoreClipping) { | 179 Viewport.prototype.setOffset = function(x, y) { |
214 if (!ignoreClipping) { | 180 if (this.offsetX_ == x && this.offsetY_ == y) |
215 x = this.clampOffsetX_(x); | 181 return; |
216 y = this.clampOffsetY_(y); | |
217 } | |
218 if (this.offsetX_ == x && this.offsetY_ == y) return; | |
219 this.offsetX_ = x; | 182 this.offsetX_ = x; |
220 this.offsetY_ = y; | 183 this.offsetY_ = y; |
221 this.invalidateCaches(); | 184 this.update_(); |
222 }; | 185 }; |
223 | 186 |
224 /** | 187 /** |
225 * @return {Rect} The image bounds in image coordinates. | 188 * @return {Rect} The image bounds in image coordinates. |
226 */ | 189 */ |
227 Viewport.prototype.getImageBounds = function() { return this.imageBounds_; }; | 190 Viewport.prototype.getImageBounds = function() { return this.imageBounds_; }; |
228 | 191 |
229 /** | 192 /** |
230 * @return {Rect} The screen bounds in screen coordinates. | 193 * @return {Rect} The screen bounds in screen coordinates. |
231 */ | 194 */ |
(...skipping 11 matching lines...) Expand all Loading... |
243 | 206 |
244 /** | 207 /** |
245 * A counter that is incremented with each viewport state change. | 208 * A counter that is incremented with each viewport state change. |
246 * Clients that cache anything that depends on the viewport state should keep | 209 * Clients that cache anything that depends on the viewport state should keep |
247 * track of this counter. | 210 * track of this counter. |
248 * @return {number} counter. | 211 * @return {number} counter. |
249 */ | 212 */ |
250 Viewport.prototype.getCacheGeneration = function() { return this.generation_; }; | 213 Viewport.prototype.getCacheGeneration = function() { return this.generation_; }; |
251 | 214 |
252 /** | 215 /** |
253 * Called on event view port state change. | |
254 */ | |
255 Viewport.prototype.invalidateCaches = function() { this.generation_++; }; | |
256 | |
257 /** | |
258 * @return {Rect} The image bounds in screen coordinates. | 216 * @return {Rect} The image bounds in screen coordinates. |
259 */ | 217 */ |
260 Viewport.prototype.getImageBoundsOnScreen = function() { | 218 Viewport.prototype.getImageBoundsOnScreen = function() { |
261 return this.imageBoundsOnScreen_; | 219 return this.imageBoundsOnScreen_; |
262 }; | 220 }; |
263 | 221 |
264 /** | 222 /** |
265 * The image bounds in screen coordinates. | 223 * The image bounds in screen coordinates. |
266 * This returns the bounds of element before applying zoom and offset. | 224 * This returns the bounds of element before applying zoom and offset. |
267 * @return {Rect} | 225 * @return {Rect} |
268 */ | 226 */ |
269 Viewport.prototype.getImageElementBoundsOnScreen = function() { | 227 Viewport.prototype.getImageElementBoundsOnScreen = function() { |
270 return this.imageElementBoundsOnScreen_; | 228 return this.imageElementBoundsOnScreen_; |
271 }; | 229 }; |
272 | 230 |
273 /** | 231 /** |
274 * The image bounds on screen, which is clipped with the screen size. | 232 * The image bounds on screen, which is clipped with the screen size. |
275 * @return {Rect} | 233 * @return {Rect} |
276 */ | 234 */ |
277 Viewport.prototype.getImageBoundsOnScreenClipped = function() { | 235 Viewport.prototype.getImageBoundsOnScreenClipped = function() { |
278 return this.imageBoundsOnScreenClipped_; | 236 return this.imageBoundsOnScreenClipped_; |
279 }; | 237 }; |
280 | 238 |
281 /** | 239 /** |
282 * @param {number} size Size in screen coordinates. | 240 * @param {number} size Size in screen coordinates. |
283 * @return {number} Size in image coordinates. | 241 * @return {number} Size in image coordinates. |
284 */ | 242 */ |
285 Viewport.prototype.screenToImageSize = function(size) { | 243 Viewport.prototype.screenToImageSize = function(size) { |
286 return size / this.getScale(); | 244 return size / this.scale_; |
287 }; | 245 }; |
288 | 246 |
289 /** | 247 /** |
290 * @param {number} x X in screen coordinates. | 248 * @param {number} x X in screen coordinates. |
291 * @return {number} X in image coordinates. | 249 * @return {number} X in image coordinates. |
292 */ | 250 */ |
293 Viewport.prototype.screenToImageX = function(x) { | 251 Viewport.prototype.screenToImageX = function(x) { |
294 return Math.round((x - this.imageBoundsOnScreen_.left) / this.getScale()); | 252 return Math.round((x - this.imageBoundsOnScreen_.left) / this.scale_); |
295 }; | 253 }; |
296 | 254 |
297 /** | 255 /** |
298 * @param {number} y Y in screen coordinates. | 256 * @param {number} y Y in screen coordinates. |
299 * @return {number} Y in image coordinates. | 257 * @return {number} Y in image coordinates. |
300 */ | 258 */ |
301 Viewport.prototype.screenToImageY = function(y) { | 259 Viewport.prototype.screenToImageY = function(y) { |
302 return Math.round((y - this.imageBoundsOnScreen_.top) / this.getScale()); | 260 return Math.round((y - this.imageBoundsOnScreen_.top) / this.scale_); |
303 }; | 261 }; |
304 | 262 |
305 /** | 263 /** |
306 * @param {Rect} rect Rectangle in screen coordinates. | 264 * @param {Rect} rect Rectangle in screen coordinates. |
307 * @return {Rect} Rectangle in image coordinates. | 265 * @return {Rect} Rectangle in image coordinates. |
308 */ | 266 */ |
309 Viewport.prototype.screenToImageRect = function(rect) { | 267 Viewport.prototype.screenToImageRect = function(rect) { |
310 return new Rect( | 268 return new Rect( |
311 this.screenToImageX(rect.left), | 269 this.screenToImageX(rect.left), |
312 this.screenToImageY(rect.top), | 270 this.screenToImageY(rect.top), |
313 this.screenToImageSize(rect.width), | 271 this.screenToImageSize(rect.width), |
314 this.screenToImageSize(rect.height)); | 272 this.screenToImageSize(rect.height)); |
315 }; | 273 }; |
316 | 274 |
317 /** | 275 /** |
318 * @param {number} size Size in image coordinates. | 276 * @param {number} size Size in image coordinates. |
319 * @return {number} Size in screen coordinates. | 277 * @return {number} Size in screen coordinates. |
320 */ | 278 */ |
321 Viewport.prototype.imageToScreenSize = function(size) { | 279 Viewport.prototype.imageToScreenSize = function(size) { |
322 return size * this.getScale(); | 280 return size * this.scale_; |
323 }; | 281 }; |
324 | 282 |
325 /** | 283 /** |
326 * @param {number} x X in image coordinates. | 284 * @param {number} x X in image coordinates. |
327 * @return {number} X in screen coordinates. | 285 * @return {number} X in screen coordinates. |
328 */ | 286 */ |
329 Viewport.prototype.imageToScreenX = function(x) { | 287 Viewport.prototype.imageToScreenX = function(x) { |
330 return Math.round(this.imageBoundsOnScreen_.left + x * this.getScale()); | 288 return Math.round(this.imageBoundsOnScreen_.left + x * this.scale_); |
331 }; | 289 }; |
332 | 290 |
333 /** | 291 /** |
334 * @param {number} y Y in image coordinates. | 292 * @param {number} y Y in image coordinates. |
335 * @return {number} Y in screen coordinates. | 293 * @return {number} Y in screen coordinates. |
336 */ | 294 */ |
337 Viewport.prototype.imageToScreenY = function(y) { | 295 Viewport.prototype.imageToScreenY = function(y) { |
338 return Math.round(this.imageBoundsOnScreen_.top + y * this.getScale()); | 296 return Math.round(this.imageBoundsOnScreen_.top + y * this.scale_); |
339 }; | 297 }; |
340 | 298 |
341 /** | 299 /** |
342 * @param {Rect} rect Rectangle in image coordinates. | 300 * @param {Rect} rect Rectangle in image coordinates. |
343 * @return {Rect} Rectangle in screen coordinates. | 301 * @return {Rect} Rectangle in screen coordinates. |
344 */ | 302 */ |
345 Viewport.prototype.imageToScreenRect = function(rect) { | 303 Viewport.prototype.imageToScreenRect = function(rect) { |
346 return new Rect( | 304 return new Rect( |
347 this.imageToScreenX(rect.left), | 305 this.imageToScreenX(rect.left), |
348 this.imageToScreenY(rect.top), | 306 this.imageToScreenY(rect.top), |
(...skipping 27 matching lines...) Expand all Loading... |
376 return Math.round( | 334 return Math.round( |
377 (this.screenBounds_.height - this.imageBounds_.height * this.scale_) / 2); | 335 (this.screenBounds_.height - this.imageBounds_.height * this.scale_) / 2); |
378 }; | 336 }; |
379 | 337 |
380 /** | 338 /** |
381 * @param {number} x X-offset. | 339 * @param {number} x X-offset. |
382 * @return {number} X-offset clamped to the valid range. | 340 * @return {number} X-offset clamped to the valid range. |
383 * @private | 341 * @private |
384 */ | 342 */ |
385 Viewport.prototype.clampOffsetX_ = function(x) { | 343 Viewport.prototype.clampOffsetX_ = function(x) { |
386 var limit = Math.round(Math.max(0, -this.getMarginX_() / this.getScale())); | 344 var limit = Math.round(Math.max(0, -this.getMarginX_() / this.scale_)); |
387 return ImageUtil.clamp(-limit, x, limit); | 345 return ImageUtil.clamp(-limit, x, limit); |
388 }; | 346 }; |
389 | 347 |
390 /** | 348 /** |
391 * @param {number} y Y-offset. | 349 * @param {number} y Y-offset. |
392 * @return {number} Y-offset clamped to the valid range. | 350 * @return {number} Y-offset clamped to the valid range. |
393 * @private | 351 * @private |
394 */ | 352 */ |
395 Viewport.prototype.clampOffsetY_ = function(y) { | 353 Viewport.prototype.clampOffsetY_ = function(y) { |
396 var limit = Math.round(Math.max(0, -this.getMarginY_() / this.getScale())); | 354 var limit = Math.round(Math.max(0, -this.getMarginY_() / this.scale_)); |
397 return ImageUtil.clamp(-limit, y, limit); | 355 return ImageUtil.clamp(-limit, y, limit); |
398 }; | 356 }; |
399 | 357 |
400 /** | 358 /** |
401 * @private | 359 * @private |
402 */ | 360 */ |
403 Viewport.prototype.getCenteredRect_ = function( | 361 Viewport.prototype.getCenteredRect_ = function( |
404 width, height, offsetX, offsetY) { | 362 width, height, offsetX, offsetY) { |
405 return new Rect( | 363 return new Rect( |
406 ~~((this.screenBounds_.width - width) / 2) + offsetX, | 364 ~~((this.screenBounds_.width - width) / 2) + offsetX, |
407 ~~((this.screenBounds_.height - height) / 2) + offsetY, | 365 ~~((this.screenBounds_.height - height) / 2) + offsetY, |
408 width, | 366 width, |
409 height); | 367 height); |
410 }; | 368 }; |
411 | 369 |
412 /** | 370 /** |
| 371 * Resets zoom and offset. |
| 372 */ |
| 373 Viewport.prototype.resetView = function() { |
| 374 this.zoomIndex_ = 0; |
| 375 this.zoom_ = 1; |
| 376 this.offsetX_ = 0; |
| 377 this.offsetY_ = 0; |
| 378 this.update_(); |
| 379 }; |
| 380 |
| 381 /** |
413 * Recalculate the viewport parameters. | 382 * Recalculate the viewport parameters. |
| 383 * @private |
414 */ | 384 */ |
415 Viewport.prototype.update = function() { | 385 Viewport.prototype.update_ = function() { |
416 var scale = this.getScale(); | 386 // Update scale. |
| 387 this.scale_ = this.getFittingScaleForImageSize_( |
| 388 this.imageBounds_.width, this.imageBounds_.height); |
| 389 |
| 390 // Limit offset values. |
| 391 var zoomedWidht = ~~(this.imageBounds_.width * this.scale_ * this.zoom_); |
| 392 var zoomedHeight = ~~(this.imageBounds_.height * this.scale_ * this.zoom_); |
| 393 var dx = Math.max(zoomedWidht - this.screenBounds_.width, 0) / 2; |
| 394 var dy = Math.max(zoomedHeight - this.screenBounds_.height, 0) /2; |
| 395 this.offsetX_ = ImageUtil.clamp(-dx, this.offsetX_, dx); |
| 396 this.offsetY_ = ImageUtil.clamp(-dy, this.offsetY_, dy); |
417 | 397 |
418 // Image bounds on screen. | 398 // Image bounds on screen. |
419 this.imageBoundsOnScreen_ = this.getCenteredRect_( | 399 this.imageBoundsOnScreen_ = this.getCenteredRect_( |
420 ~~(this.imageBounds_.width * scale * this.zoom_), | 400 zoomedWidht, zoomedHeight, this.offsetX_, this.offsetY_); |
421 ~~(this.imageBounds_.height * scale * this.zoom_), | |
422 this.offsetX_, | |
423 this.offsetY_); | |
424 | 401 |
425 // Image bounds of element (that is not applied zoom and offset) on screen. | 402 // Image bounds of element (that is not applied zoom and offset) on screen. |
| 403 var oldBounds = this.imageElementBoundsOnScreen_; |
426 this.imageElementBoundsOnScreen_ = this.getCenteredRect_( | 404 this.imageElementBoundsOnScreen_ = this.getCenteredRect_( |
427 ~~(this.imageBounds_.width * scale), | 405 ~~(this.imageBounds_.width * this.scale_), |
428 ~~(this.imageBounds_.height * scale), | 406 ~~(this.imageBounds_.height * this.scale_), |
429 0, | 407 0, |
430 0); | 408 0); |
| 409 if (!oldBounds || |
| 410 this.imageElementBoundsOnScreen_.width != oldBounds.width || |
| 411 this.imageElementBoundsOnScreen_.height != oldBounds.height) { |
| 412 this.generation_++; |
| 413 } |
431 | 414 |
432 // Image bounds on screen cliped with the screen bounds. | 415 // Image bounds on screen cliped with the screen bounds. |
433 var left = Math.max(this.imageBoundsOnScreen_.left, 0); | 416 var left = Math.max(this.imageBoundsOnScreen_.left, 0); |
434 var top = Math.max(this.imageBoundsOnScreen_.top, 0); | 417 var top = Math.max(this.imageBoundsOnScreen_.top, 0); |
435 var right = Math.min( | 418 var right = Math.min( |
436 this.imageBoundsOnScreen_.right, this.screenBounds_.width); | 419 this.imageBoundsOnScreen_.right, this.screenBounds_.width); |
437 var bottom = Math.min( | 420 var bottom = Math.min( |
438 this.imageBoundsOnScreen_.bottom, this.screenBounds_.height); | 421 this.imageBoundsOnScreen_.bottom, this.screenBounds_.height); |
439 this.imageBoundsOnScreenClipped_ = new Rect( | 422 this.imageBoundsOnScreenClipped_ = new Rect( |
440 left, top, right - left, bottom - top); | 423 left, top, right - left, bottom - top); |
441 }; | 424 }; |
442 | 425 |
443 /** | 426 /** |
444 * Obtains CSS transformation for the screen image. | 427 * Obtains CSS transformation for the screen image. |
445 * @return {string} Transformation description. | 428 * @return {string} Transformation description. |
446 */ | 429 */ |
447 Viewport.prototype.getTransformation = function() { | 430 Viewport.prototype.getTransformation = function() { |
448 return 'scale(' + (1 / window.devicePixelRatio * this.zoom_) + ')'; | 431 return 'translate(' + this.offsetX_ + 'px, ' + this.offsetY_ + 'px) ' + |
| 432 'scale(' + (1 / window.devicePixelRatio * this.zoom_) + ')'; |
449 }; | 433 }; |
450 | 434 |
451 /** | 435 /** |
452 * Obtains shift CSS transformation for the screen image. | 436 * Obtains shift CSS transformation for the screen image. |
453 * @param {number} dx Amount of shift. | 437 * @param {number} dx Amount of shift. |
454 * @return {string} Transformation description. | 438 * @return {string} Transformation description. |
455 */ | 439 */ |
456 Viewport.prototype.getShiftTransformation = function(dx) { | 440 Viewport.prototype.getShiftTransformation = function(dx) { |
457 return 'translateX(' + dx + 'px) ' + this.getTransformation(); | 441 return 'translateX(' + dx + 'px) ' + this.getTransformation(); |
458 }; | 442 }; |
459 | 443 |
460 /** | 444 /** |
461 * Obtains CSS transformation that makes the rotated image fit the original | 445 * Obtains CSS transformation that makes the rotated image fit the original |
462 * image. The new rotated image that the transformation is applied to looks the | 446 * image. The new rotated image that the transformation is applied to looks the |
463 * same with original image. | 447 * same with original image. |
464 * | 448 * |
465 * @param {boolean} orientation Orientation of the rotation from the original | 449 * @param {boolean} orientation Orientation of the rotation from the original |
466 * image to the rotated image. True is for clockwise and false is for | 450 * image to the rotated image. True is for clockwise and false is for |
467 * counterclockwise. | 451 * counterclockwise. |
468 * @return {string} Transformation description. | 452 * @return {string} Transformation description. |
469 */ | 453 */ |
470 Viewport.prototype.getInverseTransformForRotatedImage = function(orientation) { | 454 Viewport.prototype.getInverseTransformForRotatedImage = function(orientation) { |
471 var previousImageWidth = this.imageBounds_.height; | 455 var previousImageWidth = this.imageBounds_.height; |
472 var previousImageHeight = this.imageBounds_.width; | 456 var previousImageHeight = this.imageBounds_.width; |
473 var oldScale = this.getFittingScaleForImageSize_( | 457 var oldScale = this.getFittingScaleForImageSize_( |
474 previousImageWidth, previousImageHeight); | 458 previousImageWidth, previousImageHeight); |
475 var scaleRatio = oldScale / this.getScale(); | 459 var scaleRatio = oldScale / this.scale_; |
476 var degree = orientation ? '-90deg' : '90deg'; | 460 var degree = orientation ? '-90deg' : '90deg'; |
477 return [ | 461 return [ |
478 'scale(' + scaleRatio + ')', | 462 'scale(' + scaleRatio + ')', |
479 'rotate(' + degree + ')', | 463 'rotate(' + degree + ')', |
480 this.getTransformation() | 464 this.getTransformation() |
481 ].join(' '); | 465 ].join(' '); |
482 }; | 466 }; |
483 | 467 |
484 /** | 468 /** |
485 * Obtains CSS transformation that makes the cropped image fit the original | 469 * Obtains CSS transformation that makes the cropped image fit the original |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 var screenWidth = this.screenBounds_.width; | 507 var screenWidth = this.screenBounds_.width; |
524 var screenHeight = this.screenBounds_.height; | 508 var screenHeight = this.screenBounds_.height; |
525 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; | 509 var dx = screenRect.left + screenRect.width / 2 - screenWidth / 2; |
526 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; | 510 var dy = screenRect.top + screenRect.height / 2 - screenHeight / 2; |
527 return [ | 511 return [ |
528 'translate(' + dx + 'px,' + dy + 'px)', | 512 'translate(' + dx + 'px,' + dy + 'px)', |
529 'scale(' + scaleX + ',' + scaleY + ')', | 513 'scale(' + scaleX + ',' + scaleY + ')', |
530 this.getTransformation() | 514 this.getTransformation() |
531 ].join(' '); | 515 ].join(' '); |
532 }; | 516 }; |
OLD | NEW |