SVG获取文本元素宽度 [英] SVG get text element width

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

问题描述

我正在使用一些用于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屋!

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