SVGTextContentElement: getComputedTextLength() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

getComputedTextLength()SVGTextContentElement インターフェイスのメソッドで、この要素内のテキストの計算された長さを表します。

構文

js
getComputedTextLength()

引数

なし。

返値

実数です。

テキストの長さの計算

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");

// 計算されたテキストの長さを取得
const textLength = textElement.getComputedTextLength();

console.log(textLength); // 出力: 124.5 (例。フォントサイズやテキストコンテンツによって変わる)

仕様書

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGTextContentElement__getComputedTextLength

ブラウザーの互換性