Skip to content
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

Revise convertToTspans #5976

Merged
merged 5 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/5976_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Reduce calls to `getBoundingClientRect` in `convertToTspans` [[#5976](https://github.com/plotly/plotly.js/pull/5976)]
25 changes: 13 additions & 12 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ var LINE_SPACING = require('../constants/alignment').LINE_SPACING;

// text converter

function getSize(_selection, _dimension) {
return _selection.node().getBoundingClientRect()[_dimension];
}

var FIND_TEX = /([^$]*)([$]+[^$]*[$]+)([^$]*)/;

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

var newSvgW = getSize(g, 'width');
var newSvgH = getSize(g, 'height');
var newX = +_context.attr('x') - newSvgW *
var gBB = g.node().getBoundingClientRect();
var newSvgW = gBB.width;
var newSvgH = gBB.height;

var x = +_context.attr('x');
var y = +_context.attr('y');

var newX = x - newSvgW *
{start: 0, middle: 0.5, end: 1}[_context.attr('text-anchor') || 'start'];
// font baseline is about 1/4 fontSize below centerline
var textHeight = fontSize || getSize(_context, 'height');
var textHeight = fontSize || _context.node().getBoundingClientRect().height;
var dy = -textHeight / 4;

if(svgClass[0] === 'y') {
mathjaxGroup.attr({
transform: 'rotate(' + [-90, +_context.attr('x'), +_context.attr('y')] +
transform: 'rotate(' + [-90, x, y] +
')' + strTranslate(-newSvgW / 2, dy - newSvgH / 2)
});
newSvg.attr({x: +_context.attr('x'), y: +_context.attr('y')});
newSvg.attr({x: x, y: y});
} else if(svgClass[0] === 'l') {
newSvg.attr({x: _context.attr('x'), y: dy - (newSvgH / 2)});
newSvg.attr({x: x, y: dy - (newSvgH / 2)});
} else if(svgClass[0] === 'a' && svgClass.indexOf('atitle') !== 0) {
newSvg.attr({x: 0, y: dy});
} else {
newSvg.attr({x: newX, y: (+_context.attr('y') + dy - newSvgH / 2)});
newSvg.attr({x: newX, y: (y + dy - newSvgH / 2)});
}

if(_callback) _callback.call(_context, mathjaxGroup);
Expand Down