getBBox

JavaScript

var svg = d3.select("body").append("svg")
    .attr("width", 1)
    .attr("height", 1);


calculateBbox = function(string, font, x, y) { 
    
    var group = svg.append("g").style("display", "none");
    text = group.append("text") 
        .attr("x", x) 
        .attr("y", y) 
        .style("font", font) 
        .text(string); 

    return text.node().getBBox().width; 
};


console.log(calculateBbox("Hello Kitty!", "300 128px Helvetica Neue", 200, 250)); // 624