2015-03-18 09:47:26 +00:00
|
|
|
var fontFamily = "'Open Sans', Helvetica, Arial, sans-serif",
|
2015-03-13 12:08:13 +00:00
|
|
|
fontSize = 16;
|
|
|
|
|
|
|
|
function textWidth(str) {
|
2015-03-18 10:14:22 +00:00
|
|
|
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
|
|
text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
|
2015-03-13 12:08:13 +00:00
|
|
|
|
|
|
|
svg.width = 500;
|
|
|
|
svg.height = 500;
|
|
|
|
svg.style.position = 'absolute';
|
|
|
|
svg.style.left = '-1000px';
|
|
|
|
|
|
|
|
text.appendChild(document.createTextNode(str))
|
|
|
|
text.style.fontFamily = fontFamily;
|
|
|
|
text.style.fontSize = fontSize +'px';
|
|
|
|
|
|
|
|
svg.appendChild(text);
|
|
|
|
document.body.appendChild(svg);
|
|
|
|
var box = text.getBBox();
|
|
|
|
document.body.removeChild(svg);
|
|
|
|
|
|
|
|
return box.width;
|
|
|
|
}
|