SVGTextContentElement: getEndPositionOfChar() 方法

Baseline 已广泛支持

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

SVGTextContentElement 接口的 getEndPositionOfChar() 方法在执行文本布局后,返回一个排版字符的尾随位置。

语法

js
getEndPositionOfChar(index)

参数

index

一个 integer;字符的索引。

返回值

一个 DOMPoint 对象;字符在用户坐标中的位置。

异常

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 end position of the character at index 0 (the first character)
const position = textElement.getEndPositionOfChar(0);

// Get the x and y coordinates of the first character
console.log(position.x, position.y); // Output: 21.5 50

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGTextContentElement__getEndPositionOfChar

浏览器兼容性