From db4deb244f2819595e83035ac9d44fe15330270e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 8 Mar 2017 17:14:42 +0100 Subject: [PATCH] Fix calculation of internal bounds with children and applyMatrix = false Closes #1250 --- src/item/Item.js | 11 +++++++---- test/tests/Item_Bounds.js | 23 ++++++++++++++++++++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index 1da8c635..cc2c19e0 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -893,14 +893,14 @@ new function() { // Injection scope for various item event handlers * Private method that deals with the calling of _getBounds, recursive * matrix concatenation and handles all the complicated caching mechanisms. */ - _getCachedBounds: function(matrix, options) { + _getCachedBounds: function(matrix, options, noInternal) { // See if we can cache these bounds. We only cache the bounds // transformed with the internally stored _matrix, (the default if no // matrix is passed). matrix = matrix && matrix._orNullIfIdentity(); // Do not transform by the internal matrix for internal, untransformed // bounds. - var internal = options.internal, + var internal = options.internal && !noInternal, cacheItem = options.cacheItem, _matrix = internal ? null : this._matrix._orNullIfIdentity(), // Create a key for caching, reflecting all bounds options. @@ -923,7 +923,7 @@ new function() { // Injection scope for various item event handlers var cached = this._bounds[cacheKey] = { rect: bounds.clone(), // Mark as internal, so Item#transform() won't transform it - internal: options.internal + internal: internal }; } return bounds; @@ -1010,8 +1010,11 @@ new function() { // Injection scope for various item event handlers for (var i = 0, l = items.length; i < l; i++) { var item = items[i]; if (item._visible && !item.isEmpty()) { + // Pass true for noInternal, since even when getting + // internal bounds for this item, we need to apply the + // matrices to its children. var rect = item._getCachedBounds( - matrix && matrix.appended(item._matrix), options); + matrix && matrix.appended(item._matrix), options, true); x1 = Math.min(rect.x, x1); y1 = Math.min(rect.y, y1); x2 = Math.max(rect.x + rect.width, x2); diff --git a/test/tests/Item_Bounds.js b/test/tests/Item_Bounds.js index 521357bb..7cca697a 100644 --- a/test/tests/Item_Bounds.js +++ b/test/tests/Item_Bounds.js @@ -741,8 +741,7 @@ test('item.visible and item.parents.bounds (#1248)', function() { var item = new Path.Rectangle({ point: [0, 0], size: [50, 100], - fillColor: 'red', - visible: false, + visible: false }); equals(item.bounds, new Rectangle(0, 0, 50, 100), 'item.bounds'); equals(item.parent.bounds, new Rectangle(0, 0, 0, 0), @@ -750,4 +749,22 @@ test('item.visible and item.parents.bounds (#1248)', function() { item.visible = true; equals(item.parent.bounds, item.bounds, 'item.parent.bounds with item.visible = true'); -}) +}); + +test('group.internalBounds with child and child.applyMatrix = false (#1250)', function() { + var item1 = Shape.Rectangle({ + point: [100, 100], + size: [200, 200] + }); + var item2 = new Path.Rectangle({ + point: [0, 0], + size: [100, 100] + }); + var group = new Group([item1, item2]); + equals(item1.bounds, new Rectangle(100, 100, 200, 200), 'item.bounds'); + equals(group.internalBounds, new Rectangle(0, 0, 300, 300), + 'group.internalBounds before scaling item1'); + item1.scale(0.5); + equals(group.internalBounds, new Rectangle(0, 0, 250, 250), + 'group.internalBounds after scaling item1'); +});