你可以学会的svg动画(SMIL)

简介: 你可以学会的svg动画(SMIL)

SMIL介绍


SMIL 全称 Synchronized Multimedia Integration Language,它允许我们通过 HTML 标签实现动画效果,它可以用于:


  • 实现过渡动画


  • 实现补间动画


  • 动画颜色变换


  • 路径运动动画(CSS3无法实现) SMIL 包含以下标签:


<set>
<animate>
<animateColor>// 这个元素,现在已经废弃。
<animateTransform>
<animateMotion>


为什么要用 SMIL?


实现动画的全新方式:


  • 无需 CSS


  • 无需 JS


  • 几个标签轻松实现复杂动画,它不香吗?


SMIL元素一些共同属性的介绍


  • attributeName:表示需要实现动画的属性。


  • attributeType:  根据attributeName指定的值来判断是svg元素属性的变化,还是css属性的变化。


  • CSS, 表示css属性值的变化。


  • XML,表示svg属性值的变化。


  • begin: 表示动画延迟多少秒。


  • from: 表示动画开始的值。


  • to:表示动画结束的值。


  • fill: 表示动画结束时所处的状态。


  • freeze: 表示结束后,让状态处于结束时的动画状态。


  • remove: 表示结束后,让状态处于开始时的动画状态。


  • values: 表示实现多个状态动画。不能使用from, to属性。每个状态值通过;分隔。


  • dur: 表示动画持续多少秒。


  • type: 在animateTransform元素中变换的类型。


  • repeatCount: 表示重复多少次动画。

  • path: 表示animateMotion元素动画时运行的轨迹。


  • rotate: 表示animateMotion元素动画时到达有棱角的位置时,怎样运行。一般设置为0。


  • id: 指定每个动画元素的唯一标识,可以结合begin来实现复杂动画。


set 标签


实现属性的延迟设置


<svg width="400" height="400">
  <rect x="0" y="0" width="100" height="100">
    <set attributeName="x" attributeType="XML" to="10" begin="1s" />
    <set attributeName="x" attributeType="XML" to="20" begin="2s" />
    <set attributeName="x" attributeType="XML" to="30" begin="3s" />
    <set attributeName="x" attributeType="XML" to="40" begin="4s" />
    <set attributeName="x" attributeType="XML" to="50" begin="5s" />
  </rect>
</svg>


animate 标签


动画元素放在形状元素的内部,用来定义一个元素的某个属性如何踩着时点改变。在指定持续时间里,属性从开始值变成结束值。


<svg width="200" height="200" viewPort="0 0 200 200">
    <rect x="10" y="10" width="100" height="100">
      <animate attributeType="CSS" attributeName="width" from="100" to="150" dur="3s" fill="freeze" />
    </rect>
  </svg>
  <svg width="200" height="200" viewPort="0 0 200 200">
    <rect x="10" y="10" width="100" height="100">
      <animate attributeType="XML" attributeName="width" from="100" to="150" dur="3s" fill="freeze" />
    </rect>
  </svg>


以上两种实现相同效果。一个是通过CSS,一个是通过svg。


形状补间动画。不规则图形的变化。


<svg width="400" height="400">
  <polygon points="30 30 70 30 90 70 10 70" fill="#fcc" stroke="black">
    <animate attributeName="points" attributeType="XML" to="50 30 70 50 50 90 30 50" dur="5s" fill="freeze" repeatCount="1" />
  </polygon>
</svg>


animateTransform 标签


animateTransform元素变动了目标元素上的一个变形属性,从而允许动画控制转换、缩放、旋转或斜切。注意:如果遇到transform属性值的变化,需要用到该元素。而不要去用animate元素做变换。


<svg width="200" height="200" viewBox="0 0 200 200">
    <rect x="0" y="0" width="60" height="60" fill="red">
      <animateTransform attributeName="transform" begin="0s" dur="3s" type="scale" from="1" to="2"
        repeatCount="indefinite" />
    </rect>
  </svg>


animateMotion 标签


这是一个非常nb的标签。可以让元素通过指定的轨迹运动。


实现一个轨迹动画


<svg width="300px" height="100px">
    <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" fill="none" />
    <rect x="0" y="0" width="20" height="20" fill="blue" stroke="black" stroke-width="1">
      <animateMotion path="M 250,80 H 50 Q 30,80 30,50 Q 30,20 50,20 H 250 Q 280,20,280,50 Q 280,80,250,80Z" dur="3s"
        repeatCount="indefinite" rotate="auto">
    </rect>
  </svg>


网络异常,图片无法展示
|


实现一个复杂混合动画


<svg viewBox="0 0 200 200" width="200" height="200">
    <rect x="0" y="0" rx="0" ry="0" width="10" height="10" fill="red">
      <animateMotion
        id="forward-rect"
        path="M 10 10 L 110 10 L 110 110 L 10 110"
        dur="2s"
        rotate="0"
        fill="freeze"
        begin="0; backward-rect.end + 0.5s"
      />
      <animateMotion
        id="backward-rect"
        path="M 10 110 L 110 110 L 110 10 L 10 10"
        dur="2s"
        rotate="0"
        fill="freeze"
        begin="forward-rect.end + 0.5s"
      />
      <animate
        id="red-to-blue"
        attributeType="XML"
        attributeName="fill"
        begin="0; blue-to-red.end + 1s"
        from="red"
        to="blue"
        dur="2s"
        fill="freeze"
      />
      <animate
        id="blue-to-red"
        attributeType="XML"
        attributeName="fill"
        begin="red-to-blue.end + 1s"
        from="blue"
        to="red"
        dur="2s"
        fill="freeze"
      />
    </rect>
    <path d="M 10 10 L 110 10 L 110 110 L 10 110" fill="none" stroke-width="1" stroke="blue"/>
</svg>


实现一个好看的loading动画


<svg viewBox="0 0 50 50" width="200" height="200">
    <circle cx="25" cy="25" r="22" fill="none" stroke-width="3" stroke-dasharray="34" stroke="#3be6cb"
      stroke-linecap="round">
      <animateTransform attributeName="transform" type="rotate" values="0 25 25; 360 25 25" dur="1.5s"
        repeatCount="indefinite" />
      <animate attributeName="stroke" values="#3be6cb;#02bcfe;#3be6cb" dur="3s" repeatCount="indefinite" />
    </circle>
    <circle cx="25" cy="25" r="12" fill="none" stroke-width="3" stroke-dasharray="19" stroke="#02bcfe"
      stroke-linecap="round">
      <animateTransform attributeName="transform" type="rotate" values="360 25 25;0 25 25" dur="1.5s"
        repeatCount="indefinite" />
      <animate attributeName="stroke" values="#02bcfe;#3be6cb;#02bcfe" dur="3s" repeatCount="indefinite" />
    </circle>
  </svg>


网络异常,图片无法展示
|



相关文章
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
2609 0
Threejs实现相机视角切换,平滑过渡,点击模型切换到查看模型视角
|
5月前
|
网络协议 Java
SpringBoot快速搭建TCP服务端和客户端
由于工作需要,研究了SpringBoot搭建TCP通信的过程,对于工程需要的小伙伴,只是想快速搭建一个可用的服务.其他的教程看了许多,感觉讲得太复杂,很容易弄乱,这里我只讲效率,展示快速搭建过程。
469 58
|
7月前
|
SQL 运维 监控
高效定位 Go 应用问题:Go 可观测性功能深度解析
为进一步赋能用户在复杂场景下快速定位与解决问题,我们结合近期发布的一系列全新功能,精心梳理了一套从接入到问题发现、再到问题排查与精准定位的最佳实践指南。
|
前端开发 JavaScript 开发者
React的useId,现在Vue3.5终于也有了!
【9月更文挑战第22天】React 的 `useId` 功能已在 Vue 3.5 中引入,用于生成唯一 ID,提升开发效率,确保组件 ID 的一致性和增强应用的可访问性。开发者能更简便地管理唯一标识符,减少繁琐工作,同时保证在服务器和客户端渲染下的稳定性。这一改进使得 Vue 应用更加高效、易用和可靠。
147 3
|
12月前
|
存储 消息中间件 JavaScript
Vue3 多种组件通讯方法
【10月更文挑战第6天】
349 122
|
12月前
|
传感器 监控 搜索推荐
智能纺织品:健康监测与生活方式的结合
【10月更文挑战第22天】智能纺织品融合了传感器、导电纤维和微电子元件等先进技术,不仅改变了穿着体验,还为健康监测和生活方式的改善带来了新机遇。它们能实时监测心率、血压等生理数据,通过无线通信技术传输至手机或云端,实现远程监控与数据分析。未来,智能纺织品将更加智能化、个性化和环保,成为日常生活中不可或缺的一部分。
|
开发框架 JavaScript 前端开发
提升生产力:8个.NET开源且功能强大的快速开发框架
提升生产力:8个.NET开源且功能强大的快速开发框架
300 3
|
负载均衡 数据可视化 NoSQL
强烈推荐,好用的时序图开源插件PlantUML!
PlantUML这个开源时序图插件,它通过简单的语法和自动化的图形线条关联解决了传统画图软件中对齐困难、逻辑判断不易表示等问题,并提供了美观的图形和易于修改的特点,特别适合新入职场的开发者快速上手绘制高质量的时序图。
强烈推荐,好用的时序图开源插件PlantUML!
|
JavaScript
Vue3文字滚动(TextScroll)
这是一个可定制的文字滚动组件,支持水平和垂直滚动。主要属性包括滚动文字数组 `scrollText`、是否启用单条文字滚动 `single`、滚动区域宽高 `width` 和 `height`、滚动区域和文字样式 `boardStyle` 和 `textStyle`、滚动条数 `amount`、间距 `gap`、动画间隔 `interval` 和 `step`、以及垂直滚动时间间隔 `verticalInterval`。组件内置多种样式调整功能,并提供在线预览示例。
468 0
Vue3文字滚动(TextScroll)
|
前端开发 JavaScript