前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SVG - 动画制作

SVG - 动画制作

作者头像
HTML5学堂
发布2018-03-12 10:07:04
3.1K0
发布2018-03-12 10:07:04
举报
文章被收录于专栏:HTML5学堂

SVG - 动画制作

HTML5学堂:SVG - 动画制作。上一篇文章讲解了SVG的基本属性,大家能够利用SVG的基本属性进行绘制图像,那么如何让绘制好的图像动起来呢?今天要给大家分享的是SVG的动画制作,具体我们来看看下面的内容吧。

接触过HTML5的人,都知道,Canvas实现一个动画,需要不断的清除画布再重新绘制,如果没有接触过Canvas也不要紧,SVG之后我们紧接着要为大家介绍的就是Canvas。SVG提供了比较方便的API接口,动画实现起来比较方便,具体看看下面的动画命令。

SVG 动画基本命令

<set> 表示瞬间的动画设置

<animate> 用于实现单属性的动画效果

<animateColor> 颜色发生动画效果(也能够使用animate进行操作,因此,可忽略)

<animateTransform> 变形类动画

<animateMotion> 沿着某个路径进行运动

SVG 动画参数介绍

1、attributeName的属性值是要变化的元素属性名称

2、attributeType = "CSS | XML | auto";如果你不提供一个attributeType属性,那么浏览器会尝试猜测是一个XML属性还是CSS属性

3、from, to

from:动画的起始值。

to:指定动画的结束值。

4、begin, end

begin:指动画开始的时间。begin的定义是分号分隔的一组值。

end:指定动画结束的时间。与begin的取值方法类似。

5、dur

指定动画的持续时间。可以取下面三者之一:大于0的数值、media和indefinite。该属性值缺省为indefinite,即无限长。

SVG示例1:<set> 动画设置

代码语言:javascript
复制
<html>
<head>
 <meta charset="UTF-8">
 <title>HTML5学堂</title>
 <link rel="stylesheet" href="reset.css">
 <style>
  .smile {
   border: 1px solid #999;
  }
 </style>
</head>
<body>
 <svg class="smile" version="1.1" width="800" height="400" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
  <rect x="100" y="10" width="220" height="60" fill="yellow">
   <set attributeName="x" attributeType="XML" to="300" begin="1s"/>
   <!-- 如果你不提供一个attributeType属性,那么浏览器会尝试猜测是一个XML属性还是CSS属性。 -->
  </rect>
  <text x="20" y="40" fill="red">set瞬间动画设置</text>
 </svg>
</body>
</html>

SVG示例2:<animate> 动画设置

代码语言:javascript
复制
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>HTML5学堂</title>
 <link rel="stylesheet" href="reset.css">
 <style>
  .smile {
   border: 1px solid #999;
  }
 </style>
</head>
<body>
 <svg class="smile" version="1.1" width="800" height="400" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
  <rect x="100" y="100" width="220" height="60" fill="yellow">
   <animate attributeName="x" attributeType="XML" from="100"  to="470" begin="0s" dur="5s" fill="remove" repeatCount="indefinite"/>
   <!-- 当动画完成,animate的属性被设置回其原始值(fill="remove")。如果想要的将动画属保持在to值的位置,则fill设置为"freeze"。动画如果无限重复则设置repeatCount的值。 -->
  </rect>
  <text x="20" y="140" fill="red">animate用于实现单属性的动画效果</text>
 </svg>
</body>
</html>

SVG示例3:<animateTransform> 动画设置

代码语言:javascript
复制
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>HTML5学堂</title>
 <link rel="stylesheet" href="reset.css">
 <style>
  .smile {
   border: 1px solid #999;
  }
 </style>
</head>
<body>
 <svg class="smile" version="1.1" width="800" height="400" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
  <rect x="100" y="200" width="220" height="60" fill="yellow">
    <animateTransform attributeName="transform" type="rotate" from="0 50 220" to="360 50 220" begin="0s" dur="10s"repeatCount="indefinite"/>
  </rect>
  <text x="20" y="240" fill="red">animateTransform变形类动画</text>
 </svg>
</body>
</html>

SVG示例4:<animateMotion> 动画设置

代码语言:javascript
复制
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>HTML5学堂</title>
 <link rel="stylesheet" href="reset.css">
 <style>
  .smile {
   border: 1px solid #999;
  }
 </style>
</head>
<body>
 <svg class="smile" version="1.1" width="800" height="400" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
   <path d="M200,300 q60,50 100,0 q60,-50 100,0" stroke="#000000" fill="none"/>
  <rect x="0" y="0" width="30" height="15" style="stroke: #ff0000; fill: none;">
      <animateMotion path="M200,300 q60,50 100,0 q60,-50 100,0" begin="0s" dur="10s" repeatCount="indefinite"/>
  </rect>
  <text x="20" y="300" fill="red">set瞬间动画设置</text>
 </svg>
</body>
</html>

SVG 动画效果综合示例

代码语言:javascript
复制
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>HTML5学堂</title>
 <link rel="stylesheet" href="reset.css">
 <script type="text/javascript" src="jquery-1.8.3.min.js"></script>
 <style>
  .smile {
   width: 800px;
   height: 400px;
   border: 1px solid #f00;
  }
 </style>
</head>
<body>
 <svg  class="smile" version="1.1" width="800" height="400" baseProfile="full" xmlns="http://www.w3.org/2000/svg">
  <path d="M10,50 q60,50 100,0 q60,-50 100,0" stroke="#000000" fill="none"/>
  <circle cx="0" cy="0" r="100" fill="yellow" stroke="black" stroke-width="1px">
   <animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" dur="4s" repeatCount="indefinite"/>
  </circle>
 
  <circle cx="-50" cy="-20" r="20" fill="black">
   <animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" dur="4s" repeatCount="indefinite" />
  </circle>
 
  <circle cx="50" cy="-20" r="20" fill="black">
   <animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" dur="4s" repeatCount="indefinite" />
  </circle>
 
  <clipPath id="faceClip">
   <rect x="-100" y="40" width="220" height="60" />
  </clipPath>
  <circle cx="0" cy="0" r="60" fill-opacity="0" stroke="black" stroke-width="5px" clip-path="url(#faceClip)">
   <animateMotion path="M10,50 q60,50 100,0 q60,-50 100,0" dur="4s" repeatCount="indefinite" />
  </circle>
 </svg>
 <!-- 注释 -->
 <!-- 清除路径 clipPath -->
 <!-- 简单的理解就是画一个路径把它剪切出来 -->
 <!-- 用于隐藏位于剪切路径以外的对象部分。定义绘制什么和什么不绘制的模具被称为剪切路径 -->
 <!-- 动画 -->
 <!-- path中可以使用M L 和 z。M表示将画笔移动到某个位置,即moveTo L表示的是lineTo z则表示的是关闭路径(closePath) -->
 <!-- q表示的贝塞尔,也就是拐点的位置(Q表示绝对,q表示相对) -->
</body>
</html>

SVG动画效果图

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2015-08-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 懂点君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SVG示例1:<set> 动画设置
  • SVG示例2:<animate> 动画设置
  • SVG示例3:<animateTransform> 动画设置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档