SVG <path>


SVG 路徑 - <path>

<path> 元素用於定義一個路徑。

下麵的命令可用於路徑數據:

  • M = moveto
  • L = lineto
  • H = horizontal lineto
  • V = vertical lineto
  • C = curveto
  • S = smooth curveto
  • Q = quadratic Bézier curve
  • T = smooth quadratic Bézier curveto
  • A = elliptical Arc
  • Z = closepath

注意:以上所有命令均允許小寫字母。大寫表示絕對定位,小寫表示相對定位。


實例 1

上面的例子定義了一條路徑,它開始於位置150 0,到達位置75 200,然後從那裏開始到225 200,最後在150 0關閉路徑。

下麵是SVG代碼:

實例

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <path d="M150 0 L75 200 L225 200 Z" /> </svg>

對於Opera用戶:(右鍵單擊SVG圖形預覽源)。


實例 2

下麵的例子創建了一個二次方貝塞爾曲線,A 和 C 分別是起點和終點,B 是控制點:

下麵是SVG代碼:

實例

<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <path id="lineAB" d="M 100 350 l 150 -300" stroke="red" stroke-width="3" fill="none" /> <path id="lineBC" d="M 250 50 l 150 300" stroke="red" stroke-width="3" fill="none" /> <path d="M 175 200 l 150 0" stroke="green" stroke-width="3" fill="none" /> <path d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" /> <!-- Mark relevant points --> <g stroke="black" stroke-width="3" fill="black"> <circle id="pointA" cx="100" cy="350" r="3" /> <circle id="pointB" cx="250" cy="50" r="3" /> <circle id="pointC" cx="400" cy="350" r="3" /> </g> <!-- Label the points --> <g font-size="30" font="sans-serif" fill="black" stroke="none" text-anchor="middle"> <text x="100" y="350" dx="-30">A</text> <text x="250" y="50" dy="-10">B</text> <text x="400" y="350" dx="30">C</text> </g> </svg>

對於Opera用戶:(右鍵單擊SVG圖形預覽源)。

複雜嗎?是的!!由於在繪製路徑時的複雜性,強烈建議使用SVG編輯器來創建複雜的圖形。