前面已經介紹了大量基礎圖形元素,也談過組合與重用的相關元素。這一篇先把 SVG 文件結構中剩下的元素做個整體清點,再繼續往 SVG 的其他特性推進。
SVG 文件的元素大致可以歸成以下幾類:
圖形元素、漸變元素、文字、圖像與組合前面都談過了,以下補充幾個與文件結構相關的元素。
svg 元素內部可以按任意順序放置其他元素,也允許巢狀的 svg。常用屬性大致就是 id、class、x、y、width、height、viewBox、preserveAspectRatio,以及 fill 與 stroke 的相關設定。
它支援的事件也都是常見的那一批:onload、onmouseover、onmousemove、onmousedown、onmouseup、onclick、onfocusin、onfocusout、onresize、onscroll、onunload 等。完整的屬性與事件清單可對照官方文檔。
每個容器元素(可以包含其他容器或圖形元素的元素,例如 a、defs、glyph、g、marker、mask、missing-glyph、pattern、svg、switch、symbol)以及圖形元素,都可以包含 desc 與 title。這兩者都是輔助性質的元素,用來說明所處的情境,內容為純文字;SVG 文件被渲染時,它們不會出現在畫面上。
兩者的差別其實不大,只是 title 在部分實作中會作為提示資訊(tooltip)呈現,所以通常建議把 title 放在父元素的第一個位置。
<svg xmlns="https://www.w3.org/2000/svg" version="1.1" width="4in" height="3in">
<g>
<title>Company sales by region</title>
<desc>
This is a bar chart which shows
company sales by region.
</desc>
<!-- Bar chart defined as vector data -->
</g>
</svg>
實務上建議為最外層的 svg 元素加上 title 說明,可讀性會好很多。
標記定義了一個要附加到一或多個頂點(path、line、polyline、polygon 的頂點)上的圖形。箭頭就是最典型的用法——把標記附加到 path、line 或 polyline 的起點或終點;而多點標記則是把同一個標記附加到所有頂點上。
標記由 marker 元素定義,使用時在 path、line、polyline 或 polygon 上設定 marker、marker-start、marker-mid、marker-end 等屬性即可。
<svg width="4in" height="2in"
viewBox="0 0 4000 2000" version="1.1"
xmlns="https://www.w3.org/2000/svg">
<defs>
<marker id="Triangle"
viewBox="0 0 10 10" refX="0" refY="5"
markerUnits="strokeWidth"
markerWidth="4" markerHeight="3"
orient="auto">
<path d="M 0 0 L 10 5 L 0 10 z" />
</marker>
</defs>
<desc>Placing an arrowhead at the end of a path.</desc>
<path d="M 1000 750 L 2000 750 L 2500 1250"
fill="none" stroke="black" stroke-width="100"
marker-end="url(#Triangle)" />
</svg>
markerUnits = "strokeWidth | userSpaceOnUse"
定義 markerWidth、markerHeight 以及 marker 內部內容所使用的座標系統。預設值 strokeWidth 代表這些數值的單位等同於「引用該 marker 之圖形元素的 stroke-width」;userSpaceOnUse 則代表採用引用者本身的使用者座標系。
用上面的例子來說,marker 的寬高由 markerWidth、markerHeight 決定,但千萬別搞混——marker 內部的 path 使用的是 viewBox 所建立的新使用者座標系。
refX、refY
定義「被引用的點」要對齊到 marker 上的哪個座標。以上面的例子而言,被引用的點是 path 的終點,要把它對齊到 marker 的 (0, 5) 位置。注意 refX、refY 使用的是經過 viewBox 變換後的最終使用者座標系。
markerWidth、markerHeight
marker 視窗的寬與高。
orient
定義 marker 的旋轉角度,可以給一個具體角度,也可以直接設為 auto。auto 代表 x 軸正向依下列規則旋轉:
要讓圖形元素引用 marker,主要用這三個屬性:
三者的取值可以是 none(不引用)、某個 marker 的引用,或 inherit。
SVG 中幾乎所有屬性都能透過 CSS 與元素關聯,而且所有 CSS 屬性在 SVG 圖像裡都可用。你可以直接用 style 屬性設定樣式,也可以引用外部樣式表。但要注意:XML 文件不應該直接解析樣式表(因為裡面偶爾會出現造成解析問題的字元),必須包在 CDATA 區段中;腳本也是同樣的道理。
CSS 範例:
<svg width="400" height="200" xmlns="https://www.w3.org/2000/svg">
<desc>Text</desc>
<defs>
<style type="text/css">
<![CDATA[
.abbreviation { text-decoration: underline; }
]]>
</style>
</defs>
<g>
<text x="20" y="50" font-size="30">Colors can be specified</text>
<text x="20" y="100" font-size="30">by their
<tspan fill="rgb(255,0,0)" class="abbreviation">R</tspan>
<tspan fill="rgb(0,255,0)" class="abbreviation">G</tspan>
<tspan fill="rgb(0,0,255)" class="abbreviation">B</tspan> values
</text>
<text x="20" y="150" font-size="30">or by keywords such as</text>
<text x="20" y="200">
<tspan style="fill:lightsteelblue;font-size:30">lightsteelblue</tspan>,
</text>
</g>
</svg>
腳本範例(點擊橢圓時切換填色):
<svg width="500" height="300" xmlns="https://www.w3.org/2000/svg">
<desc>Scripting the onclick event</desc>
<defs>
<script type="text/ecmascript">
<![CDATA[
function hideReveal(evt) {
var imageTarget = evt.target;
var theFill = imageTarget.getAttribute("fill");
if (theFill == 'white')
imageTarget.setAttribute("fill", "url(#notes)");
else
imageTarget.setAttribute("fill", "white");
}
]]>
</script>
<pattern id="notes" x="0" y="0" width="50" height="75"
patternTransform="rotate(15)"
patternUnits="userSpaceOnUse">
<ellipse cx="10" cy="30" rx="10" ry="5" />
<line x1="20" y1="30" x2="20" y2="0"
stroke-width="3" stroke="black" />
<line x1="20" y1="0" x2="30" y2="5"
stroke-width="3" stroke="black" />
</pattern>
</defs>
<ellipse onclick="hideReveal(evt)" cx="175" cy="100" rx="125" ry="60"
fill="url(#notes)" stroke="black" stroke-width="5" />
</svg>
條件處理屬性用來控制所在元素是否渲染,大多數元素(尤其是圖形元素)都可以指定。這類屬性共有三個:requiredFeatures、requiredExtensions 與 systemLanguage。它們本質上是一組測試,都允許傳入一個值清單(前兩者以空格分隔,語言那個以逗號分隔),預設值皆為 true。
switch 元素則提供了「依條件選擇渲染對象」的能力。它是容器元素,內部可以放圖形元素、解釋性元素、動畫元素,以及 a、foreignObject、g、image、svg、switch、text、use 等。瀏覽器會依序檢查各直接子元素的條件處理屬性,渲染第一個條件成立的子元素,其餘全部忽略。
這些屬性與 display 一樣,只影響「直接使用該屬性的元素」,不會影響被引用的元素(例如 use 所引用的內容)。簡單說,它們會作用於 a、altGlyph、foreignObject、textPath、tref、tspan、animate、animateColor、animateMotion、animateTransform、set 等元素,而不會作用於 defs、cursor、mask、clipPath、pattern 等元素(這些元素本身不是可渲染的,或者只是引用別的元素)。
注意:子元素的 display 與 visibility 屬性值,並不影響 switch 的條件判斷結果。
<switch>
<rect requiredFeatures="https://www.w3.org/TR/SVG11/feature#Filter"
x="10" y="10" width="322" height="502" opacity="0.6"
fill="black" stroke="none" filter="url(#gblurshadow)" />
<rect x="10" y="10" width="322" height="502" opacity="0.6"
fill="black" stroke="none" />
</switch>
這段的意思很直白:瀏覽器若支援 filter 特性,就畫上面那個帶 filter 的矩形;不支援則畫下面那個。
實務上最常用到的其實是 systemLanguage,也就是多語言處理能力:
<svg xmlns="https://www.w3.org/2000/svg" version="1.1" width="5cm" height="5cm">
<switch>
<text x="10" y="20" systemLanguage="de">de-HAHA</text>
<text x="10" y="20" systemLanguage="en">en-haha</text>
</switch>
</svg>
瀏覽器會依使用者語言環境挑出對應的那一段文字顯示。