d
d 屬性定義要繪製的路徑。
路徑定義是路徑命令列表,每個命令都由一個命令字母和表示命令引數的數字組成。這些命令在下面詳細介紹。
此屬性與 SVG <path> 元素一起使用。
d 是一個表示屬性,因此也可以用作 CSS 屬性。
示例
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path
fill="none"
stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" />
</svg>
path
將 d 用作 CSS 屬性
d 是一個表示屬性,因此也可以使用 CSS 修改它。此屬性接受path() 或 none。
下面的示例展示瞭如何在將滑鼠懸停在元素上時應用新路徑。新路徑與舊路徑相同,但會在心形上新增一條線。
html,
body,
svg {
height: 100%;
}
/* This path is displayed on hover*/
#svg_css_ex1:hover path {
d: path(
"M10,30 A20,20 0,0,1 50,30 A20,20 0,0,1 90,30 Q90,60 50,90 Q10,60 10,30 z M5,5 L90,90"
);
}
<svg id="svg_css_ex1" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path
fill="none"
stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z
" />
</svg>
有關 <path> 動畫示例,請參閱 CSS d 屬性參考頁示例。
路徑命令
移至路徑命令
移至指令可以看作是拿起繪圖工具,並將其放置在其他地方——換句話說,移動當前點(Po;{xo, yo})。在Po 和新的當前點(Pn;{xn, yn})之間沒有繪製線條。
| 命令 | 引數 | 注意 |
|---|---|---|
| M | (x, y)+ |
將當前點移至座標 公式:Pn = { |
| m | (dx, dy)+ |
透過將路徑的最後一個已知位置沿 x 軸移動
公式:Pn = {xo + |
示例
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path
fill="none"
stroke="red"
d="M 10,10 h 10
m 0,10 h 10
m 0,10 h 10
M 40,20 h 10
m 0,10 h 10
m 0,10 h 10
m 0,10 h 10
M 50,50 h 10
m-20,10 h 10
m-20,10 h 10
m-20,10 h 10" />
</svg>
線段路徑命令
線段指令根據指定的引數,從當前點(Po;{xo, yo})繪製一條直線到終點(Pn;{xn, yn})。然後,終點 (Pn) 將成為下一個命令的當前點 (Po′)。
| 命令 | 引數 | 注意 |
|---|---|---|
| L | (x, y)+ |
從當前點繪製一條直線到由
公式: Po′ = Pn = { |
| l | (dx, dy)+ |
從當前點繪製一條直線到終點,該終點是當前點沿 x 軸偏移
公式: Po′ = Pn = {xo + |
| H | x+ |
從當前點繪製一條水平線到終點,該終點由
公式: Po′ = Pn = { |
| h | dx+ |
從當前點繪製一條水平線到終點,該終點由當前點沿 x 軸偏移
公式: Po′ = Pn = {xo + |
| V | y+ |
從當前點繪製一條垂直線到終點,該終點由
公式: Po′ = Pn = {xo, |
| v | dy+ |
從當前點繪製一條垂直線到終點,該終點由當前點沿 y 軸偏移
公式: Po′ = Pn = {xo, yo + |
示例
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<!-- LineTo commands with absolute coordinates -->
<path
fill="none"
stroke="red"
d="M 10,10
L 90,90
V 10
H 50" />
<!-- LineTo commands with relative coordinates -->
<path
fill="none"
stroke="red"
d="M 110,10
l 80,80
v -80
h -40" />
</svg>
三次貝塞爾曲線
三次貝塞爾曲線是使用四個點定義的平滑曲線
- 起點(當前點)
-
(Po = {xo, yo})
- 終點
-
(Pn = {xn, yn})
- 起點控制點
-
(Pcs = {xcs, ycs})(控制曲線開始處的曲率)
- 終點控制點
-
(Pce = {xce, yce})(控制曲線結束處的曲率)
繪製完成後,終點 (Pn) 成為下一個命令的當前點 (Po′)。
| 命令 | 引數 | 注意 |
|---|---|---|
| C | (x1,y1, x2,y2, x,y)+ |
從當前點繪製一條三次貝塞爾曲線到由
|
| c | (dx1,dy1, dx2,dy2, dx,dy)+ |
從當前點繪製一條三次貝塞爾曲線到終點,該終點是當前點沿 x 軸偏移
|
| S | (x2,y2, x,y)+ |
從當前點繪製一條平滑三次貝塞爾曲線到由x,y指定的終點。終點控制點由x2,y2指定。起點控制點是前一個曲線命令的終點控制點關於當前點的反射。如果前一個命令不是三次貝塞爾曲線,則起點控制點與曲線起點(當前點)相同。任何後續的座標對都被解釋為隱式絕對平滑三次貝塞爾曲線 (S) 命令的引數。 |
| s | (dx2,dy2, dx,dy)+ |
從當前點繪製一條平滑三次貝塞爾曲線到終點,該終點是當前點沿 x 軸偏移 dx 和沿 y 軸偏移 dy。終點控制點是當前點(曲線的起點)沿 x 軸偏移 dx2 和沿 y 軸偏移 dy2。起點控制點是前一個曲線命令的終點控制點關於當前點的反射。如果前一個命令不是三次貝塞爾曲線,則起點控制點與曲線起點(當前點)相同。任何後續的座標對都被解釋為隱式相對平滑三次貝塞爾曲線 (s) 命令的引數。 |
示例
<svg
viewBox="0 0 200 100"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Cubic Bézier curve with absolute coordinates -->
<path
fill="none"
stroke="red"
d="M 10,90
C 30,90 25,10 50,10
S 70,90 90,90" />
<!-- Cubic Bézier curve with relative coordinates -->
<path
fill="none"
stroke="red"
d="M 110,90
c 20,0 15,-80 40,-80
s 20,80 40,80" />
<!-- Highlight the curve vertex and control points -->
<g id="ControlPoints">
<!-- First cubic command control points -->
<line x1="10" y1="90" x2="30" y2="90" stroke="lightgrey" />
<circle cx="30" cy="90" r="1.5" />
<line x1="50" y1="10" x2="25" y2="10" stroke="lightgrey" />
<circle cx="25" cy="10" r="1.5" />
<!-- Second smooth command control points (the first one is implicit) -->
<line
x1="50"
y1="10"
x2="75"
y2="10"
stroke="lightgrey"
stroke-dasharray="2" />
<circle cx="75" cy="10" r="1.5" fill="lightgrey" />
<line x1="90" y1="90" x2="70" y2="90" stroke="lightgrey" />
<circle cx="70" cy="90" r="1.5" />
<!-- curve vertex points -->
<circle cx="10" cy="90" r="1.5" />
<circle cx="50" cy="10" r="1.5" />
<circle cx="90" cy="90" r="1.5" />
</g>
<use href="#ControlPoints" x="100" />
</svg>
二次貝塞爾曲線
二次貝塞爾曲線是使用三個點定義的平滑曲線
繪製完成後,終點 (Pn) 成為下一個命令的當前點 (Po′)。
| 命令 | 引數 | 注意 |
|---|---|---|
| Q | (x1,y1, x,y)+ |
從當前點繪製一條二次貝塞爾曲線到由
|
| q | (dx1,dy1, dx,dy)+ |
從當前點繪製一條二次貝塞爾曲線到終點,該終點是當前點沿 x 軸偏移
|
| T | (x,y)+ |
從當前點繪製一條平滑二次貝塞爾曲線到由
|
| t | (dx,dy)+ |
從當前點繪製一條平滑二次貝塞爾曲線到終點,該終點是當前點沿 x 軸偏移
|
示例
<svg
viewBox="0 0 200 100"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Quadratic Bézier curve with implicit repetition -->
<path
fill="none"
stroke="red"
d="M 10,50
Q 25,25 40,50
t 30,0 30,0 30,0 30,0 30,0" />
<!-- Highlight the curve vertex and control points -->
<g>
<polyline
points="10,50 25,25 40,50"
stroke="rgb(0 0 0 / 20%)"
fill="none" />
<circle cx="25" cy="25" r="1.5" />
<!-- Curve vertex points -->
<circle cx="10" cy="50" r="1.5" />
<circle cx="40" cy="50" r="1.5" />
<g id="SmoothQuadraticDown">
<polyline
points="40,50 55,75 70,50"
stroke="rgb(0 0 0 / 20%)"
stroke-dasharray="2"
fill="none" />
<circle cx="55" cy="75" r="1.5" fill="lightgrey" />
<circle cx="70" cy="50" r="1.5" />
</g>
<g id="SmoothQuadraticUp">
<polyline
points="70,50 85,25 100,50"
stroke="rgb(0 0 0 / 20%)"
stroke-dasharray="2"
fill="none" />
<circle cx="85" cy="25" r="1.5" fill="lightgrey" />
<circle cx="100" cy="50" r="1.5" />
</g>
<use href="#SmoothQuadraticDown" x="60" />
<use href="#SmoothQuadraticUp" x="60" />
<use href="#SmoothQuadraticDown" x="120" />
</g>
</svg>
橢圓弧曲線
橢圓弧曲線是定義為橢圓一部分的曲線。用橢圓弧繪製高度規則的曲線有時比用貝塞爾曲線更容易。
| 命令 | 引數 | 注意 |
|---|---|---|
| A | (rx ry angle large-arc-flag sweep-flag x y)+ |
從當前點繪製一條弧曲線到座標
x,y 成為下一個命令的新當前點。所有後續的引數集都被認為是隱式絕對弧曲線 (A) 命令。 |
| a | (rx ry angle large-arc-flag sweep-flag dx dy)+ |
從當前點繪製一條弧曲線到一個點,該點的座標是當前點沿 x 軸偏移
dx 和 dy。所有後續的引數集都被認為是隱式相對弧曲線 (a) 命令。 |
示例
<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<!-- The influence of the arc flags with which the arc is drawn -->
<path
fill="none"
stroke="red"
d="M 6,10
A 6 4 10 1 0 14,10" />
<path
fill="none"
stroke="lime"
d="M 6,10
A 6 4 10 1 1 14,10" />
<path
fill="none"
stroke="purple"
d="M 6,10
A 6 4 10 0 1 14,10" />
<path
fill="none"
stroke="pink"
d="M 6,10
A 6 4 10 0 0 14,10" />
</svg>
閉合路徑
閉合路徑指令從當前位置繪製一條直線到路徑的第一個點。
| 命令 | 引數 | 注意 |
|---|---|---|
| Z, z | 透過連線路徑的最後一個點與其初始點來關閉當前子路徑。如果這兩個點位於不同的座標,則在這兩個點之間繪製一條直線。 |
注意:使用ClosePath關閉的形狀的外觀可能與使用其他命令之一繪製到原點的線來關閉的形狀的外觀不同,因為線端是連線在一起的(根據stroke-linejoin設定),而不是僅僅放在相同的座標上。
示例
<svg viewBox="0 -1 30 11" xmlns="http://www.w3.org/2000/svg">
<!--
An open shape with the last point of
the path different to the first one
-->
<path
stroke="red"
d="M 5,1
l -4,8 8,0" />
<!--
An open shape with the last point of
the path matching the first one
-->
<path
stroke="red"
d="M 15,1
l -4,8 8,0 -4,-8" />
<!--
A closed shape with the last point of
the path different to the first one
-->
<path
stroke="red"
d="M 25,1
l -4,8 8,0
z" />
</svg>
規範
| 規範 |
|---|
| 可縮放向量圖形 (SVG) 2 # D屬性 |
| 可縮放向量圖形 (SVG) 1.1(第二版) # GlyphElementDAttribute |
| 可縮放向量圖形 (SVG) 1.1(第二版) # D屬性 |
瀏覽器相容性
BCD 表格僅在瀏覽器中載入