SVGLineElement: x2 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

SVGLineElement 介面中只讀的 x2 屬性描述了線條結束點的 x 軸座標值,其型別為 SVGAnimatedLength。它反映了 <line> 元素的 x2 幾何屬性。

該屬性值可以是 <length><percentage><number>SVGAnimatedLength.baseVal 的數值是以使用者座標系單位表示的該終點在 x 軸上的位置。

一個 SVGAnimatedLength 物件。

示例

給定以下 SVG

html
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
  <line x1="20" y1="30" x2="40" y2="50" stroke="blue" stroke-width="2" />
  <line x1="15%" y1="0" x2="30%" y2="60" stroke="red" stroke-width="4" />
</svg>

我們可以訪問 x2 屬性的計算值

js
const lines = document.querySelectorAll("line");
const x2Pos0 = lines[0].x2;
const x2Pos1 = lines[1].x2;
console.dir(x2Pos0.baseVal.value); // output: 40 (the value of `x2`)
console.dir(x2Pos1.baseVal.value); // output: 90 (30% of 300)

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGLineElement__x2

瀏覽器相容性

另見