Skip to content

Commit 29abb5d

Browse files
committed
Cache DOM nodes and selected d3 node
1 parent 7538338 commit 29abb5d

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/components/drawing/index.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ drawing.makeTester = function(gd) {
504504
// always returns a copy of the bbox, so the caller can modify it safely
505505
var savedBBoxes = [],
506506
maxSavedBBoxes = 10000;
507+
507508
drawing.bBox = function(node) {
508509
// cache elements we've already measured so we don't have to
509510
// remeasure the same thing many times
@@ -512,32 +513,36 @@ drawing.bBox = function(node) {
512513
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
513514
}
514515

515-
var test3 = d3.select('#js-plotly-tester'),
516-
tester = test3.node();
516+
if(!drawing.test3) {
517+
drawing.test3 = d3.select('#js-plotly-tester');
518+
drawing.tester = drawing.test3.node();
519+
}
517520

518521
// copy the node to test into the tester
519522
var testNode = node.cloneNode(true);
520-
tester.appendChild(testNode);
523+
drawing.tester.appendChild(testNode);
521524
// standardize its position... do we really want to do this?
522525
d3.select(testNode).attr({
523526
x: 0,
524527
y: 0,
525528
transform: ''
526529
});
527530

528-
var testRect = testNode.getBoundingClientRect(),
529-
refRect = test3.select('.js-reference-point')
531+
var testRect = testNode.getBoundingClientRect();
532+
if(!drawing.refRect) {
533+
drawing.refRect = drawing.test3.select('.js-reference-point')
530534
.node().getBoundingClientRect();
535+
}
531536

532-
tester.removeChild(testNode);
537+
drawing.tester.removeChild(testNode);
533538

534539
var bb = {
535540
height: testRect.height,
536541
width: testRect.width,
537-
left: testRect.left - refRect.left,
538-
top: testRect.top - refRect.top,
539-
right: testRect.right - refRect.left,
540-
bottom: testRect.bottom - refRect.top
542+
left: testRect.left - drawing.refRect.left,
543+
top: testRect.top - drawing.refRect.top,
544+
right: testRect.right - drawing.refRect.left,
545+
bottom: testRect.bottom - drawing.refRect.top
541546
};
542547

543548
// make sure we don't have too many saved boxes,

src/lib/svg_text_utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var stringMappings = require('../constants/string_mappings');
1919

2020
// Append SVG
2121

22+
var parser = new DOMParser();
23+
2224
d3.selection.prototype.appendSVG = function(_svgString) {
2325
var skeleton = [
2426
'<svg xmlns="', xmlnsNamespaces.svg, '" ',
@@ -27,7 +29,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
2729
'</svg>'
2830
].join('');
2931

30-
var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
32+
var dom = parser.parseFromString(skeleton, 'application/xml'),
3133
childNode = dom.documentElement.firstChild;
3234

3335
while(childNode) {

0 commit comments

Comments
 (0)