d
Baseline 廣泛可用 *
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 是一個字串,包含一系列定義要繪製路徑的路徑命令。
| 值 | <string> |
|---|---|
| 預設值 | none |
| 可動畫的 | 是 |
將 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 屬性參考頁面示例。
路徑命令
路徑命令是定義要繪製路徑的指令。每個命令由一個命令字母和表示命令引數的數字組成。
SVG 定義了 6 種路徑命令型別,總共有 20 個命令
注意: 命令是區分大小寫的。大寫命令指定絕對座標,而小寫命令指定相對於當前位置的座標。
總是可以指定負值作為命令的引數
- 負角度將是逆時針方向;
- 絕對負 x 和 y 值被解釋為負座標;
- 相對負 x 值向左移動,相對負 y 值向上移動。
移動到路徑命令
移動到指令可以認為是拿起繪圖工具,然後將其放到其他地方——換句話說,移動當前點(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 和沿 y 軸移動 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 | 透過連線路徑的最後一點和其起始點來閉合當前子路徑。如果兩個點位於不同的座標處,則在這兩個點之間繪製一條直線。 |
注意: 使用 閉合路徑 閉合的形狀的外觀可能與使用其他命令繪製到原點閉合的形狀不同,因為線端是連線在一起的(根據 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>
規範
| 規範 |
|---|
| Scalable Vector Graphics (SVG) 2 # D屬性 |
| 可縮放向量圖形 (SVG) 1.1(第二版) # 字形元素D屬性 |
| 可縮放向量圖形 (SVG) 1.1(第二版) # D屬性 |
瀏覽器相容性
載入中…