Sca*_*ffe 10 javascript svg text tspan
我有一个字符串数组.说,
['Jan 11','Feb 11']
Run Code Online (Sandbox Code Playgroud)
我正在用这些字符串创建一个垂直文本
<text x="60" y="154" text-anchor="middle" style="text-anchor: middle; font: normal normal normal 12px/normal Helvetica, Arial; " font="12px Helvetica, Arial" stroke="none" fill="#ffffff" transform="rotate(90 59.75 150)">
<tspan>Jan 11</tspan>
</text>
Run Code Online (Sandbox Code Playgroud)
在渲染svg之后,我发现文本的高度是36px.现在有一种方法可以计算预先根据字体大小预先渲染的文本的高度吗?
bjo*_*rnd 15
您可以使用getBBox方法计算SVG节点的维度.
var textNode = document.getElementsByTagName('text'),
bbox = textNode.getBBox();
//bbox now have x, y, width and height properties
Run Code Online (Sandbox Code Playgroud)