【发布时间】:2017-02-20 07:42:49
【问题描述】:
我希望能够计算 SVG 中文本(或跨度)元素占用的实际高度。
目前,我通过计算对象边界框的高度来实现这一点,但它需要字体字形的高度,因此我放入元素中的任何文本都具有相同的高度,如下例所示:
var minA = document.getElementById('min-a'),
capA = document.getElementById('cap-a'),
minAHeight = document.getElementById('min-a-height'),
capAHeight = document.getElementById('cap-a-height')
;
minAHeight.innerHTML = minA.getBBox().height;
capAHeight.innerHTML = capA.getBBox().height;
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 80">
<text id="min-a" x="20" y ="20">a</text>
<text id="cap-a" x="20" y ="50">Â</text>
<text id="min-a-height" x="70" y ="20"></text>
<text id="cap-a-height" x="70" y ="50"></text>
<svg>
如何计算每个元素的实际高度?
【问题讨论】:
-
没有API,即你不能。
-
@RobertLongson 你绝对可以
-
@RobertLongson 发布 :)
标签: javascript html svg