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

View in English Always switch to English

SVGLinearGradientElement: y2 プロパティ

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月.

y2SVGLinearGradientElement インターフェイスの読み取り専用プロパティで、グラデーションの始点の Y 軸座標を SVGAnimatedLength として記述します。これは、<linearGradient> 要素の y2 属性の計算値を反映します。

この属性の値は <length><percentage><number> のいずれかです。SVGAnimatedLength.baseVal の値は、ユーザー座標系におけるグラデーションの終点の y 座標です。

SVGAnimatedLength です。

次の SVG があったとします。

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="gradient1" x1="50%" y1="0%" x2="50%" y2="100%">
      <stop offset="0%" stop-color="blue" />
      <stop offset="100%" stop-color="white" />
    </linearGradient>
    <linearGradient id="gradient2" x1="25%" y1="0%" x2="75%" y2="100%">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="yellow" />
    </linearGradient>
  </defs>
  <rect x="0" y="0" width="200" height="100" fill="url(#gradient1)" />
  <rect x="0" y="100" width="200" height="100" fill="url(#gradient2)" />
</svg>

y2 属性の計算値には、次のようにアクセスできます。

js
const linearGradients = document.querySelectorAll("linearGradient");
const y2Gradient1 = linearGradients[0].y2;
const y2Gradient2 = linearGradients[1].y2;

console.dir(y2Gradient1.baseVal.value); // 出力: 200 (100% of 200)
console.dir(y2Gradient2.baseVal.value); // 出力: 200 (100% of 200)

仕様書

Specification
Scalable Vector Graphics (SVG) 2
# __svg__SVGLinearGradientElement__y2

ブラウザーの互換性

関連情報