SVG中的动画技术(2)
下面再看一个沿指定路径运动的例子:
>
>
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
>
1<svg height="300" viewbox="0 0 500 300" width="500"> xmlns="http://www.w3.org/2000/svg">
2> <desc>沿路径运动的例子</desc>
3> <defs>
4> <!-- 下面定义渐变颜色 -->
5> <lineargradient gradientunits="userSpace" id="grad1" x1="0" x2="0" y1="0" y2="100%">
6> <stop offset="0%" style="stop-color: #88f;"></stop>
7> <stop offset="100%" style="stop-color: #008;"></stop>
8> </lineargradient>
9> </defs>
10> <rect height="298" width="498" x="1" y="1"> fill="none" stroke="blue" stroke-width="2" />
11>
12> <!-- 下面的代码画出一个路径 -->
13> <path d="M100,250 C 100,50 400,50 400,250"> fill="none" stroke="blue" stroke-width="8" />
14>
15> <!-- 下面的这个圆将沿着定义的轨迹运动 -->
16> <circle r="25" style="fill:url(#grad1)">
17> <!-- 定义动画的运动轨迹 -->
18> <animatemotion dur="6s" repeatcount="indefinite"> path="M100,250 C 100,50 400,50 400,250" rotate="auto" />
19> </animatemotion></circle>
20> <text style="font-size:12pt" x="145" y="285">http://lucky.myrice.com</text>
21> </path></rect></svg>
上面的例子应用了animateMotion动画元素,其属性设定和前面类似。该例子的效果如图所示:
SVG DOM产生动画的方式是由脚本语言调用DOM对象的属性和方法实现的,在SVG中使用脚本语言和HTML中类似,首先是指定脚本的语言类型:有两种办法可以指定脚本的语言类型
> 1),在SVG元素里使用contentScriptType属性,可以指定整个文档里使用的缺省脚本语言类型。
> 语法如下:
>
1<svg contentscripttype="content-type">
2> content-type指定一种媒体类型,默认是"text/ecmascript"
3> 2),在script元素里使用type属性来声明该段脚本所使用的语言类型。
4> 语法如下:
5> <script type="content-type">
6> content-type就是所使用的媒体类型。
7
8SVG里的script元素和HTML里的完全一致。任何script元素里定义的函数都可以应用到整个文档里,也可以通过xlink:href属性,指定一个脚本文件的URL地址,比如:<script type="text/JavaScript" xlink:href="test.js"></script></svg>