SVGLineElement: x1 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGLineElement 接口的只读属性 x1 使用 SVGAnimatedLength 描述了 SVG 线条在 x 轴上的起始点。它反映了 <line> 元素的 x1 几何属性。

属性值可以是 <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>

我们可以访问 x1 属性的计算值

js
const lines = document.querySelectorAll("line");
const x1Pos0 = lines[0].x1;
const x1Pos1 = lines[1].x1;
console.dir(x1Pos0.baseVal.value); // output: 20 (the value of `x1`)
console.dir(x1Pos1.baseVal.value); // output: 45 (15% of 300)

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGLineElement__x1

浏览器兼容性

另见