路径 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">路径>authors are vetted experts in their fields 和 write on topics in which they have demonstrated experience. All of our content is peer reviewed 和 validated by Toptal experts in the same field.
Juan自2005年起担任系统工程师. He is an eager learner, a versatile developer, 和 has great attention to detail.
Any front-end engineer worth their salt is aware of the challenges that the fragmented ecosystem of devices brings. 不同的屏幕尺寸, 决议, 而且长宽比也很难提供一致的体验. 对于那些想要提供完美像素体验的人来说更是如此.
可缩放矢量图形 (SVGs) help in solving a part of that problem, 和 do it very well. 尽管它们有其局限性, svg在某些场合非常有用, 如果你有一个优秀的设计团队, you can also create a more visually stunning experience without putting undue burden the web browser or hampering the load times.
在过去的几个月, I have been working on a project that is making extensive use of SVG 和 its animation 和 effect capabilities. 在本文中, I will share how you can use SVG 和 its animation techniques to bring some new life to your web front-end work.
SVG是一种基于XML的图像格式,与HTML的工作原理非常相似. It defines different 元素s for a number of familiar geometric shapes that can be combined in the markup to produce two-dimensional graphics.
的 SVG规范 是万维网联盟(W3C)于1999年制定的开放标准.
所有主要的网络浏览器都有 SVG渲染支持 有一段时间了.
因为SVG图形是XML文档, web browsers provide DOM node-based APIs that can be used to interact with the images. 谈论 让图片栩栩如生!
如果SVG中有一个强大的元素,那就是 <路径>
元素.
的 路径 元素 is a basic shape that may be used to create almost any advanced 2D shape you can imagine.
元素通过接受一系列绘图命令来工作. 它很像1967年的Logo编程语言, 只有现代化和设计花哨的图形. 元素通过属性接受这一系列的绘图命令 d
.
<路径 d="M10 10 L75 10 L75 75 Z" />
你可以想象一支在屏幕上画画的虚拟笔, 路径元素中的绘图注释仅控制笔. 在上面的例子中,笔被指示移动到某个位置 (10, 10)
(M10 10
),划清界限 (75, 10)
(L75 10
),划清界限 (75, 75)
L75 75
然后通过返回起点来关闭路径(Z
).
使用其他绘图命令,例如arcs (A
)、二次贝塞尔曲线(Q
)、三次贝塞尔曲线(C
)等,您可以在SVG中创建更复杂的形状和图形.
您可以了解更多关于路径元素的信息 在这里.
“好吧,胡安,我明白了. 路径是强大的,但我如何动画它们?你说.
对于我们的第一项技术,我们将利用两个SVG属性: stroke-dasharray
和 stroke-dashoffset
.
的 stroke-dasharray 属性控制用于描边路径的破折号和空白的模式. If you wanted to draw your lines as a group of dashes 和 gaps instead of one continuous stroke of ink, 这是您将使用的属性.
使用SVG images being a part of the web browser’s DOM 和 stroke-dasharray being a presentation 元素, 该属性也可以使用CSS设置.
类似地, stroke-dashoffset attribute (which specifies how far into the dash pattern to start the dash) can also be controlled using CSS.
这两个SVG属性, 在一起, 可以用来动画SVG路径, 给观众一种错觉,即路径是逐渐绘制的.
以二次贝塞尔曲线为例:
<路径 fill="transparent" stroke="#000000" stroke-width="5" d="M10 80 Q 77.5 10, 145 80 T 280 80" class="路径">路径>
To animate 这 路径 as if it is being drawn gradually 和 smoothly on the screen, 我们必须设置破折号(和间隙)长度, 使用stroke-dasharray属性, 等于路径长度. This is so that the length of each dash 和 gap in the dashed curve is equal to the length of the entire 路径.
Next, we will set the dash offset, using the stroke-dashoffset attribute, to 0. This will make the 路径 appear on screen as a solid curve (we are essentially looking at the first dash, 我们已经让每个破折号跨越了曲线的整个长度). 通过将虚线偏移量设置为曲线的长度, we will end up with an invisible curve (we are now looking at the curve being rendered as an entire gap—the part that immediately follows a dash).
Now by animating the stroke-dashoffset property, you can make the curve appear on screen gradually.
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
如你所见,曲线总是存在的. 我们只是改变了虚线偏移量,使虚线部分一点一点地出现.
You can take 这 a step further by using the same principle but with more 路径s:
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
这里有一条固定的黑色曲线, 沿着小路移动的红色的, 红色的后面还有一个黑色的,在40px后面.
Stroke-dasharray 和 stroke-dashoffset are two very powerful attributes that can be used to apply a plethora of animations 和 effects to your SVG 路径s. 你可以试试 这 方便的工具,您可以使用它来试验这两个属性.
使用SVG 和 CSS, another cool thing you can do is animate objects or 元素s following a 路径.
我们将在动画中使用2个SVG属性:
offset-路径: 的 offset-路径 CSS property specifies the offset 路径 w在这里 the 元素 gets positioned.
offset-distance: 的 offset-distance CSS property specifies a position along an offset-路径.
By combining these two properties, you can come up with animations like 这 easily:
看笔 total - SVG & CSS路径 by total 博客 (@toptalblog) on CodePen.
如你所见,我们有了一个新元素 div.球
.
这个元素可以是任何东西,div,图像,文本,等等. 的 idea in 这 example is that with the use of offset-路径 和 offset-distance you can give the 元素 a 路径 to follow 和 animate the distance 和 the 元素 will move through the 路径.
If all of these are not fancy enough already, you can always resort to JavaScript.
用JavaScript为SVG元素制作动画与为DOM元素制作动画非常相似. 然而, 使用JavaScript, 你可以实现我们上面检查的动画技术, 但是更容易.
以前,我们必须在CSS中硬编码路径长度. 借助JavaScript函数 路径.getTotalLength ()
it is possible to calculate the length of the 路径 on-the-fly 和 use it as needed. 你可以了解更多 在这里.
除了, a number of libraries are already at your disposal that can make SVG animations a lot easier than it already is.
提前.svg 不仅使使用JavaScript绘制SVG图像变得容易, 这使得动画就像打电话一样简单 .动画({})
.
另一个库, 动漫.js,使div元素跟随SVG路径 几行代码.
If you are looking for a library that does more on its own but makes the results still look stunning, 然后 Vivus 是你要找的吗. 它采用一种不同的、更多由配置驱动的方法来实现SVG路径动画. 有了这个图书馆, you just have to add an ID to the SVG 元素 you want to draw 和 define a Vivus object with that ID. 维乌斯会处理好剩下的.
Below are a list of resources that you may find useful when dealing with SVG images 和 animating them:
要更深入地了解SVG动画,可以阅读web上的这篇短文 SVG动画的三种方法 图片和观看视频截屏由CSS技巧.
One thing 这 article didn’t cover is animating SVG images with Synchronized Multimedia Integration Language (SMIL). While using CSV for SVG gives you the advantage of working with something you are already familiar with, SMIL将事情带到了下一个层次.
With SMIL, you can implement advanced animation effects such as shape morphing using SVG alone. 关于如何使用SMIL实现这种效果,有一个简短而有效的指南 在这里.
尽管如此,对SMIL的支持仍然是一个问题 位 前卫 此时此刻(没有双关语).
在本文中, you have gone through several ways you can animate SVG 元素s using CSS or HTML 元素 on SVG 路径s.
SVG is lightweight 和 can be used to produce sharp graphics regardless of screen size, 缩放级别, 屏幕分辨率. 使用SVG, 提供现代的, 生动的体验现在比以往任何时候都容易, 同时获得使用标准web技术的好处.
就是这样! Hopefully you have found 这 SVG animation tutorial useful 和 enjoyed reading it.
的 stroke-dasharray attribute determines the pattern of dashes 和 gaps to be used as the stroke for an SVG 路径. 的 stroke-dashoffset determines the distance into the pattern from which the 路径’s stroke will start.
要确定SVG路径的确切长度,可以使用路径.getTotalLength ()函数,其中路径是对象的DOM Node <路径> 元素.路径>
布宜诺斯艾利斯,阿根廷
2016年12月20日成为会员
Juan自2005年起担任系统工程师. He is an eager learner, a versatile developer, 和 has great attention to detail.
世界级的文章,每周发一次.
世界级的文章,每周发一次.