Skip to content

Drawing bbox fix for window scroll #1683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 16, 2017
25 changes: 10 additions & 15 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ drawing.makeTester = function(gd) {
// always returns a copy of the bbox, so the caller can modify it safely
var savedBBoxes = [],
maxSavedBBoxes = 10000;

drawing.bBox = function(node) {
// cache elements we've already measured so we don't have to
// remeasure the same thing many times
Expand All @@ -598,36 +597,32 @@ drawing.bBox = function(node) {
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
}

if(!drawing.test3) {
drawing.test3 = d3.select('#js-plotly-tester');
drawing.tester = drawing.test3.node();
}
var test3 = d3.select('#js-plotly-tester'),
tester = test3.node();

// copy the node to test into the tester
var testNode = node.cloneNode(true);
drawing.tester.appendChild(testNode);
tester.appendChild(testNode);
// standardize its position... do we really want to do this?
d3.select(testNode).attr({
x: 0,
y: 0,
transform: ''
});

var testRect = testNode.getBoundingClientRect();
if(!drawing.refRect) {
drawing.refRect = drawing.test3.select('.js-reference-point')
var testRect = testNode.getBoundingClientRect(),
refRect = test3.select('.js-reference-point')
.node().getBoundingClientRect();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep the perf gains from #1585 in this fix, I'm pretty sure the only change we'd need is to call this .getBoundingClientRect() with each drawing.bBox call, rather than caching it. But we can still cache tester, and we can cache test3.select('.js-reference-point').node().

Just a question of timing really - now that you have a scrolling test in place it seems safe to me, but you can leave it as is for now if you need to move on to other things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's in #1690

}

drawing.tester.removeChild(testNode);
tester.removeChild(testNode);

var bb = {
height: testRect.height,
width: testRect.width,
left: testRect.left - drawing.refRect.left,
top: testRect.top - drawing.refRect.top,
right: testRect.right - drawing.refRect.left,
bottom: testRect.bottom - drawing.refRect.top
left: testRect.left - refRect.left,
top: testRect.top - refRect.top,
right: testRect.right - refRect.left,
bottom: testRect.bottom - refRect.top
};

// make sure we don't have too many saved boxes,
Expand Down
4 changes: 1 addition & 3 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ var stringMappings = require('../constants/string_mappings');

// Append SVG

var parser = new DOMParser();

d3.selection.prototype.appendSVG = function(_svgString) {
var skeleton = [
'<svg xmlns="', xmlnsNamespaces.svg, '" ',
Expand All @@ -29,7 +27,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
'</svg>'
].join('');

var dom = parser.parseFromString(skeleton, 'application/xml'),
var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
childNode = dom.documentElement.firstChild;

while(childNode) {
Expand Down
53 changes: 49 additions & 4 deletions test/jasmine/tests/drawing_test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
var Drawing = require('@src/components/drawing');

var d3 = require('d3');

var Plotly = require('@lib/index');

var Drawing = require('@src/components/drawing');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var fail = require('../assets/fail_test');
Expand Down Expand Up @@ -348,6 +345,54 @@ describe('Drawing', function() {
expect(g.attr('transform')).toEqual('translate(8,9) scale(4,5) translate(-8,-9) translate(1, 2)');
});
});

describe('bBox', function() {
afterEach(destroyGraphDiv);

it('should update bounding box dimension on window scroll', function(done) {
var gd = createGraphDiv();

// allow page to scroll
gd.style.position = 'static';

Plotly.plot(gd, [{
y: [1, 2, 1]
}], {
annotations: [{
text: 'hello'
}],
height: window.innerHeight * 2,
width: 500
})
.then(function() {
var node = d3.select('text.annotation').node();
expect(Drawing.bBox(node)).toEqual({
height: 14,
width: 27.671875,
left: -13.671875,
top: -11,
right: 14,
bottom: 3
});

window.scroll(0, 200);
return Plotly.relayout(gd, 'annotations[0].text', 'HELLO');
})
.then(function() {
var node = d3.select('text.annotation').node();
expect(Drawing.bBox(node)).toEqual({
height: 14,
width: 41.015625,
left: -20.671875,
top: -11,
right: 20.34375,
bottom: 3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 29abb5d made the bottom and top keys here incorrectly change - as the bbox computation were based on a outdated cached tester element.

Copy link
Contributor Author

@etpinard etpinard May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width, left and right keys change correctly here in response to the annotation text relayout update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Ha, those values appear to be machine-dependent (I suspect a font issue) ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 3fdb308

});
})
.catch(fail)
.then(done);
});
});
});

describe('gradients', function() {
Expand Down