Skip to content

Instantly share code, notes, and snippets.

@huytd
huytd / d3-text-measure.js
Created May 6, 2016 18:19
Measure text size in pixels with D3.js
function textSize(text) {
if (!d3) return;
var container = d3.select('body').append('svg');
container.append('text').attr({ x: -99999, 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");