SVG
The x2 read-only property of the SVGLineElement interface describes the x-axis coordinate value of the end of a line as an SVGAnimatedLength. It reflects the line element's x2 geometric attribute.
The attribute value is a <length>, <percentage>, or <number>. The numeric value of the SVGAnimatedLength.baseVal is that end position along the x-axis in user coordinate system units.
Value
An SVGAnimatedLength.
Example
Given the following SVG:
<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>
We can access the computed values of the x2 attributes:
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)
Specifications
Browser compatibility
See also
- SVGLineElement.y2
- SVGAnimatedLength.baseVal
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
