路径 clip-rule="evenodd" d="M33.377 4.574a3.508 3.508 0 0 0-2.633-1.126c-1 0-1.993.67-2.604 1.334l.002-1.24-1.867-.002-.02 10.17v.133l1.877.002.008-3.18c.567.611 1.464.97 2.462.973 1.099 0 2.022-.377 2.747-1.117.73-.745 1.1-1.796 1.103-3.002.003-1.232-.358-2.222-1.075-2.945Zm-3.082.55c.637 0 1.176.23 1.602.683.438.438.663 1.012.66 1.707-.003.7-.22 1.33-.668 1.787-.428.438-.964.661-1.601.661-.627 0-1.15-.22-1.6-.666-.445-.46-.662-1.086-.662-1.789.003-.695.227-1.27.668-1.708a2.13 2.13 0 0 1 1.596-.675h.005Zm5.109-.067-.008 4.291c-.002.926.263 1.587.784 1.963.325.235.738.354 1.228.354.376 0 .967-.146.967-.146l-.168-1.564s-.43.133-.64-.01c-.198-.136-.296-.428-.296-.866l.008-4.022 1.738.002.002-1.492-1.738-.002.005-2.144-1.874-.002-.005 2.143-1.573-.002 1.57 1.497ZM20.016 1.305h-9.245l-.002 1.777h3.695l-.016 8.295v.164l1.955.002-.008-8.459 3.621-.002V1.305Z" fill="#262D3D" fill-rule="evenodd">路径><路径 clip-rule="evenodd" d="M10.06 5.844 7.277 3.166 4.015.03 2.609 1.374l2.056 1.978-4.51 4.313 6.065 5.831 1.387-1.327-2.073-1.994 4.526-4.331ZM4.274 8.7a.211.211 0 0 1-.124 0c-.04-.013-.074-.03-.15-.102l-.817-.787c-.072-.069-.092-.104-.105-.143a.187.187 0 0 1 0-.12c.013-.039.03-.07.105-.143L5.76 4.938c.072-.07.108-.09.15-.099a.21.21 0 0 1 .123 0c.041.012.075.03.15.101L7 5.727c.072.07.093.104.103.144.013.04.013.08 0 .119-.013.04-.03.072-.106.143L4.422 8.601a.325.325 0 0 1-.147.099Z" fill="#204ECF" fill-rule="evenodd">路径><路径 clip-rule="evenodd" d="M24.354 4.622a3.94 3.94 0 0 0-2.876-1.149 4.1 4.1 0 0 0-2.829 1.084c-.804.725-1.214 1.733-1.217 2.992-.002 1.26.405 2.267 1.207 2.995a4.114 4.114 0 0 0 2.832 1.094c.04.002.082.002.123.002a3.967 3.967 0 0 0 2.75-1.138c.538-.532 1.183-1.473 1.186-2.938.002-1.465-.637-2.408-1.176-2.942Zm-.59 2.94c-.003.73-.228 1.334-.671 1.794-.441.458-.99.69-1.633.69a2.166 2.166 0 0 1-1.614-.697c-.43-.45-.65-1.057-.65-1.797s.222-1.344.655-1.795a2.17 2.17 0 0 1 1.617-.69c.64 0 1.189.235 1.63.698.443.46.668 1.064.665 1.797ZM41.15 6.324c0-.458.25-1.465 1.632-1.465.49 0 .768.159 1.003.347.227.18.34.626.34.994v.174l-2.282.341C40.035 6.98 39 7.913 38.993 9.28c-.002.708.266 1.314.777 1.76.503.438 1.191.67 2.004.673 1.023 0 1.792-.354 2.341-1.084.003.31.003.621.003.91h1.903l.013-5.246c.002-.856-.289-1.685-.864-2.14-.567-.449-1.31-.679-2.386-.681h-.015c-.82 0-1.69.208-2.274.695-.689.572-1.027 1.478-1.027 2.178l1.682-.02Zm.864 3.814c-.676-.002-1.115-.371-1.112-.938.003-.589.43-.933 1.346-1.081l1.875-.305v.017c-.005 1.36-.87 2.307-2.102 2.307h-.008Zm4.917-8.712-.018 10.058v.044l1.684.005.018-10.06v-.045l-1.684-.002Zm2.654 9.491c0-.173.062-.322.19-.445a.645.645 0 0 1 .462-.186c.18 0 .338.062.465.186a.596.596 0 0 1 .193.445.583.583 0 0 1-.193.443.644.644 0 0 1-.465.183.634.634 0 0 1-.461-.183.59.59 0 0 1-.191-.443Zm.108 0c0 .146.052.273.158.376a.54.54 0 0 0 .389.154.539.539 0 0 0 .547-.53.498.498 0 0 0-.16-.373.531.531 0 0 0-.387-.156.531.531 0 0 0-.387.155.497.497 0 0 0-.16.374Zm.702.344-.176-.3h-.118v.3h-.109v-.688h.292c.144 0 .23.082.23.196 0 .096-.076.168-.176.188l.178.304h-.121Zm-.294-.596v.21h.167c.093 0 .14-.034.14-.104 0-.072-.047-.106-.14-.106h-.167Z" fill="#262D3D" fill-rule="evenodd">路径>作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
Juan is a systems engineer since 2005. 他是一个渴望学习的人,一个多才多艺的开发人员,并且非常注重细节.
任何称职的前端工程师都知道碎片化的设备生态系统带来的挑战. 不同的屏幕尺寸, 决议, 和 aspect ratios make it difficult to deliver a consistent experience. More so for those who want to deliver a pixel-perfect experience.
可缩放矢量图形(svg)帮助解决了部分问题,并且做得非常好. Although they have their limitations, SVGs can be very helpful for certain occasions 和, 如果你有一个优秀的设计团队, 你也可以在不增加浏览器不必要的负担或阻碍加载时间的情况下,创造出更令人惊叹的视觉体验.
在过去的几个月, 我一直致力于一个广泛使用SVG及其动画和效果功能的项目. 在本文中, 我将分享如何使用SVG及其动画技术为您的web前端工作带来一些新的生命.
SVG is an image format that is based on XML, much like how HTML works. 它为许多熟悉的几何形状定义了不同的元素,这些几何形状可以在标记中组合以生成二维图形.
的 SVG规范 is an open st和ard developed by the World Wide Web Consortium (W3C) in 1999.
All major web browsers have had SVG渲染支持 有一段时间了.
Since SVG graphics are XML documents, web浏览器提供了基于DOM节点的api,可用于与图像交互. 谈论 让图片栩栩如生!
If t在这里 is one overpowered 元素 in SVG, that would be the <路径>
元素.
路径元素是一个基本的形状,可以用来创建几乎任何你能想到的高级2D形状.
的 元素 works by taking a sequence of drawing comm和s. It is a lot like the Logo programming language from 1967, only modernized 和 designed for fancy graphics. 的 元素 takes 这 sequence of drawing comm和s through the attribute d
.
<路径 d="M10 10 L75 10 L75 75 Z" />
You can think of a virtual pen that draws on the screen, 和 the drawing comments in the 路径 元素 merely controls the pen. In the example above, the pen is being instructed to move to position (10, 10)
(M10 10
),划清界限 (75, 10)
(L75 10
),划清界限 (75, 75)
L75 75
和 然后 to close the 路径 by returning to the starting point (Z
).
Using other drawing comm和s, such as arcs (A
)、二次贝塞尔曲线(Q
)、三次贝塞尔曲线(C
), etc you can create much more complex shapes 和 graphics in SVG.
You can learn a lot more about the 路径 元素 在这里.
“好吧,胡安,我明白了. Paths are powerful, but how do I animate them?你说.
对于我们的第一项技术,我们将利用两个SVG属性: stroke-dasharray
和 stroke-dashoffset
.
的 stroke-dasharray attribute controls the pattern of dashes 和 gaps used to stroke the 路径. 如果你想把你的线条画成一组破折号和空白,而不是一个连续的笔画, 这 is the attribute you would use.
SVG图像是web浏览器DOM的一部分,stroke-dasharray是一个表示元素, the attribute can also be set using CSS.
类似地, stroke-dashoffset 属性(指定在破折号模式中开始破折号的距离)也可以使用CSS来控制.
这两个SVG属性, 在一起, can be used to animate SVG 路径s, giving the viewer the illusion that the 路径s are being drawn gradually.
Take 这 quadratic bezier curve, for example:
<路径 fill="transparent" stroke="#000000" stroke-width="5" d="M10 80 Q 77.5 10, 145 80 T 280 80" class="路径">路径>
将这条路径动画化,就好像它是在屏幕上逐渐平滑地绘制的, we will have to set the dash (和 gap) lengths, using the stroke-dasharray attribute, 等于路径长度. 这是为了使虚线曲线中的每个破折号和间隙的长度等于整个路径的长度.
Next, we will set the dash offset, using the stroke-dashoffset attribute, to 0. 这将使路径在屏幕上显示为一条实曲线(我们实际上是在看第一个破折号), 和 we already made each dash span the entire length of the curve). By setting the dash offset equal to the length of the curve, 我们最终会得到一条看不见的曲线(我们现在看到的曲线被渲染成一个完整的缺口——紧跟着破折号的部分)。.
现在,通过动画stroke-dashoffset属性,您可以使曲线逐渐出现在屏幕上.
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
As you can see, the curve is always t在这里. We are only changing the dash offset to make the dashed part appear 位 by 位.
你可以使用相同的原则,但要有更多的路径:
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
Here you have one black curve that is fixed, a red one that is moving along the 路径, 和 another black one following the red one but 40px behind.
Stroke-dasharray和stroke-dashoffset是两个非常强大的属性,可用于向SVG路径应用大量动画和效果. 你可以试试 这 h和y tool that you can use to experiment with the two attributes.
使用SVG和CSS,您可以做的另一件很酷的事情是使对象或元素沿着路径移动.
T在这里 are 2 SVG attributes we are going to use for the animation:
offset-路径: offset-路径 CSS属性指定元素定位的偏移路径.
offset-distance: offset-distance CSS属性指定偏移路径上的位置.
通过结合这两个属性,你可以很容易地制作出这样的动画:
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
As you can see, we have a new 元素 div.球
.
This 元素 can be anything, a div, an image, text, whatever. 在这个例子中的想法是,通过使用offset-路径和offset-distance,你可以给元素一个路径来跟随和动画距离,元素将通过路径移动.
如果所有这些还不够花哨,那么您总是可以求助于JavaScript.
Animating SVG 元素s 使用JavaScript can be much like animating DOM 元素s. 然而, 使用JavaScript, you can achieve the animation techniques we have checked out above, 但是更容易.
Previously, we had to hardcode the 路径 lengths in our CSS. With the help of the JavaScript function 路径.getTotalLength ()
可以即时计算路径的长度并根据需要使用它. 你可以了解更多 在这里.
除了, 已经有许多库可供您使用,它们可以使SVG动画变得比现在简单得多.
提前.svg not only makes it easy to draw SVG images using JavaScript, it makes animating them as simple as calling .动画({})
.
另一个库, 动漫.js, lets you make a div 元素 follow an SVG 路径 with just a 几行代码.
如果你正在寻找一个库,它可以自己做更多的事情,但结果仍然令人惊叹, 然后 Vivus 是你要找的吗. It takes a different, more configuration-driven approach to SVG 路径 animation. 有了这个图书馆, 您只需要向要绘制的SVG元素添加一个ID,并使用该ID定义一个Vivus对象. Vivus will take care of the rest.
以下是在处理SVG图像并将其动画化时可能会发现有用的资源列表:
To go more in depth with SVG animation, you can read 这 short article on the SVG动画的三种方法 images 和 watch the video screencast by CSS Tricks.
本文没有介绍的一点是使用同步多媒体集成语言(SMIL)为SVG图像制作动画. 而将CSV用于SVG则为您提供了使用您已经熟悉的东西的优势, SMIL takes things to the next level.
使用SMIL,您可以单独使用SVG实现高级动画效果,例如形状变形. A short, yet effective guide to using SMIL for such effects is available 在这里.
Although, support for SMIL is a 位 前卫 at 这 moment (no pun intended).
在本文中, 您已经了解了使用SVG路径上的CSS或HTML元素为SVG元素制作动画的几种方法.
SVG是轻量级的,无论屏幕大小如何,都可以用来生成清晰的图形, 缩放级别, 屏幕分辨率. 使用SVG, 提供现代的, vivid experience now is easier than ever, all while reaping the benefits of using st和ard web technologies.
就是这样! 希望您觉得这个SVG动画教程很有用,并且喜欢阅读它.
stroke-dasharray属性确定要用作SVG路径笔画的破折号和空格的模式. stroke-dashoffset决定路径笔划开始时进入模式的距离.
To determine the exact length of an SVG 路径, you can use the 路径.getTotalLength () function, w在这里 路径 is the DOM Node for the <路径> 元素.路径>
布宜诺斯艾利斯,阿根廷
2016年12月20日成为会员
Juan is a systems engineer since 2005. 他是一个渴望学习的人,一个多才多艺的开发人员,并且非常注重细节.
World-class articles, delivered weekly.
World-class articles, delivered weekly.