このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

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 ⁨2015年7月⁩.

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

ブラウザーの互換性