PerformanceElementTiming: naturalWidth プロパティ

Experimental: これは実験的な機能です。
本番で使用する前にブラウザー互換性一覧表をチェックしてください。

naturalWidthPerformanceElementTiming インターフェイスの読み取り専用プロパティで、画像要素の内在的な幅を返します。

符号なし 32 ビット整数 (unsigned long) です。 これが画像に適用されている場合は画像の内在的な幅を表し、テキストの場合は 0 となります。

naturalWidth のログ出力

この例では、elementtiming 属性を追加して <image> 要素を監視しています。 PerformanceObserver"element" 型のパフォーマンス項目をすべて取得するために登録され、オブザーバー作成前のデータにアクセスするために buffered フラグを使用しています。画像ファイルの幅は 1000px、高さは 750px あります。 entry.naturalWidth を呼び出すと、ピクセル単位の内在的な幅である 1000 を返します。

html
<img
  src="image.jpg"
  alt="a nice image"
  elementtiming="big-image"
  id="myImage" />
js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    if (entry.identifier === "big-image") {
      console.log(entry.naturalWidth);
    }
  });
});
observer.observe({ type: "element", buffered: true });

仕様書

Specification
Element Timing API
# ref-for-dom-performanceelementtiming-naturalwidth

ブラウザーの互換性

BCD tables only load in the browser