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

View in English Always switch to English

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

y2 属性は、2 つ以上の座標を必要とする SVG 要素を描画するための 2 つ目の Y 座標を指定するのに使います。1 つしか座標を必要としない要素は、これの代わりに y 属性を使います。

要素

この属性は、下記で説明する SVG 要素で使用することができます。

<line>

<line> では、 y2 は線の終点の Y 座標を定義します。

<length> | <percentage> | <number>
デフォルト値 0
アニメーション
html
<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  <line x1="1" x2="9" y1="5" y2="1" stroke="red" />
  <line x1="1" x2="9" y1="5" y2="5" stroke="green" />
  <line x1="1" x2="9" y1="5" y2="9" stroke="blue" />
</svg>

<linearGradient>

<linearGradient> に関しては、y2 は、グラデーションの色経由点をマッピングするのに使われるグラデーションベクトル (gradient vector) の終点の y 座標を定めます。この属性の正確な振る舞いは、gradientUnits 属性の影響を受けます。

<length>
デフォルト値 0%
アニメーション
html
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  <!--
  既定では、グラデーションベクトルは、適用先の形状を囲う境界の
  右上の角で終わります。
  -->
  <linearGradient y2="0%" id="g0">
    <stop offset="5%" stop-color="black" />
    <stop offset="50%" stop-color="red" />
    <stop offset="95%" stop-color="black" />
  </linearGradient>

  <rect x="1" y="1" width="8" height="8" fill="url(#g0)" />

  <!--
  ここでは、グラデーションベクトルは、適用先の形状を囲う境界の
  右下の角で終わります。
  -->
  <linearGradient y2="100%" id="g1">
    <stop offset="5%" stop-color="black" />
    <stop offset="50%" stop-color="red" />
    <stop offset="95%" stop-color="black" />
  </linearGradient>

  <rect x="11" y="1" width="8" height="8" fill="url(#g1)" />
</svg>

Examples

html
<svg viewBox="0 0 25 25" xmlns="http://www.w3.org/2000/svg">
  <line x1="2" x2="22" y1="20" y2="0" stroke="red" />
  <line x1="2" x2="22" y1="20" y2="10" stroke="green" />
  <line x1="2" x2="22" y1="20" y2="20" stroke="blue" />
</svg>

仕様書

Specification
Scalable Vector Graphics (SVG) 2
# LinearGradientElementY2Attribute

ブラウザーの互換性

svg.elements.line.y2

svg.elements.linearGradient.y2