Skip to content

Instantly share code, notes, and snippets.

@MaxXxiMast
Last active January 11, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxXxiMast/61f1df0a4c4d37a3cf2714fddcf0e283 to your computer and use it in GitHub Desktop.
Save MaxXxiMast/61f1df0a4c4d37a3cf2714fddcf0e283 to your computer and use it in GitHub Desktop.
function textSize(text) {
if (!d3) return;
var container = d3.select('body').append('svg');
container.append('text').attr('x', -99999).attr('y',-99999).text(text);
var size = container.node().getBBox();
container.remove();
return { width: size.width, height: size.height };
}
// Usage: textSize("This is a very long text");
// => Return: Object {width: 140, height: 15.453125}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment