| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 /** | 5 /** |
| 6 * Viewport class controls the way the image is displayed (scale, offset etc). | 6 * Viewport class controls the way the image is displayed (scale, offset etc). |
| 7 */ | 7 */ |
| 8 function Viewport(repaintCallback) { | 8 function Viewport(repaintCallback) { |
| 9 this.repaintCallback_ = repaintCallback; | 9 this.repaintCallback_ = repaintCallback; |
| 10 | 10 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 * @return {Boolean} True if some part of the image is clipped by the screen. | 206 * @return {Boolean} True if some part of the image is clipped by the screen. |
| 207 */ | 207 */ |
| 208 Viewport.prototype.isClipped = function () { | 208 Viewport.prototype.isClipped = function () { |
| 209 return this.getMarginX_() < 0 || this.getMarginY_() < 0; | 209 return this.getMarginX_() < 0 || this.getMarginY_() < 0; |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 /** | 212 /** |
| 213 * Horizontal margin. Negative if the image is clipped horizontally. | 213 * Horizontal margin. Negative if the image is clipped horizontally. |
| 214 */ | 214 */ |
| 215 Viewport.prototype.getMarginX_ = function() { | 215 Viewport.prototype.getMarginX_ = function() { |
| 216 return Math.floor( | 216 return Math.round( |
| 217 (this.screenBounds_.width - this.imageBounds_.width * this.scale_) / 2); | 217 (this.screenBounds_.width - this.imageBounds_.width * this.scale_) / 2); |
| 218 }; | 218 }; |
| 219 | 219 |
| 220 /** | 220 /** |
| 221 * Vertical margin. Negative if the image is clipped vertically. | 221 * Vertical margin. Negative if the image is clipped vertically. |
| 222 */ | 222 */ |
| 223 Viewport.prototype.getMarginY_ = function() { | 223 Viewport.prototype.getMarginY_ = function() { |
| 224 return Math.floor( | 224 return Math.round( |
| 225 (this.screenBounds_.height - this.imageBounds_.height * this.scale_) / 2); | 225 (this.screenBounds_.height - this.imageBounds_.height * this.scale_) / 2); |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 Viewport.prototype.clampOffsetX_ = function(x) { | 228 Viewport.prototype.clampOffsetX_ = function(x) { |
| 229 var limit = Math.max(0, -this.getMarginX_() / this.getScale()); | 229 var limit = Math.round(Math.max(0, -this.getMarginX_() / this.getScale())); |
| 230 return ImageUtil.clamp(-limit, x, limit); | 230 return ImageUtil.clamp(-limit, x, limit); |
| 231 }; | 231 }; |
| 232 | 232 |
| 233 Viewport.prototype.clampOffsetY_ = function(y) { | 233 Viewport.prototype.clampOffsetY_ = function(y) { |
| 234 var limit = Math.max(0, -this.getMarginY_() / this.getScale()); | 234 var limit = Math.round(Math.max(0, -this.getMarginY_() / this.getScale())); |
| 235 return ImageUtil.clamp(-limit, y, limit); | 235 return ImageUtil.clamp(-limit, y, limit); |
| 236 }; | 236 }; |
| 237 | 237 |
| 238 /** | 238 /** |
| 239 * Recalculate the viewport parameters. | 239 * Recalculate the viewport parameters. |
| 240 */ | 240 */ |
| 241 Viewport.prototype.update = function() { | 241 Viewport.prototype.update = function() { |
| 242 var scale = this.getScale(); | 242 var scale = this.getScale(); |
| 243 | 243 |
| 244 // Image bounds in screen coordinates. | 244 // Image bounds in screen coordinates. |
| 245 this.imageOnScreen_ = new Rect( | 245 this.imageOnScreen_ = new Rect( |
| 246 this.getMarginX_(), | 246 this.getMarginX_(), |
| 247 this.getMarginY_(), | 247 this.getMarginY_(), |
| 248 Math.floor(this.imageBounds_.width * scale), | 248 Math.round(this.imageBounds_.width * scale), |
| 249 Math.floor(this.imageBounds_.height * scale)); | 249 Math.round(this.imageBounds_.height * scale)); |
| 250 | 250 |
| 251 // A visible part of the image in image coordinates. | 251 // A visible part of the image in image coordinates. |
| 252 this.imageClipped_ = new Rect(this.imageBounds_); | 252 this.imageClipped_ = new Rect(this.imageBounds_); |
| 253 | 253 |
| 254 // A visible part of the image in screen coordinates. | 254 // A visible part of the image in screen coordinates. |
| 255 this.screenClipped_ = new Rect(this.screenBounds_); | 255 this.screenClipped_ = new Rect(this.screenBounds_); |
| 256 | 256 |
| 257 // Adjust for the offset. | 257 // Adjust for the offset. |
| 258 if (this.imageOnScreen_.left < 0) { | 258 if (this.imageOnScreen_.left < 0) { |
| 259 this.imageOnScreen_.left += this.clampOffsetX_(this.offsetX_) * scale; | 259 this.imageOnScreen_.left += |
| 260 this.imageClipped_.left = -this.imageOnScreen_.left / scale; | 260 Math.round(this.clampOffsetX_(this.offsetX_) * scale); |
| 261 this.imageClipped_.width = this.screenBounds_.width / scale; | 261 this.imageClipped_.left = Math.round(-this.imageOnScreen_.left / scale); |
| 262 this.imageClipped_.width = Math.round(this.screenBounds_.width / scale); |
| 262 } else { | 263 } else { |
| 263 this.screenClipped_.left = this.imageOnScreen_.left; | 264 this.screenClipped_.left = this.imageOnScreen_.left; |
| 264 this.screenClipped_.width = this.imageOnScreen_.width; | 265 this.screenClipped_.width = this.imageOnScreen_.width; |
| 265 } | 266 } |
| 266 | 267 |
| 267 if (this.imageOnScreen_.top < 0) { | 268 if (this.imageOnScreen_.top < 0) { |
| 268 this.imageOnScreen_.top += this.clampOffsetY_(this.offsetY_) * scale; | 269 this.imageOnScreen_.top += |
| 269 this.imageClipped_.top = -this.imageOnScreen_.top / scale; | 270 Math.round(this.clampOffsetY_(this.offsetY_) * scale); |
| 270 this.imageClipped_.height = this.screenBounds_.height / scale; | 271 this.imageClipped_.top = Math.round(-this.imageOnScreen_.top / scale); |
| 272 this.imageClipped_.height = Math.round(this.screenBounds_.height / scale); |
| 271 } else { | 273 } else { |
| 272 this.screenClipped_.top = this.imageOnScreen_.top; | 274 this.screenClipped_.top = this.imageOnScreen_.top; |
| 273 this.screenClipped_.height = this.imageOnScreen_.height; | 275 this.screenClipped_.height = this.imageOnScreen_.height; |
| 274 } | 276 } |
| 275 }; | 277 }; |
| 276 | 278 |
| 277 Viewport.prototype.repaint = function () { | 279 Viewport.prototype.repaint = function () { |
| 278 if (this.repaintCallback_) this.repaintCallback_(); | 280 if (this.repaintCallback_) this.repaintCallback_(); |
| 279 }; | 281 }; |
| OLD | NEW |