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 * The ImageBuffer object holds an offscreen canvas object and | 6 * The ImageBuffer object holds an offscreen canvas object and |
7 * draws its content on the screen canvas applying scale and offset. | 7 * draws its content on the screen canvas applying scale and offset. |
8 * Supports pluggable overlays that modify the image appearance and behavior. | 8 * Supports pluggable overlays that modify the image appearance and behavior. |
9 * @constructor | 9 * @constructor |
10 */ | 10 */ |
11 function ImageBuffer(screenCanvas) { | 11 function ImageBuffer(screenCanvas) { |
12 this.screenCanvas_ = screenCanvas; | 12 this.screenCanvas_ = screenCanvas; |
13 | 13 |
14 this.viewport_ = new Viewport(this.repaint.bind(this)); | 14 this.viewport_ = new Viewport(this.repaint.bind(this)); |
15 this.viewport_.setScreenSize(screenCanvas.width, screenCanvas.height); | 15 this.viewport_.setScreenSize(screenCanvas.width, screenCanvas.height); |
16 | 16 |
17 this.content_ = new ImageBuffer.Content( | 17 this.content_ = new ImageBuffer.Content( |
18 this.viewport_, screenCanvas.ownerDocument); | 18 this.viewport_, screenCanvas.ownerDocument); |
19 | 19 |
20 this.overlays_ = []; | 20 this.overlays_ = []; |
21 this.addOverlay(new ImageBuffer.Margin(this.viewport_)); | 21 this.addOverlay(new ImageBuffer.Margin(this.viewport_)); |
22 this.addOverlay(this.content_); | 22 this.addOverlay(this.content_); |
23 this.addOverlay(new ImageBuffer.Overview(this.viewport_, this.content_)); | 23 // TODO(dgozman): consider adding overview in v2. |
24 } | 24 } |
25 | 25 |
26 ImageBuffer.prototype.getViewport = function() { return this.viewport_ }; | 26 ImageBuffer.prototype.getViewport = function() { return this.viewport_ }; |
27 | 27 |
28 ImageBuffer.prototype.getContent = function() { return this.content_ }; | 28 ImageBuffer.prototype.getContent = function() { return this.content_ }; |
29 | 29 |
30 /** | 30 /** |
31 * Loads the new content. | 31 * Loads the new content. |
32 * A string parameter is treated as an image url. | 32 * A string parameter is treated as an image url. |
33 * @param {String|HTMLImageElement|HTMLCanvasElement} source | 33 * @param {String|HTMLImageElement|HTMLCanvasElement} source |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 this.viewport_ = viewport; | 179 this.viewport_ = viewport; |
180 }; | 180 }; |
181 | 181 |
182 ImageBuffer.Margin.prototype = {__proto__: ImageBuffer.Overlay.prototype}; | 182 ImageBuffer.Margin.prototype = {__proto__: ImageBuffer.Overlay.prototype}; |
183 | 183 |
184 // Draw below everything including the content. | 184 // Draw below everything including the content. |
185 ImageBuffer.Margin.prototype.getZIndex = function() { return -2 }; | 185 ImageBuffer.Margin.prototype.getZIndex = function() { return -2 }; |
186 | 186 |
187 ImageBuffer.Margin.prototype.draw = function(context) { | 187 ImageBuffer.Margin.prototype.draw = function(context) { |
188 context.save(); | 188 context.save(); |
189 context.fillStyle = '#F0F0F0'; | 189 context.fillStyle = '#000000'; |
190 context.strokeStyle = '#000000'; | 190 context.strokeStyle = '#000000'; |
191 Rect.fillBetween(context, | 191 Rect.fillBetween(context, |
192 this.viewport_.getImageBoundsOnScreen(), | 192 this.viewport_.getImageBoundsOnScreen(), |
193 this.viewport_.getScreenBounds()); | 193 this.viewport_.getScreenBounds()); |
194 | 194 |
195 Rect.outline(context, this.viewport_.getImageBoundsOnScreen()); | 195 Rect.outline(context, this.viewport_.getImageBoundsOnScreen()); |
196 context.restore(); | 196 context.restore(); |
197 }; | 197 }; |
198 | 198 |
199 /** | 199 /** |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 0, 0, opt_width || this.canvas_.width, opt_height || this.canvas_.height); | 295 0, 0, opt_width || this.canvas_.width, opt_height || this.canvas_.height); |
296 }; | 296 }; |
297 | 297 |
298 /** | 298 /** |
299 * @param {HTMLImageElement|HTMLCanvasElement} image | 299 * @param {HTMLImageElement|HTMLCanvasElement} image |
300 */ | 300 */ |
301 ImageBuffer.Content.prototype.load = function(image) { | 301 ImageBuffer.Content.prototype.load = function(image) { |
302 this.canvas_.width = image.width; | 302 this.canvas_.width = image.width; |
303 this.canvas_.height = image.height; | 303 this.canvas_.height = image.height; |
304 | 304 |
| 305 this.clear(); |
305 Rect.drawImage(this.canvas_.getContext("2d"), image); | 306 Rect.drawImage(this.canvas_.getContext("2d"), image); |
306 this.invalidateCaches(); | 307 this.invalidateCaches(); |
307 | 308 |
308 this.viewport_.setImageSize(image.width, image.height); | 309 this.viewport_.setImageSize(image.width, image.height); |
309 this.viewport_.fitImage(); | 310 this.viewport_.fitImage(); |
310 }; | 311 }; |
311 | 312 |
| 313 ImageBuffer.Content.prototype.clear = function() { |
| 314 var context = this.canvas_.getContext("2d"); |
| 315 context.globalAlpha = 1; |
| 316 context.fillStyle = '#FFFFFF'; |
| 317 Rect.fill(context, new Rect(this.canvas_)); |
| 318 }; |
| 319 |
312 /** | 320 /** |
313 * @param {ImageData} imageData | 321 * @param {ImageData} imageData |
314 */ | 322 */ |
315 ImageBuffer.Content.prototype.drawImageData = function (imageData, x, y) { | 323 ImageBuffer.Content.prototype.drawImageData = function (imageData, x, y) { |
316 this.canvas_.getContext("2d").putImageData(imageData, x, y); | 324 this.canvas_.getContext("2d").putImageData(imageData, x, y); |
317 this.invalidateCaches(); | 325 this.invalidateCaches(); |
318 }; | 326 }; |
319 | 327 |
320 /** | 328 /** |
321 * The overview overlay draws the image thumbnail in the bottom right corner. | 329 * The overview overlay draws the image thumbnail in the bottom right corner. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 function scale() { return -self.scale_;} | 427 function scale() { return -self.scale_;} |
420 function hit(x, y) { return self.bounds_ && self.bounds_.inside(x, y); } | 428 function hit(x, y) { return self.bounds_ && self.bounds_.inside(x, y); } |
421 return this.viewport_.createOffsetSetter(x, y, scale, hit); | 429 return this.viewport_.createOffsetSetter(x, y, scale, hit); |
422 } else if (cursor == 'crosshair') { | 430 } else if (cursor == 'crosshair') { |
423 // Force non-draggable behavior. | 431 // Force non-draggable behavior. |
424 return function() {}; | 432 return function() {}; |
425 } else { | 433 } else { |
426 return null; | 434 return null; |
427 } | 435 } |
428 }; | 436 }; |
OLD | NEW |