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
This commit is contained in:
Jeff Avallone 2015-04-16 17:13:12 -04:00
parent 5601c6a398
commit 5917d2b035
8 changed files with 30 additions and 45 deletions

View File

@ -67,19 +67,11 @@ describe('parser/javascript/charset.js', function() {
cy: 20 cy: 20
}); });
node.container = jasmine.createSpyObj('container', ['addClass', 'getBBox']);
node.container.getBBox.and.returnValue({
x: 10,
x2: 15
});
spyOn(node, 'transform').and.returnValue({ spyOn(node, 'transform').and.returnValue({
localMatrix: Snap.matrix().translate(3, 8) localMatrix: Snap.matrix().translate(3, 8)
}); });
expect(node._anchor).toEqual({ expect(node._anchor).toEqual({
ax: 10,
ax2: 15,
ay: 28 ay: 28
}); });
}); });

View File

@ -31,10 +31,12 @@ describe('parser/javascript/match_fragment.js', function() {
this.node = new javascript.Parser('a').__consume__match_fragment(); this.node = new javascript.Parser('a').__consume__match_fragment();
this.node.content = { this.node.content = {
anchor: { getBBox() {
return {
ax: 1, ax: 1,
ax2: 2, ax2: 2,
ay: 3 ay: 3
};
} }
}; };
spyOn(this.node, 'transform').and.returnValue({ spyOn(this.node, 'transform').and.returnValue({

View File

@ -63,18 +63,9 @@ describe('parser/javascript/node.js', function() {
describe('when a proxy node is not used', 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._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({ expect(this.node.anchor).toEqual({
ax: 'bbox x',
ax2: 'bbox x2',
ay: 'bbox cy',
example: 'value' example: 'value'
}); });
}); });
@ -85,7 +76,7 @@ describe('parser/javascript/node.js', function() {
describe('#getBBox', 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 = { this.node.proxy = {
anchor: { anchor: {
anchor: 'example 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 = jasmine.createSpyObj('container', ['addClass', 'getBBox']);
this.node.container.getBBox.and.returnValue({ this.node.container.getBBox.and.returnValue({
bbox: 'example bbox' bbox: 'example bbox',
x: 'left',
x2: 'right',
cy: 'center'
}); });
expect(this.node.getBBox()).toEqual({ expect(this.node.getBBox()).toEqual({
bbox: 'example bbox', bbox: 'example bbox',
anchor: 'example anchor' anchor: 'example anchor',
x: 'left',
x2: 'right',
cy: 'center',
ax: 'left',
ax2: 'right',
ay: 'center'
}); });
}); });

View File

@ -36,10 +36,12 @@ describe('parser/javascript/subexp.js', function() {
var node = new javascript.Parser('(test)').__consume__subexp(); var node = new javascript.Parser('(test)').__consume__subexp();
node.regexp = { node.regexp = {
anchor: { getBBox() {
return {
ax: 10, ax: 10,
ax2: 15, ax2: 15,
ay: 20 ay: 20
};
} }
}; };

View File

@ -6,12 +6,9 @@ export default {
definedProperties: { definedProperties: {
_anchor: { _anchor: {
get: function() { get: function() {
var box = this.container.getBBox(), var matrix = this.transform().localMatrix;
matrix = this.transform().localMatrix;
return { return {
ax: box.x,
ax2: box.x2,
ay: matrix.y(0, this.partContainer.getBBox().cy) ay: matrix.y(0, this.partContainer.getBBox().cy)
}; };
} }

View File

@ -6,7 +6,7 @@ export default {
definedProperties: { definedProperties: {
_anchor: { _anchor: {
get: function() { get: function() {
var anchor = this.content.anchor, var anchor = this.content.getBBox(),
matrix = this.transform().localMatrix; matrix = this.transform().localMatrix;
return { return {

View File

@ -34,23 +34,15 @@ export default class Node {
} }
get anchor() { get anchor() {
var box;
if (this.proxy) { if (this.proxy) {
return this.proxy.anchor; return this.proxy.anchor;
} else { } else {
box = this.container.getBBox(); return this._anchor || {};
return _.extend({
ax: box.x,
ax2: box.x2,
ay: box.cy
}, this._anchor || {});
} }
} }
getBBox() { getBBox() {
return _.extend(this.container.getBBox(), this.anchor); return _.extend(util.normalizeBBox(this.container.getBBox()), this.anchor);
} }
transform(matrix) { transform(matrix) {

View File

@ -6,7 +6,7 @@ export default {
definedProperties: { definedProperties: {
_anchor: { _anchor: {
get: function() { get: function() {
var anchor = this.regexp.anchor, var anchor = this.regexp.getBBox(),
matrix = this.transform().localMatrix; matrix = this.transform().localMatrix;
return { return {