下列的範例表示兩秒之後,正方形會移到 x=70 的地方。
<rect width="50" height="50" x="10" y="10" fill="#c00"> <set attributeName="x" to="40" begin="2s"> </rect> <rect width="50" height="50" x="10" y="10" fill="none" stroke="#000"/>
<rect width="50" height="50" x="10" y="10" fill="#09c"> <animate attributeName="x" to="60" dur="2s" repeatCount="indefinite"> </rect> <rect width="50" height="50" x="10" y="10" fill="none" stroke="#000"/>
<circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" /> <rect id="blue-rectangle" width="50" height="50" x="25" y="100" fill="#0099cc"> <animate href="#orange-circle" // 目標元件 attributeName="cx" from="50" to="450" dur="5s" begin="click" // 單擊開動 fill="freeze" // 結束時停住 id="circ-anim"/> // 元件 id 以便另一元件引用 <animate href="#blue-rectangle" attributeName="x" from="50" to="425" dur="5s" begin="circ-anim.begin + 1s" // 圓球開動後1秒開動 fill="freeze" id="rect-anim"/>
單擊橘色圓形開動
參考A Guide to SVG Animations (SMIL)
<rect width="60" height="60" x="50" y="50" fill="#0c0"> <animateTransform attributeName="transform" type="rotate" from="0,80,80" to="360,80,80" dur="2s" repeatCount="indefinite" /> </rect> <rect width="60" height="60" x="50" y="50" fill="none" stroke="#000">
例題:
<animateTransform attributeName="transform" type="rotate" from="0 0 0" to="360 0 0" begin="0s" dur="10s" repeatCount="indefinite" />如上面所述,要控制 transform 屬性,所以 attributeName="transform",接著 type 參數就看你想要 transform 的類型是什麼,rotate、scale 都可以。其餘 from、to、begin、dur等參數都與之前的相同,用來指定動畫的起始終點值、時間長度與執行次數。
<rect width="20" height="10" x="0" y="-10" fill="#09c"> <animateMotion dur="5s" path= " M7.4,15.3c17,20.4,48.8,38,91.6,27.8c79.5-18.9,107.4,48.2,69.4,48.2 c-33.9,0-15.2-58.1,65.4-41.7c26.2,5.3,63.2-19.1,79.1-34.3" repeatCount="indefinite" rotate="auto" /> </rect> <path fill="none" stroke="#000" d="M7.4,15.3c17,20.4,48.8,38,91.6,27.8 c79.5-18.9,107.4,48.2,69.4,48.2c-33.9,0-15.2-58.1,65.4-41.7 c26.2,5.3,63.2-19.1,79.1-34.3"/>
</!--軌跡--> <path d="M10,50 q60,50 100,0 q60,-50 100,0" stroke="black" stroke-width="2" /> <g> </!-- Rick 飛船 svg--> <animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" begin="0s" dur="10s" repeatCount="indefinite" /> </g>
完整程式: rick-animate-motion
animateMotion 還有個特別的屬性值 rotate,
用來指定是否要隨著路徑移動的同時,
旋轉綁定的 svg 物件,可以設定為 auto 或 auto-reverse:
<animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" begin="0s" dur="10s" repeatCount="indefinite" rotate="auto" />
四種 SVG 動畫元件,除了個別拿來使用外,這些元件是能夠組合在一起使用的。
只要個別把對應的動畫套用在想要的 svg shape 上即可。
舉例來說,可以讓 Rick 旋轉的同時,顏色改變、眼睛轉動(可右鍵看 svg 原始碼,在裡面可以找到多個動畫元件):
<animate attributeName="fill" from="#B0DDF7" to="#f79a0e" // 顏色改變 dur="3s" repeatCount="indefinite" /> <animate attributeName="cx" from="56.7573" to="64.7573" // 眼睛轉動 dur="2s" repeatCount="indefinite" /> <animateTransform attributeName="transform" // Rick 旋轉 type="rotate" from="0 0 0" to="360 0 0" begin="0s" dur="10s" repeatCount="indefinite" />
例如前面 animateTransform 的例子,我們可以改為:
<animateTransform attributeName="transform" type="rotate" from="0 0 0" by="360" begin="0s" dur="10s" repeatCount="indefinite" />再來看看 values,這個剛剛的範例都沒出現,它的功用是來補足 from、to、by 的不足。
<animateTransform attributeName="transform" type="translate" values="20;120;20" begin="0s" dur="3s" repeatCount="indefinite" />
<!-- Rick head --> <g> <animateTransform attributeName="transform" type="scale" values="1;1.2;1" begin="ship.end" dur="3s" repeatCount="indefinite"blue /> </g> <!-- spaceship --> <g> <animateTransform id="ship" attributeName="transform" type="translate" values="20;120;20" begin="rickhead.click" dur="3s" /> </g>
單擊太空船中的 rick 頭啟動
<path id="path1" fill="none" stroke="#000" d="M7.4,15.3c17,20.4,48.8,38,91.6,27.8 c79.5-18.9,107.4,48.2,69.4,48.2c-33.9,0-15.2-58.1,65.4-41.7 c26.2,5.3,63.2-19.1,79.1-34.3"/> <rect width="20" height="10" x="0" y="-10" fill="#09c"> <animateMotion dur="5s" repeatCount="indefinite" rotate="auto" /> <mpath xlink:href="#path1"/> </animateMotion> </rect>
<circle id="growndot" r="50" fill="#02324b" transform="translate(50 50)" /> <animateTransform id="grow" xlink:href="#growndot" attributeName="transform" type="scale" additive="sum" from="0 0" to="1 1" begin="0s" dur="3s" repeatCount="indefinite" end="click" />
例如:透過 animateTransform 先將圖案放大再旋轉。
<animateTransform attributeName="transform" type="scale" by="1.1" begin="0s" dur="5s" repeatCount="indefinite" additive="sum" /> <animateTransform attributeName="transform" type="rotate" from="0 0 0" to="360 0 0" begin="0s" dur="5s" repeatCount="indefinite" additive="sum" />