Chromium Code Reviews| 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 * The overlay displaying the image. | 8 * The overlay displaying the image. |
| 9 * | 9 * |
| 10 * @param {HTMLElement} container The container element. | 10 * @param {HTMLElement} container The container element. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 this.setupDeviceBuffer(this.screenImage_); | 112 this.setupDeviceBuffer(this.screenImage_); |
| 113 | 113 |
| 114 forceRepaint = true; | 114 forceRepaint = true; |
| 115 } | 115 } |
| 116 | 116 |
| 117 if (forceRepaint || | 117 if (forceRepaint || |
| 118 this.displayedContentGeneration_ !== this.contentGeneration_) { | 118 this.displayedContentGeneration_ !== this.contentGeneration_) { |
| 119 this.displayedContentGeneration_ = this.contentGeneration_; | 119 this.displayedContentGeneration_ = this.contentGeneration_; |
| 120 | 120 |
| 121 ImageUtil.trace.resetTimer('paint'); | 121 ImageUtil.trace.resetTimer('paint'); |
| 122 this.paintDeviceRect(this.viewport_.getDeviceClipped(), | 122 this.paintDeviceRect(this.contentCanvas_, new Rect(this.contentCanvas_)); |
| 123 this.contentCanvas_, this.viewport_.getImageClipped()); | |
| 124 ImageUtil.trace.reportTimer('paint'); | 123 ImageUtil.trace.reportTimer('paint'); |
| 125 } | 124 } |
| 126 }; | 125 }; |
| 127 | 126 |
| 128 /** | 127 /** |
| 129 * Applies the viewport change that does not affect the screen cache size (zoom | 128 * Applies the viewport change that does not affect the screen cache size (zoom |
| 130 * change or offset change) with animation. | 129 * change or offset change) with animation. |
| 131 */ | 130 */ |
| 132 ImageView.prototype.applyViewportChange = function() { | 131 ImageView.prototype.applyViewportChange = function() { |
| 133 this.setTransform( | 132 this.setTransform( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 * @return {number} The content revision number. | 170 * @return {number} The content revision number. |
| 172 */ | 171 */ |
| 173 ImageView.prototype.getContentRevision = function() { | 172 ImageView.prototype.getContentRevision = function() { |
| 174 return this.contentRevision_; | 173 return this.contentRevision_; |
| 175 }; | 174 }; |
| 176 | 175 |
| 177 /** | 176 /** |
| 178 * Copies an image fragment from a full resolution canvas to a device resolution | 177 * Copies an image fragment from a full resolution canvas to a device resolution |
| 179 * canvas. | 178 * canvas. |
| 180 * | 179 * |
| 181 * @param {Rect} deviceRect Rectangle in the device coordinates. | 180 * @param {HTMLCanvasElement} canvas Canvas containing whole image. The canvas |
| 182 * @param {HTMLCanvasElement} canvas Full resolution canvas. | 181 * may not be fullresolution (scaled). |
|
mtomasz
2014/07/17 05:43:20
nit: fullresolution -> full resolution
hirono
2014/07/17 05:56:47
Done.
| |
| 183 * @param {Rect} imageRect Rectangle in the full resolution canvas. | 182 * @param {Rect} imageRect Rectangle region of the canvas to be rendered. |
| 184 */ | 183 */ |
| 185 ImageView.prototype.paintDeviceRect = function(deviceRect, canvas, imageRect) { | 184 ImageView.prototype.paintDeviceRect = function(canvas, imageRect) { |
| 186 // Map screen canvas (0,0) to (deviceBounds.left, deviceBounds.top) | 185 // Check canvas size. |
| 187 var deviceBounds = this.viewport_.getDeviceClipped(); | 186 var deviceBounds = this.viewport_.getDeviceBounds(); |
| 188 deviceRect = deviceRect.shift(-deviceBounds.left, -deviceBounds.top); | 187 if (this.screenImage_.width != deviceBounds.width || |
| 188 this.screenImage_.height != deviceBounds.height) { | |
| 189 console.error('The size of canvas is invalid.', (new Error).stack); | |
| 190 return; | |
| 191 } | |
| 189 | 192 |
| 190 // The source canvas may have different physical size than the image size | 193 // Map the rectangle in full resolution image to the rectangle in the device |
| 191 // set at the viewport. Adjust imageRect accordingly. | 194 // canvas. |
| 192 var bounds = this.viewport_.getImageBounds(); | 195 var scaleX = deviceBounds.width / canvas.width; |
| 193 var scaleX = canvas.width / bounds.width; | 196 var scaleY = deviceBounds.height / canvas.height; |
| 194 var scaleY = canvas.height / bounds.height; | 197 var deviceRect = new Rect( |
| 195 imageRect = new Rect(imageRect.left * scaleX, imageRect.top * scaleY, | 198 imageRect.left * scaleX, |
| 196 imageRect.width * scaleX, imageRect.height * scaleY); | 199 imageRect.top * scaleY, |
| 200 imageRect.width * scaleX, | |
| 201 imageRect.height * scaleY); | |
| 202 | |
| 197 Rect.drawImage( | 203 Rect.drawImage( |
| 198 this.screenImage_.getContext('2d'), canvas, deviceRect, imageRect); | 204 this.screenImage_.getContext('2d'), canvas, deviceRect, imageRect); |
| 199 }; | 205 }; |
| 200 | 206 |
| 201 /** | 207 /** |
| 202 * Creates an overlay canvas with properties similar to the screen canvas. | 208 * Creates an overlay canvas with properties similar to the screen canvas. |
| 203 * Useful for showing quick feedback when editing. | 209 * Useful for showing quick feedback when editing. |
| 204 * | 210 * |
| 205 * @return {HTMLCanvasElement} Overlay canvas. | 211 * @return {HTMLCanvasElement} Overlay canvas. |
| 206 */ | 212 */ |
| 207 ImageView.prototype.createOverlayCanvas = function() { | 213 ImageView.prototype.createOverlayCanvas = function() { |
| 208 var canvas = this.document_.createElement('canvas'); | 214 var canvas = this.document_.createElement('canvas'); |
| 209 canvas.className = 'image'; | 215 canvas.className = 'image'; |
| 210 this.container_.appendChild(canvas); | 216 this.container_.appendChild(canvas); |
| 211 return canvas; | 217 return canvas; |
| 212 }; | 218 }; |
| 213 | 219 |
| 214 /** | 220 /** |
| 215 * Sets up the canvas as a buffer in the device resolution. | 221 * Sets up the canvas as a buffer in the device resolution. |
| 216 * | 222 * |
| 217 * @param {HTMLCanvasElement} canvas The buffer canvas. | 223 * @param {HTMLCanvasElement} canvas The buffer canvas. |
| 218 */ | 224 */ |
| 219 ImageView.prototype.setupDeviceBuffer = function(canvas) { | 225 ImageView.prototype.setupDeviceBuffer = function(canvas) { |
| 220 var deviceRect = this.viewport_.getDeviceClipped(); | |
| 221 | |
| 222 // Set the canvas position and size in device pixels. | 226 // Set the canvas position and size in device pixels. |
| 227 var deviceRect = this.viewport_.getDeviceBounds(); | |
| 223 if (canvas.width !== deviceRect.width) | 228 if (canvas.width !== deviceRect.width) |
| 224 canvas.width = deviceRect.width; | 229 canvas.width = deviceRect.width; |
| 225 | |
| 226 if (canvas.height !== deviceRect.height) | 230 if (canvas.height !== deviceRect.height) |
| 227 canvas.height = deviceRect.height; | 231 canvas.height = deviceRect.height; |
| 228 | 232 |
| 229 canvas.style.left = deviceRect.left + 'px'; | 233 // Center the image. |
| 230 canvas.style.top = deviceRect.top + 'px'; | 234 var imageBoudns = this.viewport_.getImageElementBoundsOnScreen(); |
| 235 canvas.style.left = imageBoudns.left + 'px'; | |
| 236 canvas.style.top = imageBoudns.top + 'px'; | |
| 231 | 237 |
| 232 // Scale the canvas down to screen pixels. | 238 // Scale the canvas down to screen pixels. |
| 233 this.setTransform(canvas); | 239 this.setTransform(canvas); |
| 234 }; | 240 }; |
| 235 | 241 |
| 236 /** | 242 /** |
| 237 * @return {ImageData} A new ImageData object with a copy of the content. | 243 * @return {ImageData} A new ImageData object with a copy of the content. |
| 238 */ | 244 */ |
| 239 ImageView.prototype.copyScreenImageData = function() { | 245 ImageView.prototype.copyScreenImageData = function() { |
| 240 return this.screenImage_.getContext('2d').getImageData( | 246 return this.screenImage_.getContext('2d').getImageData( |
| (...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 962 }; | 968 }; |
| 963 | 969 |
| 964 ImageView.Effect.Rotate.prototype = { __proto__: ImageView.Effect.prototype }; | 970 ImageView.Effect.Rotate.prototype = { __proto__: ImageView.Effect.prototype }; |
| 965 | 971 |
| 966 /** | 972 /** |
| 967 * @override | 973 * @override |
| 968 */ | 974 */ |
| 969 ImageView.Effect.Rotate.prototype.transform = function(element, viewport) { | 975 ImageView.Effect.Rotate.prototype.transform = function(element, viewport) { |
| 970 return viewport.getInverseTransformForRotatedImage(this.orientation_); | 976 return viewport.getInverseTransformForRotatedImage(this.orientation_); |
| 971 }; | 977 }; |
| OLD | NEW |