SVG中的动画技术(2)

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"&gt;   
 2&gt;  <desc>沿路径运动的例子</desc>   
 3&gt;  <defs>   
 4&gt;  <!-- 下面定义渐变颜色 -->   
 5&gt;  <lineargradient gradientunits="userSpace" id="grad1" x1="0" x2="0" y1="0" y2="100%">   
 6&gt;  <stop offset="0%" style="stop-color: #88f;"></stop>   
 7&gt;  <stop offset="100%" style="stop-color: #008;"></stop>   
 8&gt;  </lineargradient>   
 9&gt;  </defs>   
10&gt;  <rect height="298" width="498" x="1" y="1">  fill="none" stroke="blue" stroke-width="2" /&gt;   
11&gt;    
12&gt;  <!-- 下面的代码画出一个路径 -->   
13&gt;  <path d="M100,250 C 100,50 400,50 400,250">  fill="none" stroke="blue" stroke-width="8" /&gt;   
14&gt;    
15&gt;  <!-- 下面的这个圆将沿着定义的轨迹运动 -->   
16&gt;  <circle r="25" style="fill:url(#grad1)">   
17&gt;  <!-- 定义动画的运动轨迹 -->   
18&gt;  <animatemotion dur="6s" repeatcount="indefinite">  path="M100,250 C 100,50 400,50 400,250" rotate="auto" /&gt;   
19&gt;  </animatemotion></circle>   
20&gt;  <text style="font-size:12pt" x="145" y="285">http://lucky.myrice.com</text>   
21&gt;  </path></rect></svg>

上面的例子应用了animateMotion动画元素,其属性设定和前面类似。该例子的效果如图所示:

SVG DOM产生动画的方式是由脚本语言调用DOM对象的属性和方法实现的,在SVG中使用脚本语言和HTML中类似,首先是指定脚本的语言类型:有两种办法可以指定脚本的语言类型

> 1),在SVG元素里使用contentScriptType属性,可以指定整个文档里使用的缺省脚本语言类型。
> 语法如下:
>

1<svg contentscripttype="content-type">   
2&gt;  content-type指定一种媒体类型,默认是"text/ecmascript"   
3&gt;  2),在script元素里使用type属性来声明该段脚本所使用的语言类型。   
4&gt;  语法如下:   
5&gt;  <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>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus