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

瀏覽器相容性