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

瀏覽器相容性

另見