SVG支持动画。可以通过以下几种方法获得动画效果:

  • 使用SVG动画元素。SVG可以描述随时间变化的图形对象,使用不同的动画元素可以定义运动路径,淡入淡出效果和对象的膨胀、收缩、旋转和变换颜色。
  • 使用SVG DOM。SVG DOM兼容DOM1和DOM2,而SVG又定义了一套附加的DOM接口,支持脚本动画。通过这个方法可以获得各种动画效果。脚本语言中的定时器对象可以用来启动和控制动画。
  • SVG被设计为支持未来版本的SMIL(Sychronized Multimedia Integration Language)。SMIL将被模块化而与SVG及其它基于XML的语言连接,产生动画效果。

一、SVG中的动画元素

SVG中的动画元素有五个,它们都可以随着时间的变化而改变SVG元素的属性或样式值,如下所示:

  • <animate>:改变数值的属性或样式的值;
  • <set>:改变非数据值的属性或样式的值,如visibility属性等;
  • <animateMotion> 
    沿着某路径移动SVG元素获得动画效果, 该元素提供一种通过指定路径移动元素的简单方法。路径数据与路径元素的 d 属性相同,但用路径元素指定。也可以用 xlink:href 将它链接到 animateMotion 元素。起点和终点由 from 和 to 属性确定,并且可以通过将 rotate 值设为 auto 来设置对象垂直对齐于路径。(也可以将 rotate 属性设为 auto-reverse 以将这个方位改变 180 度。或者可以给定一个特定角度)。如动画和交互性所示:
    
    <animateMotion path="M0,300 S150,100 200,200 S400,400 500,0" dur="8s" repeatCount="indefinite" rotate="auto" />  
  • <animateColor>:改变某些元素与颜色有关的属性或样式的值; 
<circle cx="250" cy="100" r="50" fill="red">
     <animateColor attributeType="CSS" attributeName="fill" from="rgb(255,0,0)" to="rgb(0,0,255)" dur="8s"  repeatCount="indefinite"/>
 </circle> 
  • <animateTransform>:改变SVG元素进行坐标变换时候的动画效果;
    <rect x="333" y="49" width="50" height="50" fill="none" stroke="purple">
          <animateTransform attributeName="transform" attributeType="XML"
               type="scale" from="1" to="3" additive="sum" begin="3s" dur="6s" fill="freeze" />
          <animateTransform attributeName="transform" attributeType="XML"
               type="translate" from="0,0" to="-222,-45" additive="sum" begin="3s" dur="6s" fill="freeze" />
     </rect>
  • set:剩下的这个元素可以很容易地设置一个元素在指定时间段内的特殊属性
    <circle cx="250" cy="100" r="50" fill="red">
        <set attributeName="r" to="100" begin="1s" dur="5s" fill="remove" />
    </circle>

     attributeName="<attributeName>":批明所作用的SVG元素中哪个属性或样式需要产生动画效果。 

attrbuteType="<XML|CSS|auto>":指明产生动画效果的属性或样式值是哪个命名空间定义的。“XML”表示“attributeName”的值是默认XML命名空间里定义的XML属性名;“CSS”表示“attributeName”的值是默认CSS属性的名称;“auto”是默认值,解析器解析时先在CSS属性列表中查找是否有匹配“attributeName”的属性名,然后再在XML的命名空间里找 

1.与时间控制相关的常用属性

begin="<clock-time-value>|wallclock-sync-value|indefinite|eventName":定义动画的开始时刻。

  • a.时间偏移值,如3S表示3秒后开始播放动画(hh:mm:ss.xxxx)
  • b.现实世界中的时间,定义后要保证SVG文档要在这个时间之前打开
  • c.Endefinite:表示这个动画不会自动开始,需要使用动态脚本调用"beginElement()"方法或指向动画元素
  • d.eventName:表示在某个事件触发时开始播放动画。如begin="mousedown"
    • 1.dur="<clock-time-value>|indefinite":定义动画的持续时间
    • 2.end="<clock-time-value>|indefinite":定义动画的结束时间
    • 3.restart="always|whenNotactive|never":是否重播
    • 4.repeatCount:重复播放次数 indefinite表示无限重播
    • 5.repeatDur:定义动画播放总时间
    • 6.fil="freeze|remove":定义动画播放完毕后是停留在播放的终点还是回到起始位置

2.与过程控制相关的常用属性

from="<value>":定义该动画元素所作用的属性值在开始变化时的值

to="<value>":定义该动画元素所作用的属性值在结束变化时的值

by="<value>":定义该动画元素所作用的属性值每次变化的步长值

calcMode="discrete|linear|paced|spline":定义动画关键点直接过渡的插值计算方式

  • discrete表示不采用插值算法,动画是从一个关键点跳跃到另外一个关键点
  • linear表示采用线性插值算法,这是<animate>元素和<set>元素采用的默认插值算法
  • paced表示采用在埋单上均匀变化的方法来插值
  • spline表示采用三次贝塞尔曲线的方式来插值,需要"KeySplines"属性的配合

 例子1:

<rect x="50" y="50" width="100" height="50">
    <animate attributeType="XML" attributeName="x" from="50" to="300" dur="3s" begin="3s" restart="always" repeatCount="3" >
</animate>
</rect>

例子2:

<text x="0" y="0" font-size="37" visibility='hidden" stroke="black" stroke-width="2" >
    <animateMotion path="M0,0 L50,50 L100,150" begin="1s" dur="5s" fill="freeze" rotate="auto"/>
</text>

例子3:

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <g transform="translate(100,100)"> 
<text id="TextElement" x="0" y="0" style="font-family:Verdana;font-size:24; visibility:hidden"> It's SVG!
        <set attributeName="visibility" attributeType="CSS" to="visible" begin="1s" dur="5s" fill="freeze" />
        <animateMotion path="M 50 50 L 300 300" begin="1s" dur="5s" fill="freeze" />
        <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" begin="1s" dur="15s" fill="freeze" repeatCount='indefinite'/> 
        <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="3" additive="sum" begin="1s" dur="5s" fill="freeze" /> 
    </text> 
</g> 
</svg>

 二、事件的脚本编制(DOM动画)

象 HTML 页面一样,可以设置 SVG 图像以捕获某些事件(如点击鼠标和滚动),并用它们启动脚本。在构建简单SVG 图像时,可以通过属性捕获这些事件。最常用的是onclick、onactivate、onmousedown、onmouseup、onmouseover、onmousemove、onmouseout、onload、onresize、onunload 和 onrepeat。 

当这些事件之一被触发,就可以将事件对象本身提供给脚本,脚本反过来再用它确定哪个对象触发了该事件(也就是点击了什么对象)。然后脚本可以操纵那个对象的特性,如它的属性。 

这一示例回到了图案示例,但在此例中,当用户点击椭圆时,其填充由白色变为使用图案。

<?xml version="1.0"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
        "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="500" height="300" xmlns="http://www.w3.org/2000/svg">
    <desc>Scripting the onclick event</desc>
    <script type="text/ecmascript">
        <![CDATA[
      function hideReveal(evt) {
        var imageTarget = evt.target;
        var theFill = imageTarget.getAttribute("fill");
        if (theFill == 'white')
          imageTarget.setAttribute("fill", "url(#notes)");
        else
          imageTarget.setAttribute("fill", "white");
      }
    ]]>

    </script>
    <defs>
        <pattern id="notes" x="0" y="0" width="50" height="75"
                 patternTransform="rotate(15)"
                 patternUnits="userSpaceOnUse">
            <ellipse cx="10" cy="30" rx="10" ry="5"/>
            <line x1="20" y1="30" x2="20" y2="0"
                  stroke-width="3" stroke="black"/>
            <line x1="20" y1="0" x2="30" y2="5"
                  stroke-width="3" stroke="black"/>
        </pattern>
    </defs>

    <!-- Outline the drawing area with a blue line -->

    <rect x="1" y="1" width="350" height="200" fill="none" stroke="blue"/>
    <ellipse onclick="hideReveal(evt)"  cx="175" cy="100" rx="125" ry="60"
            fill="url(#notes)" stroke="black" stroke-width="5"/>

</svg>
View Code

相关文章: