Skip to content

Commit 08e6a96

Browse files
authored
Merge pull request #5976 from plotly/revise-convertToTspans
Revise `convertToTspans`
2 parents fa9c817 + d1e5017 commit 08e6a96

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

draftlogs/5976_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reduce calls to `getBoundingClientRect` in `convertToTspans` [[#5976](https://github.com/plotly/plotly.js/pull/5976)]

src/lib/svg_text_utils.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ var LINE_SPACING = require('../constants/alignment').LINE_SPACING;
1111

1212
// text converter
1313

14-
function getSize(_selection, _dimension) {
15-
return _selection.node().getBoundingClientRect()[_dimension];
16-
}
17-
1814
var FIND_TEX = /([^$]*)([$]+[^$]*[$]+)([^$]*)/;
1915

2016
exports.convertToTspans = function(_context, gd, _callback) {
@@ -109,26 +105,31 @@ exports.convertToTspans = function(_context, gd, _callback) {
109105
var g = newSvg.select('g');
110106
g.attr({fill: fill, stroke: fill});
111107

112-
var newSvgW = getSize(g, 'width');
113-
var newSvgH = getSize(g, 'height');
114-
var newX = +_context.attr('x') - newSvgW *
108+
var gBB = g.node().getBoundingClientRect();
109+
var newSvgW = gBB.width;
110+
var newSvgH = gBB.height;
111+
112+
var x = +_context.attr('x');
113+
var y = +_context.attr('y');
114+
115+
var newX = x - newSvgW *
115116
{start: 0, middle: 0.5, end: 1}[_context.attr('text-anchor') || 'start'];
116117
// font baseline is about 1/4 fontSize below centerline
117-
var textHeight = fontSize || getSize(_context, 'height');
118+
var textHeight = fontSize || _context.node().getBoundingClientRect().height;
118119
var dy = -textHeight / 4;
119120

120121
if(svgClass[0] === 'y') {
121122
mathjaxGroup.attr({
122-
transform: 'rotate(' + [-90, +_context.attr('x'), +_context.attr('y')] +
123+
transform: 'rotate(' + [-90, x, y] +
123124
')' + strTranslate(-newSvgW / 2, dy - newSvgH / 2)
124125
});
125-
newSvg.attr({x: +_context.attr('x'), y: +_context.attr('y')});
126+
newSvg.attr({x: x, y: y});
126127
} else if(svgClass[0] === 'l') {
127-
newSvg.attr({x: _context.attr('x'), y: dy - (newSvgH / 2)});
128+
newSvg.attr({x: x, y: dy - (newSvgH / 2)});
128129
} else if(svgClass[0] === 'a' && svgClass.indexOf('atitle') !== 0) {
129130
newSvg.attr({x: 0, y: dy});
130131
} else {
131-
newSvg.attr({x: newX, y: (+_context.attr('y') + dy - newSvgH / 2)});
132+
newSvg.attr({x: newX, y: (y + dy - newSvgH / 2)});
132133
}
133134

134135
if(_callback) _callback.call(_context, mathjaxGroup);

0 commit comments

Comments
 (0)