Skip to content

Commit 388986b

Browse files
committed
centralize getBoundingClientRect in hover and transform DOMRect
1 parent 88e0524 commit 388986b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/components/fx/hover.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ function createHoverText(hoverData, opts) {
891891
var ya = c0.ya;
892892
var axLetter = hovermode.charAt(0);
893893
var t0 = c0[axLetter + 'Label'];
894-
var outerContainerBB = outerContainer.getBoundingClientRect();
894+
var outerContainerBB = getBoundingClientRect(gd, outerContainer);
895895
var outerTop = outerContainerBB.top;
896896
var outerWidth = outerContainerBB.width;
897897
var outerHeight = outerContainerBB.height;
@@ -966,7 +966,7 @@ function createHoverText(hoverData, opts) {
966966

967967
label.attr('transform', '');
968968

969-
var tbb = ltext.node().getBoundingClientRect();
969+
var tbb = getBoundingClientRect(gd, ltext.node());
970970
var lx, ly;
971971

972972
if(hovermode === 'x') {
@@ -1059,7 +1059,7 @@ function createHoverText(hoverData, opts) {
10591059
var dummy = Drawing.tester.append('text')
10601060
.text(s.text())
10611061
.call(Drawing.font, commonLabelFont);
1062-
var dummyBB = dummy.node().getBoundingClientRect();
1062+
var dummyBB = getBoundingClientRect(gd, dummy.node());
10631063
if(Math.round(dummyBB.width) < Math.round(tbb.width)) {
10641064
s.attr('x', ltx - dummyBB.width);
10651065
}
@@ -1148,7 +1148,7 @@ function createHoverText(hoverData, opts) {
11481148

11491149
// Position the hover
11501150
var legendContainer = container.select('g.legend');
1151-
var tbb = legendContainer.node().getBoundingClientRect();
1151+
var tbb = getBoundingClientRect(gd, legendContainer.node());
11521152
var tWidth = tbb.width + 2 * HOVERTEXTPAD;
11531153
var tHeight = tbb.height + 2 * HOVERTEXTPAD;
11541154
var winningPoint = hoverData[0];
@@ -1313,7 +1313,7 @@ function createHoverText(hoverData, opts) {
13131313
.call(svgTextUtils.positionText, 0, 0)
13141314
.call(svgTextUtils.convertToTspans, gd);
13151315

1316-
var t2bb = tx2.node().getBoundingClientRect();
1316+
var t2bb = getBoundingClientRect(gd, tx2.node());
13171317
tx2width = t2bb.width + 2 * HOVERTEXTPAD;
13181318
tx2height = t2bb.height + 2 * HOVERTEXTPAD;
13191319
} else {
@@ -1326,7 +1326,7 @@ function createHoverText(hoverData, opts) {
13261326
stroke: contrastColor
13271327
});
13281328

1329-
var tbb = tx.node().getBoundingClientRect();
1329+
var tbb = getBoundingClientRect(gd, tx.node());
13301330
var htx = d.xa._offset + (d.x0 + d.x1) / 2;
13311331
var hty = d.ya._offset + (d.y0 + d.y1) / 2;
13321332
var dx = Math.abs(d.x1 - d.x0);
@@ -2100,3 +2100,33 @@ function getCoord(axLetter, winningPoint, fullLayout) {
21002100
// the offset parent, whatever that may be.
21012101
function getTopOffset(gd) { return gd.offsetTop + gd.clientTop; }
21022102
function getLeftOffset(gd) { return gd.offsetLeft + gd.clientLeft; }
2103+
2104+
function getBoundingClientRect(gd, node) {
2105+
var fullLayout = gd._fullLayout;
2106+
2107+
var rect = node.getBoundingClientRect();
2108+
2109+
var x0 = rect.x;
2110+
var y0 = rect.y;
2111+
var x1 = x0 + rect.width;
2112+
var y1 = y0 + rect.height;
2113+
2114+
var A = Lib.apply3DTransform(fullLayout._invTransform)(x0, y0);
2115+
var B = Lib.apply3DTransform(fullLayout._invTransform)(x1, y1);
2116+
2117+
var Ax = A[0];
2118+
var Ay = A[1];
2119+
var Bx = B[0];
2120+
var By = B[1];
2121+
2122+
return {
2123+
x: Ax,
2124+
y: Ay,
2125+
width: Bx - Ax,
2126+
height: By - Ay,
2127+
top: Math.min(Ay, By),
2128+
left: Math.min(Ax, Bx),
2129+
right: Math.max(Ax, Bx),
2130+
bottom: Math.max(Ay, By),
2131+
};
2132+
}

0 commit comments

Comments
 (0)