SVG animation 回顧

想起來學習SVG動畫徹底是由於作比賽時,須要用到沿着運動路徑運動,做爲一個前端萌新,css3顯然沒法作到,就現學了SVG動畫。

  1、 SVG animation元素

  1. <set>
  2. <animate>
  3. <animateColor>
  4. <animateTransform>
  5. <animateMotion>

1. setcss

set雖然不能觸發連續的動畫,可是,其仍是能夠實現基本的延遲功能。就是指:能夠在特定時間以後修改某個屬性值(也能夠是CSS屬性值)。前端

<svg width="320" height="320" xmlns="http://www.w3.org/2000/svg">
  <g> 
    <text font-family="microsoft yahei" font-size="120" y="160" x="160">
      3s後移動
      <set attributeName="x" attributeType="XML" to="60" begin="3s" />
    </text>
  </g>
</svg>

2. animatecss3

基礎動畫元素。實現單屬性的動畫過渡效果。相似Snap.svg的animate()方法支持的動畫效果瀏覽器

<svg width="320" height="320" xmlns="http://www.w3.org/2000/svg">
  <g> 
    <text font-family="microsoft yahei" font-size="120" y="160" x="160">
    動畫過渡
      <animate attributeName="x" from="160" to="60" begin="0s" dur="3s" repeatCount="indefinite" />
    </text>
  </g>
</svg>

3. animateColor
一看就知道是顏色動畫。不過,animate能夠實現其功能與效果,所以,此屬性已經被廢棄。逝者已矣~

4. animateTransform
一看就知道實現transform變換動畫效果的。知識是一脈相承的,這裏的transform變換與CSS3的transform變換,以及Snap.svg.js中的transform()方法都是一個路數。svg

<svg width="320" height="320">
 <g>
     <text font-family="microsoft yahei" font-size="80" y="100" x="100">
    規模從1變大到1.5
    </text>
    <animateTransform attributeName="transform" begin="0s" dur="3s" type="scale" from="1" to="1.5" repeatCount="indefinite"/>
 </g> 
</svg>

5. animateMotion
讓SVG各類圖形沿着特定的path路徑運動,跟上面的SVG代碼相比,少個組標籤g元素。無妨。只要動畫元素是圖形元素子元素就能夠。rotate="auto"學習

<svg width="360" height="200">
  <text font-family="microsoft yahei" font-size="40" x="0" y="0" fill="#cd0000">沿路徑運動,方向自動
  <animateMotion path="M10,80 q100,120 120,20 q140,-50 160,0" begin="0s" dur="3s" rotate="auto" repeatCount="indefinite"/> </text> </svg>

6. 自由組合
實際製做時候的動畫,不可能老是一個屬性修改。測試

<svg width="320" height="200">
    <text font-family="microsoft yahei" font-size="120" y="160" x="160">位置透明度同時變化 
        <animate attributeName="x" from="160" to="60" begin="0s" dur="3s" repeatCount="indefinite" /> 
        <animate attributeName="opacity" from="1" to="0" begin="0s" dur="3s" repeatCount="indefinite" /> 
    </text> 
</svg>

2、SVG animation參數詳解

 1. attributeName = <attributeName>

     要變化的元素屬性名稱。動畫

  ① 能夠是元素直接暴露的屬性,例如,text元素上的x, y或者font-size; ② 能夠是CSS屬性。例如,透明度opacity.spa

 2. attributeType = 「CSS | XML | auto」

   attributeType支持三個固定參數,CSS/XML/auto.rest

  用來代表attributeName屬性值的列表。x, y以及transform就屬於XML, opacity就屬於CSS. auto爲默認值,自動判別的意思(其實是先當成CSS處理,若是發現不認識,直接XML類別處理)。所以,若是你不確信某屬性是XML類別仍是CSS類別的時候,不設置attributeType值,直接讓瀏覽器本身去判斷,幾乎無差錯。

 3. from, to, by, values

  from = 「<value>「
      動畫的起始值。
  to = 「<value>「
      指定動畫的結束值。
  by = 「<value>「
      動畫的相對變化值。
  values = 「<list>「
      用分號分隔的一個或多個值,能夠看出是動畫的多個關鍵值點。

   相互之間仍是有制約關係的。有如下一些規則:

  1. (不考慮values)to,by兩個參數至少須要有一個出現。不然動畫效果沒有。to表示絕對值,by表示相對值。拿位移距離,若是from是100, to值爲160則表示移動到160這個位置,可是,若是by值是160,則表示移動到100+160=260這個位置。

  2. 若是to,by同時出現,則by打醬油,只識別to.
  3. 若是to,by,values都沒設置,天然沒動畫效果。若是任意(包括from)一個屬性的值不合法,規範上說是沒有動畫效果。(據測試,FireFox確實如此,可是Chrome特地作了容錯處理。如,原本是數值的屬性,寫了個諸如「a」這個不合法的值,其會看成「0」來處理,動畫效果依然存在。)

  4. values能夠是一個值或多值。但在Chrome瀏覽器下的測試,是一個值的時候是沒有動畫效果。多值時候有動畫效果。當values值設置並能識別時候,from, to, by的值都會被忽略。咱們實現動畫,不可能就是單純的從a位置到b位置,有時候,須要去c位置過渡下。此時,實際上有3個動畫關鍵點。而from, to/by只能駕馭兩個,此時就是values大顯身手的時候了!

<svg width="320" height="200" xmlns="http://www.w3.org/2000/svg">
     <text font-family="microsoft yahei" font-size="120" y="150" x="160">來回跑 
        <animate attributeName="x" values="160;40;160" dur="3s" repeatCount="indefinite" />
     </text> 
</svg>    

總結下,也就是from-to動畫、from-by動畫、to動畫、by動畫以及values動畫。

4. begin, end

begin指動畫開始的時間,看上去很簡單。設個時間,延遲嘛~~實際上非也非也,上面出現的beigin="3s"只是最簡單最基本的表示。

begin的定義是分號分隔的一組值。單值只是其中的狀況之一。例如,beigin="3s;5s"表示的是3s以後動畫走一下,6s時候動畫再走一下(若是以前動畫沒走完,會當即中止從頭開始)。若是一次動畫時間爲3s, 即dur="3s",同時沒有repeatCount屬性時候,咱們能夠看到動畫彷佛連續執行了2次。

begin的單值除了普通value,還有下面這些類別的value:
offset-value | syncbase-value | event-value | repeat-value | accessKey-value | media-marker-value | wallclock-sync-value | "indefinite"

① offset-value表示偏移值,數值前面有+或-. 應該指相對於documentdocument的begin值而言。
② syncbase-value基於同步肯定的值。語法爲:[元素的id].begin/end +/- 時間值. 就是說借用其餘元素的begin值再加加減減,這個能夠準確實現兩個獨立元素的動畫級聯效果。OK,看完下面的例子必定會豁然開朗,對於上面的offset-value也會有必定的認知。後面attributeName爲y的元素的begin值是x.end. x.end中的x就是上面一個animate元素的id值,而end是動畫元素都有的一個屬性,動畫結束的時間。所以,begin="x.end"意思就是,當id爲x的元素動畫結束的時候,我執行動畫。很是相似於PowerPoint動畫的「上一個動畫以後」的選項。還能夠增長一些偏移值,例如begin="x.end-1s", 就表示id爲x的元素動畫結束前一秒開始縱向移動

<svg width="320" height="200" xmlns="http://www.w3.org/2000/svg">
     <text font-family="microsoft yahei" font-size="120" y="160" x="160">id爲x的動畫結束了開始,無縫鏈接
        <animate id="x" attributeName="x" to="60" begin="0s" dur="3s" fill="freeze" /> 
        <animate attributeName="y" to="100" begin="x.end" dur="3s" fill="freeze" /> 
    </text> 
</svg>    

③ event-value這個表示與事件相關聯的值。相似於PowerPoint動畫的「點擊執行該動畫」。語法是:[元素的id].[事件類型] +/- 時間值. 舉個例子,點擊下圖的圓圈圈,馬兒它就會本身跑!

<svg id="svg" width="320" height="200" xmlns="http://www.w3.org/2000/svg"> 
    <circle id="circle" cx="100" cy="100" r="50"></circle> 
        <text font-family="microsoft yahei" font-size="120" y="160" x="160">點擊圓圈字會跑 
            <animate attributeName="x" to="60" begin="circle.click" dur="3s" /> 
        </text> 
</svg> 

注意,這類與事件關聯的SVG須要內聯在頁面中,不然click什麼的都是徒勞。

④ repeat-value指重複多少次以後幹嗎幹嗎。語法爲:[元素的id].repeat(整數) +/- 時間值. 舉個例子,下面這個馬兒會在水平運動2次以後,斜向運動,begin="x.repeat(2)"指id爲x的元素的動畫重複2次後執行~~

<svg width="320" height="200" xmlns="http://www.w3.org/2000/svg">
     <text font-family="microsoft yahei" font-size="120" y="160" x="160">x執行2次後再執行y 
        <animate id="x" attributeName="x" to="60" begin="0s" dur="3s" repeatCount="indefinite" />
        <animate attributeName="y" to="100" begin="x.repeat(2)" dur="3s" fill="freeze" />
     </text> 
</svg>    

⑤ accessKey-value定義快捷鍵。即按下某個按鍵動畫開始。語法爲:accessKey(" character "). character表示快捷鍵所在的字符,舉個例子,按下s鍵動畫走起。SVG代碼以下:

<svg width="320" height="200" xmlns="http://www.w3.org/2000/svg">
    <text font-family="microsoft yahei" font-size="120" y="160" x="160">按鍵盤s鍵執行
        <animate attributeName="x" to="60" begin="accessKey(s)" dur="3s" repeatCount="indefinite" />
    </text>
</svg>

⑥ wallclock-sync-value指真實世界的時鐘時間定義。時間語法是基於在ISO8601中定義的語法。

⑦ "indefinite"就是這個字符串值,表示「無限等待」。聽說須要beginElement()方法觸發或者指向該動畫元素的超連接(SVG中的a元素)。點擊咱們的svg, 觸發animate元素的beginElement()方法,前提是begin="indefinite".

<svg id="svg" width="320" height="200" xmlns="http://www.w3.org/2000/svg">         
    <text font-family="microsoft yahei" font-size="120" y="160" x="160">觸發事件後運動   
        <animate attributeName="x" to="60" begin="indefinite" dur="3s" />
    </text>
</svg>
var animate = document.getElementsByTagName("animate")[0]; 
    if (animate) {
         document.getElementById("svg").onclick = function() {     
            animate.beginElement();
    };
}

 a元素的xlink:href指向的咱們的動畫元素

<svg width="320" height="200" font-family="microsoft yahei" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
    <text font-size="120" y="160" x="160">點擊觸發<animate id="animate" attributeName="x" to="60" begin="indefinite" dur="3s" repeatCount="indefinite" />
    </text> 
    <a xlink:href="#animate"> 
    <text x="10" y="20" fill="#cd0000" font-size="30">點擊我</text> 
    </a>
</svg>

5. dur
後面兩種:常規時間值 | "indefinite".

「常規時間值」就是3s之類的正常值;"indefinite"指事件無限。動畫時間無限,實際上就是動畫壓根不執行的意思。所以,設置爲"indefinite"跟沒有dur是一個意思,與dur解析異常一個意思。

6. calcMode, keyTimes, keySplines
這幾個參數是控制動畫先快仍是先慢相似這樣做用的。

calcMode屬性支持4個值:discrete | linear | paced | spline. 中文意思分別是:「離散」|「線性」|「踏步」|「樣條」。

discrete
from值直接跳到 to值。
linear
animateMotion元素之外元素的 calcMode默認值。動畫從頭至尾的速率都是一致的。
paced
經過插值讓動畫的變化步調平穩均勻。僅支持線性數值區域內的值,這樣點之間「距離」的概念才能被計算(如 position, width, height等)。
若是」 paced「指定,任何 keyTimeskeySplines值都會打醬油。
spline
插值定義貝塞爾曲線。 spline點的定義在 keyTimes屬性中,每一個時間間隔控制點由 keySplines定義。

keyTimes = 「<list>」
跟上面提到的<list>相似,都是分號分隔一組值。keyTimes從名字上看是關鍵時間點的意思,大體就是這個意思。前面提到過values也是多值,這裏有一些約定的規則:

  1、keyTimes值的數目要和values一致,若是是from/to/by動畫,keyTimes就必須有兩個值。

  2、對於linearspline動畫,第一個數字要是0, 最後一個是1

  3、每一個連續的時間值必須比它前面的值大或者相等。

 paced模式下,keyTimes會被忽略;keyTimes定義錯誤,也會被忽略;durindefinite也會被忽略。

keySplines = 「<list>」
keySplines表示的是與keyTimes相關聯的一組貝塞爾控制點(默認0 0 1 1)。每一個控制點使用4個浮點值表示:x1 y1 x2 y2. 只有模式是spline時候這個參數纔有用,也是分號分隔,值範圍0~1,老是比keyTimes少一個值。

若是keySplines值不合法或個數不對,是沒有動畫效果的。

以下4個SVG,只展現重要部分代碼:

<animate attributeName="x" dur="5s" values="0; 20; 160" calcMode="linear" />

<animate attributeName="x" dur="5s" values="0; 20; 160" calcMode="paced"/>

<animate attributeName="x" dur="5s" values="0; 80; 160" keyTimes="0; .8; 1" calcMode="linear"/>

<animate attributeName="x" dur="5s" values="0; 80; 160" keyTimes="0; .8; 1" calcMode="spline"  keySplines=".5 0 .5 1; 0 0 1 1" />

就是values, keyTimes, keySplines三我的之間事情。values肯定動畫的關鍵位置,keyTimes肯定到這個關鍵點須要的時間,keySplines肯定的是每一個時間點段之間的貝塞爾曲線,也就是具體的緩動表現。平時CSS3寫的transition動畫效果,也是這麼回事,這是values值就兩個,因此,keyTimes只能是0;1, 貝塞爾曲線就只有一個,要不ease, 要不linear等

7. repeatCount, repeatDur

repeatCount表示動畫執行次數,能夠是合法數值或者」indefinite「(動畫循環到電腦死機)。
repeatDur定義重複動畫的總時間。能夠是普通時間值或者」indefinite(」動畫循環到電腦死機)。

動畫只玩執行完整3個 + 一個1/3個動畫。由於repeat總時間就10s而已。

<animate attributeName="x" to="60" dur="3s" repeatCount="indefinite" repeatDur="10s" />

8. fill

fill表示動畫間隙的填充方式。支持參數有:freeze | remove. 其中remove是默認值,表示動畫結束直接回到開始的地方。freeze「凍結」表示動畫結束後像是被凍住了,元素保持了動畫結束以後的狀態。

9. accumulate, additive

accumulate是累積的意思。支持參數有:none | sum. 默認值是none. 若是值是sum表示動畫結束時候的位置做爲下次動畫的起始位置。
additive控制動畫是否附加。支持參數有:replace | sum. 默認值是replace. 若是值是sum表示動畫的基礎知識會附加到其餘低優先級的動畫上,

<img ...>    
    <animateMotion begin="0" dur="5s" path="[some path]" additive="sum" fill="freeze" />    
    <animateMotion begin="5s" dur="5s" path="[some path]" additive="sum" fill="freeze" />    
    <animateMotion begin="10s" dur="5s" path="[some path]" additive="sum" fill="freeze" /> 
</img>

這裏輪到第二個動畫的時候,路徑是從第一個動畫路徑結束地方開始的,因而,3個動畫完美無縫鏈接起來了。

    <animateTransform attributeName="transform" type="scale" from="1" to="3" dur="10s" repeatCount="indefinite" additive="sum"/> 
    <animateTransform attributeName="transform" type="rotate" from="0 30 20" to="360 30 20" dur="10s" fill="freeze" repeatCount="indefinite" additive="sum"/>;

兩個動畫同時都是transform,都要使用一個type屬性,好在這個例子additive="sum"是累加的而不是replace替換。

因而,咱們就能夠是實現一邊旋轉一邊放大的效果。

10. restart

restart這個屬性誕生的背景以下:不少動畫呢,其觸發可能與事件相關。如今,存在這種狀況,但願點擊一次反應一次,以後再點擊就沒有反應。這種需求的出現迫使restart參數的出現。支持的參數有:always | whenNotActive | never.

always是默認值,表示老是,也就是點一次,觸發一次事件。whenNotActive表示動畫正在進行的時候,是不能重啓動畫的。never表示動畫是一波流。

11. min, max

min/max表示動畫執行最短和最長時間。支持參數爲時間值和"media"(媒介元素有效), max還支持indefinite.

4、動畫的暫停與播放

SVG animation中是有內置的API能夠暫停和啓動動畫的

// svg指當前svg DOM元素
// 暫停
    svg.pauseAnimations(); 
// 重啓動 
    svg.unpauseAnimations()
相關文章
相關標籤/搜索