From 5981ddf89f57344161d5567553ce053e474ff1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 14 Aug 2013 12:14:28 -0700 Subject: [PATCH] Implement Raster#getSubRaster() and document both #getSubImage() and #getSubRaster(). --- src/item/Raster.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/item/Raster.js b/src/item/Raster.js index c5dc5da6..17e0a1e9 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -250,8 +250,9 @@ var Raster = Item.extend(/** @lends Raster# */{ /** * The source of the raster, which can be set using a DOM Image, a Canvas, * a data url, a string describing the URL to load the image from, or the - * ID of a DOM element to get the image from (either a DOM Image or a Canvas). - * Reading this property will return the url of the source image or a data-url. + * ID of a DOM element to get the image from (either a DOM Image or a + * Canvas). Reading this property will return the url of the source image or + * a data-url. * * @bean * @type HTMLImageElement|Canvas|String @@ -320,12 +321,14 @@ var Raster = Item.extend(/** @lends Raster# */{ return this._canvas || this._image; }, - // DOCS: document Raster#getSubImage /** + * Extracts a part of the Raster's content as a sub image, and returns it as + * a Canvas object. + * * @param {Rectangle} rect the boundaries of the sub image in pixel * coordinates * - * @return {Canvas} + * @return {Canvas} the sub image as a Canvas object */ getSubImage: function(rect) { rect = Rectangle.read(arguments); @@ -335,6 +338,23 @@ var Raster = Item.extend(/** @lends Raster# */{ return ctx.canvas; }, + /** + * Extracts a part of the raster item's content as a new raster item, placed + * in exactly the same place as the original content. + * + * @param {Rectangle} rect the boundaries of the sub raster in pixel + * coordinates + * + * @return {Raster} the sub raster as a newly created raster item + */ + getSubRaster: function(rect) { + rect = Rectangle.read(arguments); + var raster = new Raster(this.getSubImage(rect)); + raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); + raster._matrix.preConcatenate(this._matrix); + return raster; + }, + /** * Returns a Base 64 encoded {@code data:} URL representation of the raster. *