From 5917d2b0358aaa6edc0a17d93360164370211111 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Thu, 16 Apr 2015 17:13:12 -0400 Subject: [PATCH] Updating getBBox and anchor code to improve performance This change will reduce the number of calls to this.container.getBBox when calculating the bounding box of a node --- spec/parser/javascript/charset_spec.js | 8 ------ spec/parser/javascript/match_fragment_spec.js | 10 ++++--- spec/parser/javascript/node_spec.js | 26 +++++++++---------- spec/parser/javascript/subexp_spec.js | 10 ++++--- src/js/parser/javascript/charset.js | 5 +--- src/js/parser/javascript/match_fragment.js | 2 +- src/js/parser/javascript/node.js | 12 ++------- src/js/parser/javascript/subexp.js | 2 +- 8 files changed, 30 insertions(+), 45 deletions(-) diff --git a/spec/parser/javascript/charset_spec.js b/spec/parser/javascript/charset_spec.js index 51de74a..1a11cd9 100644 --- a/spec/parser/javascript/charset_spec.js +++ b/spec/parser/javascript/charset_spec.js @@ -67,19 +67,11 @@ describe('parser/javascript/charset.js', function() { cy: 20 }); - node.container = jasmine.createSpyObj('container', ['addClass', 'getBBox']); - node.container.getBBox.and.returnValue({ - x: 10, - x2: 15 - }); - spyOn(node, 'transform').and.returnValue({ localMatrix: Snap.matrix().translate(3, 8) }); expect(node._anchor).toEqual({ - ax: 10, - ax2: 15, ay: 28 }); }); diff --git a/spec/parser/javascript/match_fragment_spec.js b/spec/parser/javascript/match_fragment_spec.js index 4a78807..2ae23b4 100644 --- a/spec/parser/javascript/match_fragment_spec.js +++ b/spec/parser/javascript/match_fragment_spec.js @@ -31,10 +31,12 @@ describe('parser/javascript/match_fragment.js', function() { this.node = new javascript.Parser('a').__consume__match_fragment(); this.node.content = { - anchor: { - ax: 1, - ax2: 2, - ay: 3 + getBBox() { + return { + ax: 1, + ax2: 2, + ay: 3 + }; } }; spyOn(this.node, 'transform').and.returnValue({ diff --git a/spec/parser/javascript/node_spec.js b/spec/parser/javascript/node_spec.js index e45a07d..5367de8 100644 --- a/spec/parser/javascript/node_spec.js +++ b/spec/parser/javascript/node_spec.js @@ -63,18 +63,9 @@ describe('parser/javascript/node.js', function() { describe('when a proxy node is not used', function() { - it('returns anchor data from the bbox of the container merged _anchor of the node', function() { + it('returns _anchor of the node', function() { this.node._anchor = { example: 'value' }; - this.node.container = jasmine.createSpyObj('container', ['addClass', 'getBBox']); - this.node.container.getBBox.and.returnValue({ - x: 'bbox x', - x2: 'bbox x2', - cy: 'bbox cy' - }); expect(this.node.anchor).toEqual({ - ax: 'bbox x', - ax2: 'bbox x2', - ay: 'bbox cy', example: 'value' }); }); @@ -85,7 +76,7 @@ describe('parser/javascript/node.js', function() { describe('#getBBox', function() { - it('returns the bbox of the container merged with the anchor', function() { + it('returns the normalized bbox of the container merged with the anchor', function() { this.node.proxy = { anchor: { anchor: 'example anchor' @@ -93,11 +84,20 @@ describe('parser/javascript/node.js', function() { }; this.node.container = jasmine.createSpyObj('container', ['addClass', 'getBBox']); this.node.container.getBBox.and.returnValue({ - bbox: 'example bbox' + bbox: 'example bbox', + x: 'left', + x2: 'right', + cy: 'center' }); expect(this.node.getBBox()).toEqual({ bbox: 'example bbox', - anchor: 'example anchor' + anchor: 'example anchor', + x: 'left', + x2: 'right', + cy: 'center', + ax: 'left', + ax2: 'right', + ay: 'center' }); }); diff --git a/spec/parser/javascript/subexp_spec.js b/spec/parser/javascript/subexp_spec.js index 94063d7..43b6281 100644 --- a/spec/parser/javascript/subexp_spec.js +++ b/spec/parser/javascript/subexp_spec.js @@ -36,10 +36,12 @@ describe('parser/javascript/subexp.js', function() { var node = new javascript.Parser('(test)').__consume__subexp(); node.regexp = { - anchor: { - ax: 10, - ax2: 15, - ay: 20 + getBBox() { + return { + ax: 10, + ax2: 15, + ay: 20 + }; } }; diff --git a/src/js/parser/javascript/charset.js b/src/js/parser/javascript/charset.js index b2e2b6a..35192eb 100644 --- a/src/js/parser/javascript/charset.js +++ b/src/js/parser/javascript/charset.js @@ -6,12 +6,9 @@ export default { definedProperties: { _anchor: { get: function() { - var box = this.container.getBBox(), - matrix = this.transform().localMatrix; + var matrix = this.transform().localMatrix; return { - ax: box.x, - ax2: box.x2, ay: matrix.y(0, this.partContainer.getBBox().cy) }; } diff --git a/src/js/parser/javascript/match_fragment.js b/src/js/parser/javascript/match_fragment.js index d37d17d..0c1730b 100644 --- a/src/js/parser/javascript/match_fragment.js +++ b/src/js/parser/javascript/match_fragment.js @@ -6,7 +6,7 @@ export default { definedProperties: { _anchor: { get: function() { - var anchor = this.content.anchor, + var anchor = this.content.getBBox(), matrix = this.transform().localMatrix; return { diff --git a/src/js/parser/javascript/node.js b/src/js/parser/javascript/node.js index 4a7758b..722ff4c 100644 --- a/src/js/parser/javascript/node.js +++ b/src/js/parser/javascript/node.js @@ -34,23 +34,15 @@ export default class Node { } get anchor() { - var box; - if (this.proxy) { return this.proxy.anchor; } else { - box = this.container.getBBox(); - - return _.extend({ - ax: box.x, - ax2: box.x2, - ay: box.cy - }, this._anchor || {}); + return this._anchor || {}; } } getBBox() { - return _.extend(this.container.getBBox(), this.anchor); + return _.extend(util.normalizeBBox(this.container.getBBox()), this.anchor); } transform(matrix) { diff --git a/src/js/parser/javascript/subexp.js b/src/js/parser/javascript/subexp.js index 7ac1042..4ef405a 100644 --- a/src/js/parser/javascript/subexp.js +++ b/src/js/parser/javascript/subexp.js @@ -6,7 +6,7 @@ export default { definedProperties: { _anchor: { get: function() { - var anchor = this.regexp.anchor, + var anchor = this.regexp.getBBox(), matrix = this.transform().localMatrix; return {