SVG动画

摘要:
动画原理SVG动画是元素属性值相对于时间的变化。SVG动画语法SVG动画基于SMIL语言,全称为同步多媒体集成语言。SVG动画使用SVG元素。使用动画有两种方法:1.由xlink引用:href2.包含在目标元素中此标签用于基本动画。值动画的多个关键值用分号分隔。

动画原理

SVG动画,就是元素的属性值关于时间的变化。 如下图来说,元素的某个属性值的起始值(from)到结束值(to)在一个时间段(duration)根据时间函数(timing-function)计算出每一帧(frame)的插值(interpolation)作为变换的行为。

SVG动画第1张

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>

效果:

SVG动画第2张

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>

效果:

SVG动画第3张

注意:

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>

效果:

SVG动画第4张

注意:

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">
          <animateMotion
               path="M100,400Q150,300 250,400 T400,400"
               rotate="auto"
               dur="3s"
               fill="freeze">
          </animateMotion>
     </rect>
</svg>

效果:

SVG动画第5张

注意:

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

免责声明:文章转载自《SVG动画》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS开发基础知识--碎片38IT面试技巧(2)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用阿里云的maven私服的setting.xml, 提高maven项目jar下载速度

下载:  http://files.cnblogs.com/files/007sx/settings.zip 然后替换自己原本maven的配置文件。 如下载失败,可内容替换: <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/S...

XML与java的应用

对于XML进行解析,我们需要调用第三方提供的接口,对XML文档进行解析; 对于XML处理方面,W3C有两种解决方法: (1)DOM(Document Object Model): (2)SAX(Simple API for XML) DOM介绍: 特点:先将xml文档解析成dom树,并放入内存,能够随机访问任意节点。 缺点:因为需要放入内存操作,所以对文...

iOS AppIcon透明圆角导致的动画问题

今天设计师给了一个如下AppIcon,四边是透明的圆角区域。 这个图标在iOS14上正常显示没什么问题,但是当使用上滑手势让程序退到后台时,在程序缩小到图标的过程中,动画的4个脚都显示出了不正确的黑色块,十分难看。如下图: 后来发现,如果吧透明圆角去掉,直接使用方形,如下图,就可以解决问题。ios14系统会自动把图标切成带圆角的样式,显示在桌面上。...

CButtonEx的实现

要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制。这可以通过定义一个以CButton为基类的新按钮类来实现。以下为具体的实现方法: 方法一: 加入一个新类,类名:CButtonEx,基类:CButton。 在头文件 CButtonEx.h 中加入以下变量和函数定义: private: intm_Style; //按钮形状(...

Eclipse设置:背景与字体大小和xml文件中字体大小调整

源文:http://blog.csdn.net/macong01/article/details/7686238Eclipse中代码编辑背景颜色修改: 代码编辑界面默认颜色为白色。对于长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以改变workspace的背景色,可以使眼睛舒服一些。设置方法如下: 1、打开window / Preference,弹出P...

帧动画和骨骼json、极速、二进制对比

对比总结: 1. 帧动画的效率最高,但是图片超过一定帧数,资源图片非常大。比较适合帧数少,大量动画存在,要求效率高的场合。 骨骼json效率较低,已经不推荐使用。 骨骼极速,不支持网格等。 骨骼二进制,最新的格式。资源小,效率高。导出时将骨骼跑一遍,将计算数据序列化成二进制,使用时反序列化,节约了大量cpu运算,所以效率较json格式高。  (我猜是这样...