From 37f5a64dce3f52c5459ed03053ebe92eed269f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 20 Jun 2011 17:34:43 +0100 Subject: [PATCH] Improve _bounds handling by introducing Item#_createBounds() that always uses LinkedRectangle class. --- src/item/Item.js | 14 ++++++++++---- src/item/PlacedSymbol.js | 8 +++----- src/item/Raster.js | 10 +++++----- src/path/Path.js | 9 ++++----- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index 721382d0..f6f0b8c4 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1079,10 +1079,16 @@ var Item = this.Item = Base.extend({ y2 = Math.max(rect.y + rect.height, y2); } } - return includeStroke - ? Rectangle.create(x1, y1, x2 - x1, y2 - y1) - : LinkedRectangle.create(this, 'setBounds', - x1, y1, x2 - x1, y2 - y1); + var bounds = Rectangle.create(x1, y1, x2 - x1, y2 - y1); + return includeStroke ? bounds : this._createBounds(bounds); + }, + + /** + * Creates a LinkedRectangle that when modified calls #setBounds(). + */ + _createBounds: function(rect) { + return LinkedRectangle.create(this, 'setBounds', + rect.x, rect.y, rect.width, rect.height) }, setBounds: function(rect) { diff --git a/src/item/PlacedSymbol.js b/src/item/PlacedSymbol.js index bf392288..43ae21c5 100644 --- a/src/item/PlacedSymbol.js +++ b/src/item/PlacedSymbol.js @@ -90,11 +90,9 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({ }, getBounds: function() { - if (!this._bounds) { - var bounds = this.symbol._definition.getStrokeBounds(this.matrix); - this._bounds = LinkedRectangle.create(this, 'setBounds', - bounds.x, bounds.y, bounds.width, bounds.height); - } + if (!this._bounds) + this._bounds = this._createBounds( + this.symbol._definition.getStrokeBounds(this.matrix)) return this._bounds; }, diff --git a/src/item/Raster.js b/src/item/Raster.js index ee14f1e2..768667b0 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -148,6 +148,7 @@ var Raster = this.Raster = Item.extend({ this._size = new Size(canvas.width, canvas.height); this._image = null; this._context = null; + // TODO: _changed() this._bounds = null; }, @@ -170,6 +171,7 @@ var Raster = this.Raster = Item.extend({ this._size = new Size(image.naturalWidth, image.naturalHeight); this._canvas = null; this._context = null; + // TODO: _changed() this._bounds = null; }, @@ -368,14 +370,12 @@ var Raster = this.Raster = Item.extend({ // raster, simply preconcatenate the internal matrix with the provided // one. this.matrix.preConcatenate(matrix); - this._bounds = null; }, getBounds: function() { - if (!this._bounds) { - this._bounds = this.matrix._transformBounds( - new Rectangle(this._size).setCenter(0, 0)); - } + if (!this._bounds) + this._bounds = tis._createBounds(this.matrix._transformBounds( + new Rectangle(this._size).setCenter(0, 0))); return this._bounds; }, diff --git a/src/path/Path.js b/src/path/Path.js index fbd69922..c6eb6526 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1766,11 +1766,10 @@ var Path = this.Path = PathItem.extend({ // Pass the matrix hidden from Bootstrap, so it still inject // getBounds as bean too. if (!useCache || !this._bounds) { - var bounds = getBounds(this, arguments[0]); - if (!useCache) - return bounds; - this._bounds = LinkedRectangle.create(this, 'setBounds', - bounds.x, bounds.y, bounds.width, bounds.height); + var bounds = this._createBounds(getBounds(this, arguments[0])); + if (useCache) + this._bounds = bounds; + return bounds; } return this._bounds; },