這個實例來看看如何使用anime.js來實現一個形變(morphing)動畫。數組
至於什麼是morphing動畫,看完下面這個圖就知道了。svg
簡而言之,就是不一樣形狀之間能平滑的轉換。這個用SVG來作羹適合不過了,由於SVG自己的形狀就是有各類不一樣座標之間的線段繪製而成的。從形狀到另一個形狀的轉化就是移動座標點而已。oop
提及來貌似挺簡單的,其實真正要實現起來仍是有點複雜的。今天咱們來使用以前屢次提到過的anime.js這個js庫來實現morphing動畫。動畫
關於anime.js的基礎知識,能夠去看看之前發過的這篇文章。網站
在anime.js中提供了作動畫必不可少時間軸的管理功能,能夠輕鬆的控制和管理動畫的播放。spa
具體能夠去官網的文檔看看,Timeline。3d
下面來看看要實現的效果:code
使用anime.js實現起來很是簡單,只須要準備兩個形狀的SVG便可。xml
觀察這個效果,能夠發現這個效果主要是形狀之間的轉化,同時還有顏色的變化。ci
首先默認顯示的F這個字母,結構以下:
<h2>SVG morph in HTML, CSS & JS</h2> <p>Powered by <a href="http://anime-js.com/" target="_blank">anime-js</a></p> <svg class="social" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"> <path class="path" style="fill:#3b5998;fill-rule:evenodd;stroke:#3b5998;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 41.416254,90 c -0.327378,-7.4702 0.20833,-32.7284 0,-39.901 -5.386902,-0.2083 -4.521603,0.3274 -9.848987,0 0.20833,-5.50595 0.36436,-7.66666 0.126269,-13.32142 4.646472,0.0181 3.439989,-0.009 9.848987,-0.1894 0.09586,-3.7736 0.133082,-3.0791 0.126269,-7.38674 0.18259,-3.73943 -0.486609,-10.54308 4.293149,-14.96288 4.779758,-4.4198 13.606811,-3.64808 22.223356,-3.53554 -0.04417,5.73754 -0.03936,9.37986 0,12.87945 -5.049924,0.46388 -7.309188,-0.33689 -10.85914,1.26269 -1.403378,3.17794 -1.569601,4.80531 -1.262691,11.93242 3.147964,-0.13336 8.201788,-0.1378 12.626907,0 -0.995158,6.00899 -0.948285,7.62376 -1.767767,13.06882 -3.676625,0.088 -5.605721,-0.1488 -11.111678,0 -0.148814,6.756 0.357147,33.0107 0,40.1536 -6.428576,0.1786 -8.174438,-0.03 -14.394674,0 z" /> </svg>
接下來就是使用anime.js來實現形變更畫效果。
首先聲明一個時間軸:
var socialTimeline = anime.timeline({ autoplay: true, direction: 'alternate', loop: true });
中間的幾個參數也很是容易理解,自動循環來回播放效果。
下面就是具體動畫效果的編寫,顯示形狀的變化,即從字母F轉變爲twitter的logo。
代碼以下:
socialTimeline .add({ targets: '.path', d: { value: [ "m 41.416254,90 c -0.327378,-7.4702 0.20833,-32.7284 0,-39.901 -5.386902,-0.2083 -4.521603,0.3274 -9.848987,0 0.20833,-5.50595 0.36436,-7.66666 0.126269,-13.32142 4.646472,0.0181 3.439989,-0.009 9.848987,-0.1894 0.09586,-3.7736 0.133082,-3.0791 0.126269,-7.38674 0.18259,-3.73943 -0.486609,-10.54308 4.293149,-14.96288 4.779758,-4.4198 13.606811,-3.64808 22.223356,-3.53554 -0.04417,5.73754 -0.03936,9.37986 0,12.87945 -5.049924,0.46388 -7.309188,-0.33689 -10.85914,1.26269 -1.403378,3.17794 -1.569601,4.80531 -1.262691,11.93242 3.147964,-0.13336 8.201788,-0.1378 12.626907,0 -0.995158,6.00899 -0.948285,7.62376 -1.767767,13.06882 -3.676625,0.088 -5.605721,-0.1488 -11.111678,0 -0.148814,6.756 0.357147,33.0107 0,40.1536 -6.428576,0.1786 -8.174438,-0.03 -14.394674,0 z", "m 10.44335,90 c 11.073313,0.3952 19.483106,-1.8358 23.901837,-7.1603 -7.9736,-1.4292 -11.832311,-4.1933 -15.078321,-11.0837 3.459698,0.8219 5.795894,0.6358 7.606781,-0.607 -7.19593,-1.719 -12.734543,-6.7971 -13.741664,-15.836 2.766355,1.55307 5.466848,2.66623 7.828682,2.0203 -4.336544,-2.92911 -9.838998,-10.47636 -5.555839,-22.47589 8.400675,11.87052 23.824269,17.67568 33.840111,17.67767 -0.936406,-9.74688 5.88057,-19.46521 15.302849,-19.97853 8.13118,-0.50719 10.57457,4.01944 12.476346,4.82624 3.644547,0.13419 7.393301,-1.74401 10.354063,-3.53553 -1.380842,4.47157 -5.06769,5.62903 -6.313453,8.58629 5.42317,0.41513 5.891376,-1.53111 8.333758,-2.0203 -2.071414,3.75017 -5.393863,5.00034 -7.323606,8.08122 -1.633654,16.12573 -5.16049,27.57123 -14.647212,36.36553 -13.825764,11.3764 -34.755458,17.369 -56.984332,5.14 z" ], duration: 700, delay: 200, easing: 'easeInOutQuart' },
選中路徑元素,而後選中要改變的屬性,咱們這裏是要改變路徑的值即d屬性,直接寫入要變化形狀的值就能夠了。而後是動畫時間,延遲和動畫曲線等動畫參數的調整,讓動畫更天然。
後面是改變路徑形狀的填充和邊框顏色,很是簡單。
好比改變填充顏色:
fill: { value: ['#3b5998', '#4099ff'], duration: 700, delay: 200, easing: 'easeInOutQuart' },
這裏顏色的值,是一個數組。在執行的時候,anime.js會自動讀取數組裏顏色的值,填充到路徑中去,從而實現兩個形狀填充顏色的動畫。
經過上面一個簡簡單單的動畫,能夠發現使用anime.js來作SVG的動畫,也很是的方便,主要是輕量級。
最近作了一個關於SVG的應用的技術分享網站svgtrick.com,會同步一些文章到這裏來,更多的關於SVG方面的技術知識能夠去網站看看。