SVGTextContentElement: getExtentOfChar() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGTextContentElement 接口的 getExtentOfChar() 方法,表示与给定排版字符对应的字形单元格的计算出的紧密边界框。

语法

js
getExtentOfChar(index)

参数

index

一个 integer;字符的索引。

返回值

一个 DOMRect 对象;指定字符的紧密边界框。

异常

IndexSizeError DOMException

如果 index 处未找到字符,则抛出异常。

示例

获取字符的范围

html
<svg width="300" height="100">
  <text id="exampleText" x="10" y="50" font-size="16">Hello, SVG World!</text>
</svg>
js
const textElement = document.getElementById("exampleText");

// Get the extent (bounding box) of the character at index 0 (the first character)
const extent = textElement.getExtentOfChar(0);

// The bounding box of the first character
console.dir(extent); // Output: DOMRect { x: 10, y: 38, width: 11.55, height: 16 }

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGTextContentElement__getExtentOfChar

浏览器兼容性

另见