SVG获取文本元素宽度 [英] SVG get text element width
问题描述
我正在使用一些用于SVG文件的ECMAScript / JavaScript,需要获取 width
和 height
的一个文本
元素,所以我可以调整围绕它的矩形的大小。在HTML中,我可以使用元素上的 offsetWidth
和 offsetHeight
属性,但这些属性似乎不可用。
I'm working on some ECMAScript/JavaScript for an SVG file and need to get the width
and height
of a text
element so I can resize a rectangle that surrounds it. In HTML I would be able to use the offsetWidth
and offsetHeight
attributes on the element but it appears that those properties are unavailable.
这是一个需要处理的片段。当我更改文本时,我需要更改矩形的宽度,但是我不知道如何获得宽度
(以像素为单位) >文本元素
Here's a fragment that I need to work with. I need to change the width of the rectangle whenever I change the text but I don't know how to get the actual width
(in pixels) of the text
element.
<rect x="100" y="100" width="100" height="100" />
<text>Some Text</text>
任何想法?
推荐答案
var bbox = textElement.getBBox();
var width = bbox.width;
var height = bbox.height;
然后相应地设置rect的属性。
and then set the rect's attributes accordingly.
链接: getBBox()
在SVG v1.1标准。
Link: getBBox()
in the SVG v1.1 standard.
这篇关于SVG获取文本元素宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!