SVG SMIL 动画(基本动画 、变换动画)

简介: SVG SMIL 动画(基本动画 、变换动画)

一、简介

  • 动画标签:<animate>、<animateTransform>、<animateMotion> ...
  • 动画元素、属性定位以及动画参数设置:
  • attributeNameattributeType
  • fromtodurrepeatCountfill
  • calcMode

二、使用方式

  • 定位动画目标
  • Internal Resource Identifier 定位,指定需要生效动画的标签
<animate xlink:href="url(#rect1)"></animate>
  • 包含在目标元素内,对包含的父元素生效动画
<rect x="0" ...>
    <animate></animate>
</rect>
  • 设置要进行动画的属性以及变化范围、时间长度
<animate xlink:href="url(#rect1)"
    attributeType="XML"
    attributeName="x"
    from="0" to="100"
    dur="2s"
    repeatCount="1">
</animate>

三、基本动画

  • animate 支持叠加动画,也就是同一个标签支持添加多个 animate 动画标签。
  • 效果

  • 代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html,body {
      margin: 0;
      padding: 0;
      background-color: #001122;
      /* 全屏 */
      width: 100%;
      height: 100%;
      /* 清空字号,免得影响 svg */
      line-height: 0;
      font-size: 0;
    }
  </style>
</head>
<body>
  <!-- animate
    attributeType: 标签类型
    attributeName: 需要支持动画的属性
    begin: 动画开始时机
    from: 开始值
    to: 结束值
    dur: 动画时间
    fill: freeze(停留在最后)、none(默认,回到起点)
    repeatCount: 动画次数,indefinite(无限次),默认 1
    -->
  <!-- svg -->
  <svg width="100%" height="100%">
    <!-- 1 平移动画 -->
    <rect x="50" y="10" width="40" height="40" fill="red">
      <!-- 动画 -->
      <animate attributeType="XML"
        attributeName="x"
        from="50" to="200"
        dur="1s"
        repeatCount="indefinite"/>
      <!-- 颜色变化 -->
      <animate attributeType="XML"
        attributeName="fill"
        from="red" to="yellow"
        dur="1s"
        repeatCount="indefinite"/>
    </rect>
    <!-- 2 平移动画 - 往返 -->
    <rect x="50" y="80" width="40" height="40" fill="red">
      <!-- 动画 -->
      <animate id="goright1" attributeType="XML"
        attributeName="x"
        begin="0; goleft1.end"
        from="50" to="200"
        dur="1s"
        fill="freeze"/>
      <animate id="goleft1" attributeType="XML"
        attributeName="x"
        begin="goright1.end"
        from="200" to="50"
        dur="1s"
        fill="freeze"/>
      <!-- 颜色变化 -->
      <animate attributeType="XML"
        attributeName="fill"
        from="red" to="yellow"
        dur="1s"
        repeatCount="indefinite"/>
    </rect>
    <!-- 3 平移动画 - 往返 - 暂停 -->
    <rect x="200" y="150" width="40" height="40" fill="red">
      <!-- 动画 -->
      <animate id="goleft2" attributeType="XML"
        attributeName="x"
        begin="0; goright2.end + 1s"
        from="200" to="50"
        dur="1s"
        fill="freeze"/>
      <animate id="goright2" attributeType="XML"
        attributeName="x"
        begin="goleft2.end + 1s"
        from="50" to="200"
        dur="1s"
        fill="freeze"/>
      <!-- 颜色变化 -->
      <animate attributeType="XML"
        attributeName="fill"
        from="red" to="yellow"
        dur="1s"
        repeatCount="indefinite"/>
    </rect>
  </svg>
</body>
</html>

四、变换动画

  • animateTransform 支持叠加动画,也就是同一个标签不能添加多个 animateTransform 动画标签。
  • 效果

  • 代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html,body {
      margin: 0;
      padding: 0;
      background-color: #001122;
      /* 全屏 */
      width: 100%;
      height: 100%;
      /* 清空字号,免得影响 svg */
      line-height: 0;
      font-size: 0;
    }
  </style>
</head>
<body>
  <!-- animateTransform
    attributeType: 标签类型
    attributeName: 需要支持动画的属性
    type: 子属性
    begin: 动画开始时机
    from: 开始值
    to: 结束值
    dur: 动画时间
    fill: freeze(停留在最后)、none(默认,回到起点)
    repeatCount: 动画次数,indefinite(无限次),默认 1
    -->
  <!-- svg -->
  <svg width="100%" height="100%" viewBox="-400 -400 800 800">
    <!-- 1 原地旋转 -->
    <rect x="0" y="0" width="40" height="40" fill="red">
      <!-- 动画 -->
      <animateTransform attributeType="XML"
        attributeName="transform"
        type="rotate"
        from="0"
        to="360"
        dur="3s"
        repeatCount="indefinite">
    </rect>
    <!-- 1 原地旋转 - 缩放 - 不支持多个动画叠加 -->
    <rect x="0" y="80" width="40" height="40" fill="red">
      <!-- 动画 -->
      <!-- <animateTransform attributeType="XML"
        attributeName="transform"
        type="rotate"
        from="0"
        to="360"
        dur="3s"
        repeatCount="indefinite"> -->
      <animateTransform attributeType="XML"
        attributeName="transform"
        type="scale"
        from="1"
        to="2"
        dur="3s"
        repeatCount="indefinite">
    </rect>
  </svg>
</body>
</html>

五、轨迹 Path 动画(animateMotion)

六、Scripting Animation(脚本动画)

七、参考文档


相关文章
|
存储 缓存 关系型数据库
Idea 进行远程服务器debug操作
Idea 进行远程服务器debug操作
1034 0
UE4动画蓝图节点Layered blend per bone详解
UE4动画蓝图节点Layered blend per bone详解
494 1
Window端口占用处理
Window端口占用处理
79 2
|
SQL 关系型数据库 MySQL
【MySQL从入门到精通】常用SQL语句分享
【MySQL从入门到精通】常用SQL语句分享
180 2
|
数据库 数据安全/隐私保护 数据库管理
基于SpringBoot+Vue医院管理系统(源码+部署说明+演示视频+源码介绍+lw)(2)
基于SpringBoot+Vue医院管理系统(源码+部署说明+演示视频+源码介绍+lw)
747 2
|
前端开发 Java 开发工具
如何在Spring Boot框架下实现高效的Excel服务端导入导出?
ArtifactId:是项目的唯一标识符,在实际开发中一般对应项目的名称,就是项目根目录的名称。 Group Id,Artfact Id是保证项目唯一性的标识,一般来说如果项目打包上传至maven这样的包管理仓库中。在搜索你的项目时,Group Id,Artfact Id是必要的条件。 Version:版本号,默认0.0.1-SNAPSHOT。SNAPSHOT代表不稳定的版本,与之相对的有RELEASE。 Project type:工程的类型,maven工程还是gradle工程。 Language:语言(Java,Kotlin,Groovy)。
187 0
|
开发者
全网最全最简单使用easypoi导入导出Excel的操作手册(二)
今天做Excel导出时,发现了一款非常好用的POI框架EasyPoi,其 使用起来简洁明了。现在我们就来介绍下EasyPoi,首先感谢EasyPoi 的开发者 Lemur开源
10748 1
全网最全最简单使用easypoi导入导出Excel的操作手册(二)
|
开发工具 git Windows
解决 Failed to install golang.org/x/crypto/bcrypt 问题
解决 Failed to install golang.org/x/crypto/bcrypt 问题
172 0