SVG 不只是靜態的向量圖形語言,它從設計之初就重視與使用者之間的互動。歸納起來,SVG 的互動性主要展現在以下幾個方面:
cursor 屬性,可以精準捕捉使用者滑鼠的移動軌跡。zoomAndPan 屬性,就能輕鬆實現縮放與平移效果。只要為 SVG 元素掛接事件,再搭配指令稿語言,就能完成各種互動任務。SVG 支援大部分 DOM2 事件,例如 onfocusin、onfocusout、onclick、onmousedown、onmouseup、onmousemove、onmouseout、onload、onresize、onscroll 等。
除此之外,SVG 還提供了幾個動畫專屬的事件,像是 onbegin、onend、onrepeat 等,讓你能更精細地控制動畫的時序。由於事件的概念大家已經相當熟悉,這裡就不再贅述。
SVG 以文字來定義圖形,這種文件結構天生就適合製作動畫。想改變圖形的位置、大小或顏色,只要調整對應的屬性即可。事實上,SVG 為事件處理與動畫準備了許多專用屬性,其中不少更是為動畫量身打造。在 SVG 中,實作動畫主要有三種途徑:
https://www.w3.org/TR/2008/REC-SMIL3-20081201/。下面的範例涵蓋了 SVG 幾種最基本的動畫元素,把它貼進 HTML 文件的 body 中執行,就能看到動畫效果:
<svg width="8cm" height="3cm" viewBox="0 0 800 300"
xmlns="https://www.w3.org/2000/svg" version="1.1">
<desc>基本動畫元素</desc>
<rect x="1" y="1" width="798" height="298"
fill="none" stroke="blue" stroke-width="2" />
<!-- 矩形位置和大小的動畫 -->
<rect id="RectElement" x="300" y="100" width="300" height="100"
fill="rgb(255,255,0)">
<animate attributeName="x" attributeType="XML"
begin="0s" dur="9s" fill="freeze" from="300" to="0" />
<animate attributeName="y" attributeType="XML"
begin="0s" dur="9s" fill="freeze" from="100" to="0" />
<animate attributeName="width" attributeType="XML"
begin="0s" dur="9s" fill="freeze" from="300" to="800" />
<animate attributeName="height" attributeType="XML"
begin="0s" dur="9s" fill="freeze" from="100" to="300" />
</rect>
<!-- 建立新的使用者座標空間,後續變換皆針對新座標系 -->
<g transform="translate(100,100)">
<text id="TextElement" x="0" y="0"
font-family="Verdana" font-size="35.27" visibility="hidden">
It's alive!
<set attributeName="visibility" attributeType="CSS" to="visible"
begin="3s" dur="6s" fill="freeze" />
<animateMotion path="M 0 0 L 100 100"
begin="3s" dur="6s" fill="freeze" />
<animate attributeName="fill" attributeType="CSS"
from="rgb(0,0,255)" to="rgb(128,0,0)"
begin="3s" dur="6s" fill="freeze" />
<animateTransform attributeName="transform" attributeType="XML"
type="rotate" from="-30" to="0"
begin="3s" dur="6s" fill="freeze" />
<animateTransform attributeName="transform" attributeType="XML"
type="scale" from="1" to="3" additive="sum"
begin="3s" dur="6s" fill="freeze" />
</text>
</g>
</svg>
SVG 動畫元素的屬性大致可以分成四大類,分別負責指定目標、控制時間、定義數值以及決定是否採用增量方式。
xlink:href:指向要執行動畫的元素。該元素必須定義在目前的 SVG 文件片段中;若未指定此屬性,動畫會套用在自己的父元素上。
attributeName:指定要套用動畫的屬性名稱。若該屬性帶有命名空間(別忘了 SVG 本質上是 XML 文件),就必須一併寫上命名空間。以下範例為 xlink 取了不同別名,animate 指定屬性時便帶上了命名空間:
<svg version="1.1" xmlns="https://www.w3.org/2000/svg"
xmlns:xlink="https://www.w3.org/1999/xlink">
<title>Demonstration of the resolution of namespaces for animation</title>
<g xmlns:a="https://www.w3.org/1999/xlink">
<animate attributeName="a:href" xlink:href="#foo" dur="2s"
to="two.png" fill="freeze"/>
</g>
<g xmlns:b="https://www.w3.org/1999/xlink"
xmlns:xlink="https://example.Net/bar">
<image xml:id="foo" b:href="one.png" x="35" y="50" width="410" height="160"/>
</g>
</svg>
attributeType = "CSS | XML | auto(預設值)":指定屬性值的命名空間來源。
CSS:代表 attributeName 指定的是 CSS 屬性。XML:代表 attributeName 指定的是 XML 預設命名空間下的屬性。auto:先在 CSS 屬性中查找,找不到時再到 XML 預設命名空間下尋找。這類屬性掌控動畫的時間線,包含如何開始與結束、是否重複、是否保留結束狀態等。
media(依內部多媒體元素長度)、indefinite(無限)。always 隨時可重啟;whenNotActive 僅能在未播放時重啟;never 則不允許重啟。indefinite 代表無限重複。indefinite 代表無限重複。freeze 停留在最後狀態,remove 則回到動畫前的模樣。時鐘格式支援的合法寫法如下:
:30:03 = 2 小時 30 分 3 秒 :00:10.25 = 50 小時 10 秒 250 毫秒 :33 = 2 分 33 秒 :10.5 = 10.5 秒 .2h = 3.2 小時 = 3 小時 12 分 min = 45 分鐘 s = 30 秒 ms = 5 毫秒 .5s = 500 毫秒 :00.005 = 5 毫秒
這類屬性定義了動畫屬性的取值方式,本質上是在描述關鍵frame與插值演算法。
當需要的不是絕對值而是增量值時,就可以派上用場。
additive = "replace(預設值) | sum":控制動畫是否為增量式。sum 會疊加在相關屬性值或其他低優先級動畫之上;replace 則是覆蓋。以下範例展示矩形寬度遞增的效果:
<rect width="20px" ...>
<animate attributeName="width" from="0px" to="10px" dur="10s"
additive="sum"/>
</rect>
accumulate = "none(預設值) | sum":控制重複動畫的效果是否累積。none 表示不累積,sum 表示每次迭代都會累加。此屬性僅對重複動畫有意義:
<rect width="20px" ...>
<animate attributeName="width" from="0px" to="10px" dur="10s"
additive="sum" accumulate="sum" repeatCount="5"/>
</rect>
SVG 提供了下列幾種動畫元素:
calcMode 預設為 paced;path 指定移動路徑,格式與 path 元素的 d 屬性一致;keyPoints 是一串 0~1 的浮點數,代表對應時間點移動的距離比例;rotate 控制移動時的旋轉角度,auto 隨路徑方向旋轉,auto-reverse 則朝相反方向。此外它的 from/by/to/values 都是座標對,x 與 y 以逗號或空格分隔,各座標對之間以分號隔開。指定路徑有兩種方式:直接給 path 屬性賦值,或用 mpath 子元素引用;兩者同時存在時 mpath 優先,且都比 values/from/by/to 優先。type 指定變換類型(translate、scale、rotate、skewX、skewY,預設 translate);from/by/to 的值為對應變換的參數,values 則是一組以分號隔開的這類數值。基本上所有圖形元素(path、rect、ellipse、text、image 等)與容器元素(svg、g、defs、use、switch、clipPath、mask 等)都支援動畫,大多數屬性也都能套用動畫效果,細節可查閱官方文件。
除了動畫元素,SVG 動畫也能用指令稿完成。以下範例透過 DOM 操作,讓文字在 5 秒內逐漸放大並變得更不透明:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="4cm" height="2cm" viewBox="0 0 400 200"
xmlns="https://www.w3.org/2000/svg"
onload="StartAnimation(evt)" version="1.1">
<script type="application/ecmascript"><![CDATA[
var timevalue = 0;
var timer_increment = 50;
var max_time = 5000;
var text_element;
function StartAnimation(evt) {
text_element = evt.target.ownerDocument.getElementById("TextElement");
ShowAndGrowElement();
}
function ShowAndGrowElement() {
timevalue = timevalue + timer_increment;
if (timevalue > max_time)
return;
// 逐步放大文字,最終放大為 20 倍
scalefactor = (timevalue * 20.) / max_time;
text_element.setAttribute("transform", "scale(" + scalefactor + ")");
// 讓文字越來越不透明
opacityfactor = timevalue / max_time;
text_element.setAttribute("opacity", opacityfactor);
// 每隔 timer_increment 毫秒再次呼叫
setTimeout("ShowAndGrowElement()", timer_increment)
}
window.ShowAndGrowElement = ShowAndGrowElement
]]></script>
<rect x="1" y="1" width="398" height="198"
fill="none" stroke="blue" stroke-width="2"/>
<g transform="translate(50,150)" fill="red" font-size="7">
<text id="TextElement">SVG</text>
</g>
</svg>