What?SVG 还能做动画,这么强大还不学!(附源码和Demo)

大家好,我是 前端大卫

当我第一次看到这动画效果时,还以为是通过 GIF、CSS3 或者 Canvas 制作的。结果出乎意料,居然是用 SVG 实现的!

也许你对 SVG 动画还不太熟悉,那就让我们一起开启这场奇妙的旅程吧!

什么是 SVG 动画?

SVG(Scalable Vector Graphics)是一种用于描述二维矢量图形的 XML 格式。除了静态图形,SVG 还支持动画效果,这为前端开发提供了更加灵活和强大的工具。

简单的 SVG 示例

我们从一个简单的例子开始:

html 复制代码
<style>
  svg {
    border: 1px solid red;
  }
  svg line {
    stroke: #000; /* 线条的颜色 */
  }
</style>
<svg width="200" height="200">
  <line x1="0" x2="100%" y1="50%" y2="50%"></line>
</svg>

代码解析

<svg> 标签

  • 作用 :定义 SVG 容器。
  • 属性
    • widthheight:设置容器宽高(单位:像素),这里为 200px

<line> 标签

  • 作用:绘制直线。
  • 关键属性
    • x1x2:起点和终点的水平坐标。
    • y1y2:起点和终点的垂直坐标。

为直线添加动画

首先为直线增加一些样式:

css 复制代码
svg line {
  stroke: #000;
+ stroke-width: 5;
+ stroke-dasharray: 10;
+ stroke-dashoffset: 0;
}

新增属性解析

  • stroke-width:设置线条的宽度(例如 5px)。
  • stroke-dasharray:定义虚线模式,例如 10 表示线段和空隙均为 10px
  • stroke-dashoffset:控制虚线起始偏移量。

然后利用 stroke-dashoffset,为这条虚线添加动画效果:

css 复制代码
svg line {
  stroke: black;
  stroke-width: 5;
  stroke-dasharray: 10;
  stroke-dashoffset: 0;
+ animation: dash 2s linear infinite;
}

+@keyframes dash {
to {
    stroke-dashoffset: -100; /* 虚线反方向移动 */
  }
}

属性说明

  • 动画通过改变 stroke-dashoffset,让虚线看起来在移动。
  • 2s linear infinite 表示动画每 2 秒 循环一次,且速度匀速。

绘制折线动画

使用 <path> 标签,我们可以绘制更复杂的路径:

html 复制代码
<style>
  svg {
    border: 1px solid red;
  }
</style>
<svg width="200" height="200">
  <path d="M125.69 0.44 L182.46 16.41 L0.23 113" />
</svg>

看上去怎么有点怪,因为 path 填充了黑色的背景颜色。这时只需加入 CSS 样式 fill: none:

css 复制代码
svg path {
  fill: none;
  stroke: blue;
}

再加上之前例子的动画 CSS

css 复制代码
svg path {
    fill: none;
    stroke: blue;
  + stroke-width: 5;
  + stroke-dasharray: 10;
  + stroke-dashoffset: 0;
  + animation: dash 2s linear infinite;
}

+ @keyframes dash {
to {
    stroke-dashoffset: -100;
  }
}

🎉🎉🎉 是不是已经实现了开头的线条动画效果?

创建箭头移动动画

不仅是线条,普通元素也能沿着路径移动!利用 offset-path 属性,我们可以实现箭头沿路径运动的效果:

html 复制代码
<style>
  .box {
    width: 200px;
    height: 200px;
    border: 1px solid red;
    position: relative;
  }
.arrow {
    background: url(./images/arrow.png) no-repeat 00 / contain;
    width: 45px;
    height: 30px;
    offset-path: path("M125.69 0.44 L182.46 16.41 L0.23 113");
    animation: arrow-go 4s linear infinite;
  }
@keyframes arrow-go {
    0% {
      offset-distance: 0%;
    }
    100% {
      offset-distance: 100%;
    }
  }
</style>
<div class="box">
  <div class="arrow"></div>
</div>

效果解析

  • offset-path:设置运动路径,其值可以是路径字符串(与 <path> 元素的 d 属性语法相似)
  • offset-distance:控制元素在路径上的移动距离,配合动画可以实现平滑移动。

通过调整 opacity 属性,你还可以进一步增强动画效果,例如实现淡入淡出或者旋转。

绘制自定义路径

您可以通过访问 https://yqnn.github.io/svg-path-editor/ 轻松绘制各种自定义路径。

总结

通过以上示例,我们探索了 SVG 的基本用法和动画实现方式。无论是简单的直线动画、折线动画,还是路径运动,SVG 都能轻松胜任。

示例源码地址

https://github.com/zm8/wechat-oa/tree/main/svg-animation

最后

点赞👍 + 关注➕ + 收藏❤️ = 学会了🎉。

更多优质内容关注公众号,@前端大卫

相关推荐
吃饺子不吃馅2 天前
Canvas 如何渲染富文本、图片、SVG 及其 Path 路径?
前端·svg·canvas
用户984022766791814 天前
【React.js】渐变环形进度条
前端·react.js·svg
明远湖之鱼20 天前
opentype.js 使用与文字渲染
前端·svg·字体
wsWmsw21 天前
[译] 浏览器里的 Liquid Glass:利用 CSS 和 SVG 实现折射
前端·css·svg
CodeCraft Studio22 天前
CAD文件处理控件Aspose.CAD教程:在 Python 中将 SVG 转换为 PDF
开发语言·python·pdf·svg·cad·aspose·aspose.cad
红烧code1 个月前
【Rust GUI开发入门】编写一个本地音乐播放器(4. 绘制按钮组件)
rust·gui·svg·slint
吃饺子不吃馅1 个月前
AntV X6图编辑器如何实现切换主题
前端·svg·图形学
吃饺子不吃馅1 个月前
深感一事无成,还是踏踏实实做点东西吧
前端·svg·图形学
吃饺子不吃馅1 个月前
AntV X6 核心插件帮你飞速创建画布
前端·css·svg
吃饺子不吃馅1 个月前
揭秘 X6 核心概念:Graph、Node、Edge 与 View
前端·javascript·svg