html - 使圆形路径从顶部开始

标签 html css animation svg stroke-dasharray

我已经非常接近满足我的需要了。只是一个小问题,我尝试进行计算但陷入困境。 片段:

.path {
  stroke-dasharray: 120;
  stroke-dashoffset: -120;
  animation: dash 3s linear infinite;
}

@keyframes dash {
  to {
    stroke-dashoffset: 0;
  }
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 376 56.6" enable-background="new 0 0 376 56.6" xml:space="preserve">
<path fill="#232527" d="M345.8,53.3C345.8,53.3,345.8,53.3,345.8,53.3c-1.1,0-2.2-0.7-2.7-1.7l-21.7-44c-0.7-1.5-0.1-3.3,1.4-4
	c1.5-0.7,3.3-0.1,4,1.4l19,38.6l19.2-38.6c0.7-1.5,2.5-2.1,4-1.3c1.5,0.7,2.1,2.5,1.3,4l-21.9,44C348,52.6,347,53.3,345.8,53.3z
	 M274.5,53.3c-1.7,0-3-1.3-3-3v-44c0-1.7,1.3-3,3-3s3,1.3,3,3v44C277.5,51.9,276.1,53.3,274.5,53.3z M205,53.3c-1.7,0-3-1.3-3-3v-41
	h-19c-1.7,0-3-1.3-3-3s1.3-3,3-3h44c1.7,0,3,1.3,3,3s-1.3,3-3,3h-19v41C208,51.9,206.7,53.3,205,53.3z M30.5,53.2
	C30.5,53.2,30.5,53.2,30.5,53.2c-1.1,0-2.2-0.7-2.7-1.7l-16-32.5v31.1c0,1.7-1.3,3-3,3s-3-1.3-3-3v-44c0-1.4,1-2.6,2.3-2.9
	C9.6,3,11,3.6,11.6,4.9l19,38.6L49.8,4.9c0.6-1.2,2-1.9,3.4-1.6c1.4,0.3,2.3,1.5,2.3,2.9v44c0,1.7-1.3,3-3,3s-3-1.3-3-3V18.9
	L33.2,51.6C32.7,52.6,31.7,53.2,30.5,53.2z"/>
<path class="path" stroke="#232527" stroke-width="5" fill="#fff" d="M120.2,2.7c-14.1,0-25.6,11.5-25.6,25.6c0,14.1,11.5,25.6,25.6,25.6c14.1,0,25.6-11.5,25.6-25.6,25.6c14.1,0,25.6-11.5,25.6-25.6
	"/>

</svg>

我只需要一点帮助来使圆形路径动画从顶部开始。我猜我的计算有问题。我只需要像 this 这样的动画。但是它使用了我不想使用的第三方 js 插件。

还有什么方法可以将 css 嵌入到 SVG 中,以便我们可以将图形作为带有动画的 .svg 来代替将 svg 代码放入 html 文件中?

最佳答案

如果您需要笔划从圆的顶部开始,则需要更改用于制作圆的路径。我按照this answer中描述的公式又做了一个。 。我刚刚对其进行了修改,使路径从顶部开始。

然后您可以根据 Stroke-dasharrayStroke-dashoffset 值计算圆的周长 (π * 2 * r) :

.path {
  stroke-dasharray: 157;
  stroke-dashoffset: -157;
  animation: dash 6s linear infinite;
}

@keyframes dash {
  to { stroke-dashoffset: 157; }
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"  viewBox="0 0 376 56.6" enable-background="new 0 0 376 56.6" xml:space="preserve">
  <path fill="#232527" d="M345.8,53.3C345.8,53.3,345.8,53.3,345.8,53.3c-1.1,0-2.2-0.7-2.7-1.7l-21.7-44c-0.7-1.5-0.1-3.3,1.4-4 c1.5-0.7,3.3-0.1,4,1.4l19,38.6l19.2-38.6c0.7-1.5,2.5-2.1,4-1.3c1.5,0.7,2.1,2.5,1.3,4l-21.9,44C348,52.6,347,53.3,345.8,53.3z M274.5,53.3c-1.7,0-3-1.3-3-3v-44c0-1.7,1.3-3,3-3s3,1.3,3,3v44C277.5,51.9,276.1,53.3,274.5,53.3z M205,53.3c-1.7,0-3-1.3-3-3v-41 h-19c-1.7,0-3-1.3-3-3s1.3-3,3-3h44c1.7,0,3,1.3,3,3s-1.3,3-3,3h-19v41C208,51.9,206.7,53.3,205,53.3z M30.5,53.2 C30.5,53.2,30.5,53.2,30.5,53.2c-1.1,0-2.2-0.7-2.7-1.7l-16-32.5v31.1c0,1.7-1.3,3-3,3s-3-1.3-3-3v-44c0-1.4,1-2.6,2.3-2.9 C9.6,3,11,3.6,11.6,4.9l19,38.6L49.8,4.9c0.6-1.2,2-1.9,3.4-1.6c1.4,0.3,2.3,1.5,2.3,2.9v44c0,1.7-1.3,3-3,3s-3-1.3-3-3V18.9 L33.2,51.6C32.7,52.6,31.7,53.2,30.5,53.2z"/>
  <path class="path" stroke="#232527" stroke-width="5" fill="#fff" d="M120 28.3 m0 -25 a25 25 0 1 0 0 50 a25 25 0 1 0 0 -50" />
</svg>

关于html - 使圆形路径从顶部开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37417438/

相关文章:

javascript - 我的拖放不起作用

java - 使用jsoup从 "a"inside "span"inside "class"提取属性用于体育软件

html - 将 div 中的图像垂直对齐到中间

ios 多分辨率动画

ios - 如何在执行 sendSubviewToBack 之前强制 View 动画完成?

jquery - 无法使 jQuery 函数正常工作

html - 有没有办法将图像中的 GUI 转换为 html?

html - Flexbox align-content spacebetween 不工作

javascript - 如何根据某些数据更改 Canvas 元素内的颜色?

wpf - 试图为高度设置动画但得到高度为 NaN 的错误