SVG动画由[use::SVG 可缩放矢量图形]元素内部的元素属性控制,一般通过一下几个元素控制:

  • : 用于控制动画延时
  • :对属性的连续改变进行控制
  • :颜色变化,但用就能控制
  • :控制如缩放、旋转等几何变化
  • :控制SVG内元素的移动路径

1. 示例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
	*{
		margin:0;
		padding:0;
	}
	</style>
</head>
<body>
	<svg id="box" width="800" height="400" xmlns="http://www.w3.org/2000/svg" version="1.1">
		<rect x="" y="" width="100" height="100" fill="rgb(255,0,0);" stroke="" stroke-width="">
			<set attributeName="x" attributeType="XML" to="100" begin="4s"/>
			<animate attributeName="x" attributeType="XML" begin="0s" dur="4s" from="0" to="300"/>
			<animate attributeName="y" attributeType="XML" begin="0s" dur="4s" from="0" to="0"/>
			<animateTransform attributeName="transform" begin="0s" dur="4s" type="scale" from="1" to="2" repeatCount="1" />
			<animateMotion path="M10,80 q100,120 120,20 q140,-50 160,0" begin="0s" dur="4s" repeatCount="1" />
		</rect>		
	</svg>	
</body>
</html>

这里推荐一个在sublime text3中使用svg提示插件:svg snippet。

2. 比较

SVG的一大优势是含有较为丰富的动画功能,原生绘制各种图形、滤镜和动画,并且能被js调用。html是对dom的渲染,那么svg就是对图形的渲染。 但是,另一方面元素较多且复杂的动画使用svg渲染会比较慢,而且SVG格式的动画绘制方式必须让内容嵌入到HTML中使用。CSS3的出现让svg的应用变得相对少了。