SVGTextContentElement: getCharNumAtPosition() 方法
SVGTextContentElement 介面的 getCharNumAtPosition() 方法用於表示在給定座標系位置渲染文字字形時,導致該字形被渲染的字元。由於字元與字形之間不是一對一的關係,因此僅返回相關排版字元的第一個字元。
如果在指定位置找不到字元,則返回 -1。
語法
js
getCharNumAtPosition(point)
引數
返回值
一個長整數;與該位置對應的字元的索引。
示例
查詢特定位置的字元
html
<svg width="200" height="100">
<text id="exampleText" x="10" y="40" font-size="16">Hello, SVG World!</text>
</svg>
js
const textElement = document.getElementById("exampleText");
// Create a DOMPoint for the position (30, 40)
const point = new DOMPoint(30, 40);
// Get the character at the specified position
const charIndex = textElement.getCharNumAtPosition(point);
console.log(charIndex); // Output: 2 (for character "l")
// Check with a point where no character is present
const offPoint = new DOMPoint(300, 40);
const offCharIndex = textElement.getCharNumAtPosition(offPoint);
console.log(offCharIndex); // Output: -1 (no character found)
規範
| 規範 |
|---|
| Scalable Vector Graphics (SVG) 2 # __svg__SVGTextContentElement__getCharNumAtPosition |
瀏覽器相容性
載入中…