SVG動畫

動畫原理

SVG動畫,就是元素的屬性值關于時間的變化。 如下圖來說,元素的某個屬性值的起始值(from)到結束值(to)在一個時間段(duration)根據時間函數(timing-function)計算出每一幀(frame)的插值(interpolation)作為變換的行為。

PS:SVG動畫是幀動畫,在SVG里也就是每秒設置多少個value值。

SVG動畫語法

SVG動畫是基于SMIL(Synchronized Multimedia Integration Language)語言的,全稱是同步多媒體集成語言。

SVG動畫使用

SVG元素使用動畫有兩種方式:

1. 被xlink:href引用

<animate  xlink:href="url(#rect1)"></animate> 

2. 包含在目標元素里

<rect  x="0"  ...><animate></animate>
</rect>

<animate>標簽

該標簽用于基本動畫。

參數描述
attributeName要變化屬性名稱
1.可以是元素直接暴露的屬性
2.可以是CSS屬性
attributeType?用來表明attributeName屬性值的類型
支持三個固定參數,CSS/XML/auto,默認值auto。
例如:x、 y以及transform就屬于XML, opacity就屬于CSS。
from起始值
起始值與元素的默認值是一樣的,該參數可省略。
to結束值
by相對from的變化值
PS:當有to值時,該值無效。
values動畫的多個關鍵值,用分號分隔。
dur持續時間
取值:常規時間值 | "indefinite"
repeatCount動畫執行次數
取值:合法數值或者“indefinite”
fill動畫間隙的填充方式
取值:freeze | remove(默認值)。
remove:表示動畫結束直接回到開始的地方。
freeze:表示動畫結束后保持了動畫結束之后的狀態。
calcMode控制動畫的快慢
取值:discrete | linear(默認值) | paced | spline.
中文意思分別是:“離散”|“線性”|“踏步”|“樣條”。
另外,該參數要結合keyTimes、keySplines使用,數值的是對應values的,
所以如果沒有設置values和keyTime或keySplines,是沒有效果的。
begin動畫開始的時機,取值:
time-value | offset-value | syncbase-value | event-value?| repeat-value |
accessKey-value?| media-marker-value | wallclock-sync-value | "indefinite"
1.?time-value:動畫開始時間,可傳多個值,分號分隔。
2.?syncbase-value:[元素的id].begin/end +/- 時間值(offset-value)
? ? 某個動畫效果開始或結束觸發此動畫的,可加上偏移量。
3.?event-value:事件觸發
4.?repeat-value:指某animation重復多少次開始。
? ? 語法為:[元素的id].repeat(整數) +/- 時間值
endend與begin除了名字和字面含義不一樣,其值的種類與表意都是一模一樣的。

PS:只列出常用參數,其他請查閱參考文獻。

例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"><rect x="50" y ="50" width="100" height="50" fill="red"><animate attributeType="XML"attributeName="x"from="50"to="400"dur="5s"fill="freeze"></animate></rect><rect x="50" y ="150" width="100" height="50" fill="green"><animate attributeType="XML"attributeName="x"from="50"by="400"dur="5s"fill="freeze"></animate></rect><rect x="50" y ="250" width="100" height="50" fill="blue"><animate attributeType="XML"attributeName="x"values="50;450;50"dur="10s"></animate></rect><rect x="50" y ="350" width="100" height="50" fill="orange"><animate attributeType="XML"attributeName="x"dur="10s"values="50;450;50"calcMode="spline"keySplines=".5 0 .5 1; 0 0 1 1"fill="freeze"></animate></rect><rect x="50" y ="450" width="100" height="50" fill="black"><animate attributeType="XML"attributeName="x"from="50"by="400"dur="5s"calcMode="spline"keySplines=".5 0 .5 1; 0 0 1 1"fill="freeze"></animate></rect>
</svg>

效果:

begin例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"><text x="50" y="30" id="t" stroke="red">click red go</text><rect x="50" y ="50" width="100" height="50" fill="red"><animate attributeType="XML"attributeName="x"begin="t.click"from="50"to="400"dur="5s"fill="freeze"></animate></rect><rect x="50" y ="150" width="100" height="50" fill="green"><!--表示的是3s之后動畫開始,10s時候動畫再開始一次(如果之前動畫沒走完,會立即停止從頭開始)--><animate attributeType="XML"attributeName="x"begin="3s;10s"from="50"to="400"dur="5s"fill="freeze"></animate></rect><rect x="50" y ="250" width="100" height="50" fill="blue"><animate id="goleft" attributeType="XML"attributeName="x"from="50"to="400"dur="5s"fill="freeze"></animate><!--注意begin的id是animate的id,不是元素的--><animate attributeType="XML"attributeName="y"begin="goleft.end"to="350"dur="2s"fill="freeze"></animate></rect><rect x="50" y ="350" width="100" height="50" fill="orange"><animate id="goleft" attributeType="XML"attributeName="x"from="50"to="400"dur="5s"fill="freeze"></animate><!--注意begin的id是animate的id,不是元素的--><animate attributeType="XML"attributeName="y"to="400"dur="5s"fill="freeze"></animate></rect><line stroke='black' x1="50" y1="350" x2="500" y2="350"/><line stroke='black' x1="50" y1="400" x2="500" y2="400"//>
</svg>

效果:

注意:

1. 多個animate是可以疊加的。

<animateTransform>標簽

該標簽用于變換動畫,animateTransform也有animate的參數,額外的是type。

參數描述
type變換的類型,取值:translate、scale、rotate、skewX、skewY

例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="-200 -200 800 800"><rect x="50" y ="50" width="50" height="50" fill="red"><animateTransform attributeName="transform"attributeType="XML"type="rotate"from="0 75 75"to="360 75 75"dur="2"repeatCount="indefinite"/></rect><!--x、y都放大了--><rect x="50" y ="150" width="50" height="50" fill="green"><animateTransform attributeName="transform"attributeType="XML"type="scale"from="1"to="2"dur="2"fill="freeze"/></rect>     <rect x="50" y ="250" width="50" height="50" fill="blue"><animateTransform attributeName="transform"attributeType="XML"type="translate"to="250 0"dur="2"fill="freeze"/></rect><rect x="50" y ="150" width="50" height="50" fill="black"><animateTransform attributeName="transform"attributeType="XML"type="rotate"from="0 75 125"to="360 75 125"dur="2"repeatCount="indefinite" additive="sum"/><animateTransform attributeName="transform"attributeType="XML"type="scale"from="1"to="2"dur="2"fill="freeze" additive="sum"/></rect>
</svg>

效果:

注意:

1.?animateTransform也是可以疊加的,不過要加上additive="sum",否則后面的無效了。

<animateMotion>標簽

這個標簽讓元素在路徑(Path)上滑動。

例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M100,400Q150,300 250,400 T400,400" stroke="red" fill="none"/><rect width="20" height="20" fill="red"><animateMotionpath="M100,400Q150,300 250,400 T400,400"rotate="auto"dur="3s"fill="freeze"></animateMotion></rect>
</svg>

效果:

注意:

1. 設置rotate="auto",可以讓元素根據路徑的切線方向做旋轉。

腳本動畫

SVG的requestAnimationFrame函數可以讓我們用js來做動畫,瀏覽器對requestAnimationFrame調用的頻率是每秒60次逐幀動畫。

例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"><rect id="rect" x="50" y="50" width="100" height="100" fill="green" />
</svg>
<script>var cur = 0;var rect=document.getElementById("rect");var frames = window.requestAnimationFrame(doAnim);function doAnim(){if(cur>=360){//取消幀動畫
            window.cancelAnimationFrame(frames);return;}cur++;rect.setAttribute("transform", "rotate(" + cur + ",100, 100)");frames = window.requestAnimationFrame(doAnim);}
</script>

PS:效果就是正方形旋轉360°后停止。

?

參考視頻

1.?SVG課程(慕課網)

?

參考文獻

1.?http://www.w3.org/TR/SVG/animate.html

2.?http://www.zhangxinxu.com/wordpress/?p=4333

?

本文為原創文章,轉載請保留原出處,方便溯源,如有錯誤地方,謝謝指正。

本文地址 :http://www.cnblogs.com/lovesong/p/6011328.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457707.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457707.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457707.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

bootstrap彈出的模態框水平垂直居中的實現

學習javascript從入門到放棄&#xff01;&#xff0c;這是第一篇隨筆&#xff0c;經驗不足&#xff0c;如有不當之處&#xff0c;還望指出。好了廢話不多說直接切入正題吧 1.bootstrap默認的model寫法&#xff1a; //觸發模態框的button <button data-toggle"modal&quo…

python開發實踐教程_Python開發實踐教程

前言第一章 通過求三角形面積步入Python程序世界 案例1-1 求三角形面積 導讀 知識梳理與擴展 小結 練習一第二章 常用運算、使用自定義函數 案例2-1 用函數的方法計算三角形面積 導讀 案例2-2 開發一個求三角形面積的工具包 知識梳理與擴展 小結 練習二第三章 分支、循環和列表…

正則表達式 PHP

正則表達式的常用函數&#xff1a; 先寫一個函數show&#xff0c;用來輸出數組&#xff0c;使代碼看起來方便&#xff0c;且把經常重復用到的量放在前面 function show($varnull) {if(empty($var))//檢測變量是否為空{echo null;}elseif (is_array($var)||is_object($var))//…

iis發布網站怎么支持.json文件

轉載于:https://www.cnblogs.com/daizhipeng/p/5622839.html

PHP Cookie和Session

cookie cookle常用來識別用戶&#xff0c;可以理解為瀏覽器留下的一種文件&#xff0c;cookie可以分為會話cookie和硬盤cookie兩種&#xff0c;會話cookie生命周期短&#xff0c;瀏覽器關閉即銷毀&#xff0c;硬盤cookie生命周期由開發者設定&#xff0c;可長可短 1.創建cook…

瀏覽器渲染機制

1. 明白瀏覽器渲染的目的 可訪問性&#xff08;Accessability&#xff09;、加載性能和重構靈活性一直是前端工程師們關心的主題。 其中加載性能與瀏覽器的渲染機制深深掛鉤&#xff0c;弄明白瀏覽器背后的渲染機制&#xff0c;才能在日常的前端的開發中明白如何進行性能優化。…

java中List Set Map使用

Testpublic void run(){ArrayList<String> list new ArrayList<String>();list.add( "美女");list.add( "帥哥");list.add( "芙蓉姐姐" );for (int i 0; i < list.size(); i) {String strlist.get(i);System. out.println(str);…

python免費開源工具推薦_年薪200萬的程序員,推薦這10大Python免費開源工具!

原標題&#xff1a;年薪200萬的程序員&#xff0c;推薦這10大Python免費開源工具&#xff01;毫無疑問&#xff0c;Python是最流行的語言之一&#xff0c;其成功的原因之一是它為科學計算提供了廣泛的報道。 在這里&#xff0c;我們仔細研究用于機器學習和數據科學的十大Python…

通過Yeoman快速搭建AngularJS webapp應用的實踐

這里主要記錄關于使用yeoman快速構建angluarJS的前端應用的實踐&#xff1a; 安裝node、yeoman等過程略去 1、首頁我們可以在公共的腳手架中找到自己想要的腳手架&#xff0c;官方腳手架庫的地址 Yeoman generators: http://yeoman.io/generators/ 這里我們選擇官方團隊出的ang…

[Objective-C]編程藝術 筆記整理

看了《禪與 Objective-C 編程藝術》&#xff0c;發現不少平時不注意的或注意但沒有系統總結的東西&#xff0c;特此記錄一下。 這次沒有整理完&#xff0c;后續更新會結合手里的一些其他資料整理。 新博客wossoneri.com傳送門 完整的介紹看這兩個鏈接Google開源項目風格指南禪與…

python大數據工程師 培訓_大數據工程師學習之路

大數據的作用&#xff1a;發現過去事件的特征預測未來最優化選擇職位劃分&#xff1a;數據產品經理數據分析師->商業敏感性&#xff0c;產品經理的助手數據研發工程師數據挖掘工程師/數據科學家需要準備的技術知識&#xff1a;Linux操作系統與網絡編程&#xff1a;進程/線程…

python序列

python序列 列表 python變量不存放值&#xff0c;之存放值的引用&#xff0c;所以列表中元素可以是不同類型 1.常用方法 1.1 增加元素 append,在末尾插入元素 extend&#xff0c;將一個列表整體插入到尾部 insert&#xff0c;將某個元素插入到特定位 /*&#xff0c;拼合…

前端資源(11)

移動端API 地址99移動端知識集合 https://github.com/jtyjty99999/mobileTech移動端前端開發知識庫 https://github.com/AlloyTeam/Mars移動前端的一些坑和解決方法&#xff08;外觀表現&#xff09; http://caibaojian.com/mobile-web-bug.html【原】移動web資源整理 http://w…

win10使用Composer-Setup安裝Composer以及使用Composer安裝Yii2最新版

1&#xff1a;下載 ca-bundle.crt和cacert.pem(見導航欄——文件) 將這兩個文件放在php目錄下 2:php.ini中添加上述兩個文件的路徑 curl.cainfoC:/xampp/php/ca-bundle.crtopenssl.cafileC:/xampp/php/ca-bundle.crt 寫到最后一行就可以 3&#xff1a;下載Composer-Setup.exe h…

python離群點檢測方法分幾類_數據分析 第五篇:離群點檢測

離群點(outlier)是指和其他觀測點偏離非常大的數據點&#xff0c;離群點是異常的數據點&#xff0c;但是不一定是錯誤的數據點。確定離群點對于數據分析會帶來不利的影響&#xff0c;比如&#xff0c;增大錯誤方差、影響預測和影響正態性。從散點圖上可以直觀地看到離群點&…

計算機網絡TCP/IP

TCP/IP原理 看《圖解TCP/IP》時做的筆記&#xff0c;記錄一些感覺重要的東西…還沒完&#xff0c;正在學&#xff0c;慢慢寫 1.計算機網絡的發展 2.OSI參考模型 3.傳輸方式分類 4.地址及網絡的構成 5.TCP/IP協議的出現 6.TCP/IP分層模型及通信示例 7.數據鏈路層1 8.數…

hive求差集和交集

2019獨角獸企業重金招聘Python工程師標準>>> 用的要求總數和統計數&#xff1a; 總數的概念是利用安卓ID&#xff0c;就以為這把兩個月前的安卓ID統統的統計一遍&#xff0c;如果沒有出現&#xff0c;恰好在今天出現了&#xff0c;那么當前的這個用戶就是新增的…

python靜態方法可以被繼承嗎_python 類的繼承 實例方法.靜態方法.類方法的代碼解析...

這篇文章主要介紹了python 類的繼承 實例方法.靜態方法.類方法的代碼解析,文中通過示例代碼介紹的非常詳細&#xff0c;對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下dt{}class Denglu:def register(self,name,psd):if name.isalnum() and psd.isalnum():i…

漸變色--瀏覽器兼容性

說明&#xff1a; 1.360兼容模式效果不好 2.可以直接用不同瀏覽器打開這個頁面查看效果 <!DOCTYPE html><html><head lang"en"> <meta charset"UTF-8"> <title></title> <style> body{ …

如何學習前端 轉載

作者&#xff1a;小不了鏈接&#xff1a;https://zhuanlan.zhihu.com/p/23265155來源&#xff1a;知乎著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。 鑒于時不時&#xff0c;有同學私信問我&#xff08;老姚&#xff0c;下同&#xff09;怎…