From 17c3e0fa28c4d1f23f47751392a8938e352b3927 Mon Sep 17 00:00:00 2001 From: Flint O'Brien Date: Sun, 24 Apr 2016 16:56:32 -0400 Subject: [PATCH] Performance improvement for select.js and canvas.addToSelection canvas.addToSelection was calling getBBox, throwing it away, then calling selectorManager.requestSelector() which called getBBox again. Now passing-in an optional bbox to selector functions. --- editor/select.js | 25 ++++++++++++++++--------- editor/svgcanvas.js | 6 ++++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/editor/select.js b/editor/select.js index 4ac342ef..088c4ad7 100644 --- a/editor/select.js +++ b/editor/select.js @@ -33,7 +33,8 @@ var gripRadius = svgedit.browser.isTouch() ? 10 : 4; // Parameters: // id - integer to internally indentify the selector // elem - DOM element associated with this selector -svgedit.select.Selector = function(id, elem) { +// bbox - Optional bbox to use for initialization (prevents duplicate getBBox call). +svgedit.select.Selector = function(id, elem, bbox) { // this is the selector's unique number this.id = id; @@ -77,7 +78,7 @@ svgedit.select.Selector = function(id, elem) { 'w' : null }; - this.reset(this.selectedElement); + this.reset(this.selectedElement, bbox); }; @@ -86,10 +87,11 @@ svgedit.select.Selector = function(id, elem) { // // Parameters: // e - DOM element associated with this selector -svgedit.select.Selector.prototype.reset = function(e) { +// bbox - Optional bbox to use for reset (prevents duplicate getBBox call). +svgedit.select.Selector.prototype.reset = function(e, bbox) { this.locked = true; this.selectedElement = e; - this.resize(); + this.resize(bbox); this.selectorGroup.setAttribute('display', 'inline'); }; @@ -136,7 +138,8 @@ svgedit.select.Selector.prototype.showGrips = function(show) { // Function: svgedit.select.Selector.resize // Updates the selector to match the element's size -svgedit.select.Selector.prototype.resize = function() { +// bbox - Optional bbox to use for resize (prevents duplicate getBBox call). +svgedit.select.Selector.prototype.resize = function(bbox) { var selectedBox = this.selectorRect, mgr = selectorManager_, selectedGrips = mgr.selectorGrips, @@ -162,7 +165,10 @@ svgedit.select.Selector.prototype.resize = function() { m.e *= current_zoom; m.f *= current_zoom; - var bbox = svgedit.utilities.getBBox(selected); + if (!bbox) + bbox = svgedit.utilities.getBBox(selected); + // TODO: svgedit.utilities.getBBox (previous line) already knows to call getStrokedBBox when tagName === 'g'. Remove this? + // TODO: svgedit.utilities.getBBox doesn't exclude 'gsvg' and calls getStrokedBBox for any 'g'. Should getBBox be updated? if (tagName === 'g' && !$.data(selected, 'gsvg')) { // The bbox for a group does not include stroke vals, so we // get the bbox based on its children. @@ -413,7 +419,8 @@ svgedit.select.SelectorManager.prototype.initGroup = function() { // // Parameters: // elem - DOM element to get the selector for -svgedit.select.SelectorManager.prototype.requestSelector = function(elem) { +// bbox - Optional bbox to use for reset (prevents duplicate getBBox call). +svgedit.select.SelectorManager.prototype.requestSelector = function(elem, bbox) { if (elem == null) {return null;} var i, N = this.selectors.length; @@ -425,13 +432,13 @@ svgedit.select.SelectorManager.prototype.requestSelector = function(elem) { for (i = 0; i < N; ++i) { if (this.selectors[i] && !this.selectors[i].locked) { this.selectors[i].locked = true; - this.selectors[i].reset(elem); + this.selectors[i].reset(elem, bbox); this.selectorMap[elem.id] = this.selectors[i]; return this.selectors[i]; } } // if we reached here, no available selectors were found, we create one - this.selectors[N] = new svgedit.select.Selector(N, elem); + this.selectors[N] = new svgedit.select.Selector(N, elem, bbox); this.selectorParentGroup.appendChild(this.selectors[N].selectorGroup); this.selectorMap[elem.id] = this.selectors[N]; return this.selectors[N]; diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 1511bc30..37062ace 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -940,7 +940,9 @@ var addToSelection = this.addToSelection = function(elemsToAdd, showGrips) { var i = elemsToAdd.length; while (i--) { var elem = elemsToAdd[i]; - if (!elem || !svgedit.utilities.getBBox(elem)) {continue;} + if (!elem) {continue;} + var bbox = svgedit.utilities.getBBox(elem); + if (!bbox) {continue;} if (elem.tagName === 'a' && elem.childNodes.length === 1) { // Make "a" element's child be the selected element @@ -955,7 +957,7 @@ var addToSelection = this.addToSelection = function(elemsToAdd, showGrips) { // only the first selectedBBoxes element is ever used in the codebase these days // if (j == 0) selectedBBoxes[0] = svgedit.utilities.getBBox(elem); j++; - var sel = selectorManager.requestSelector(elem); + var sel = selectorManager.requestSelector(elem, bbox); if (selectedElements.length > 1) { sel.showGrips(false);