计算SVG文本的垂直高度 [英] Calculating vertical height of a SVG text

查看:125
本文介绍了计算SVG文本的垂直高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组。说,

I have an array of strings. Say,

['Jan 11','Feb 11']

我正在创建一个包含这些字符串的垂直文本

And i am creating a vertical text with these string like so

<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>

在渲染svg后,我发现文本的高度为36px。现在有没有办法计算预先渲染的文本高度给定字体大小?

After the svg has been rendered i find that the height of the text is 36px. Now is there a way to calculate the height of a text that will be rendered beforehand given the font-size?

推荐答案

你可以使用getBBox方法计算SVG节点的维度。

You can use getBBox method to calculate dimensions of the SVG nodes.

var textNode = document.getElementsByTagName('text'),
    bbox = textNode.getBBox();

//bbox now have x, y, width and height properties

这篇关于计算SVG文本的垂直高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆