【问题标题】:SVG infinity animate not working properlySVG无限动画无法正常工作
【发布时间】:2018-12-15 07:15:33
【问题描述】:

我已经创建了 SVG 无限动画,但它运行不顺畅,它卡在中间然后流走。 SVG 中 Illustrator 中的已解决节点,但之后我遇到了同样的问题。有人可以查看代码并帮助我吗?

.svg-main {
    width: 700px;
    margin: 30px auto;
    position: relative;
}
svg#svga {
    position: absolute;
    top: 0;
    left: auto;
    bottom: auto;
    right: auto;
}

.sd1{fill:none;stroke:#000000; stroke-width:6;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
.circ{fill:#000000; }
.st0{fill:#cccccc;}
.st1{fill:#cccccc;}
.st2{fill:none;stroke:#cccccc;stroke-width:6;stroke-miterlimit:10;}
<div class="svg-main">
<svg version="1.1" id="svga" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="300px" height="200px" viewBox="0 0 685 310" style="enable-background:new 0 0 671.1 304.4;" xml:space="preserve"
	>
<g class="svg-col">
	<path class="sd1" id="loop-normal" d="M343.6,156.5c11,15.9,104.6,147.2,181.9,147.6c0.1,0,0.8,0,1,0c82.1-0.3,148.6-67,148.6-149.2
	c0-82.4-66.8-149.2-149.2-149.2c-77.2,0-170.8,131-182.2,147.5c-0.8,1.1-1.6,2.3-2.1,3.1c-10.6,15.3-104.8,147.8-182.4,147.8
	C76.7,304.2,9.9,237.4,9.9,155S76.7,5.8,159.1,5.8c77.2,0,170.7,130.9,182.2,147.5C342.1,154.4,342.9,155.6,343.6,156.5z"/>
		<animate attributeName="stroke-dasharray" attributeType="XML"
		    	from="1900, 1700"  to="200 1700"
		    	begin="0s" dur="3s"
		    	repeatCount="indefinite"/>
		<animate attributeName="stroke-dashoffset" attributeType="XML"
	    	from="-1700"  to="-3600"
	    	begin="0s" dur="3s"
	    	repeatCount="indefinite"/>  
	</path>

	<circle id="plug" class="circ" cx="0" cy="0" r="7"/> 
	<animateMotion
		xlink:href="#plug"
	  	dur="3s"
		rotate="auto"
		repeatCount="indefinite"
		calcMode="linear"
		keyTimes="0;1"    
		keySplines="0.42, 0, 0.58, 1">
		<mpath xlink:href="#loop-normal"/>
	</animateMotion> 
</g>	
</svg>
 
<!-- Generator: Adobe Illustrator 22.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="svg-bg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="300px" height="200px" viewBox="0 0 685 310" style="enable-background:new 0 0 685 310;" xml:space="preserve"> 
<path class="st2" d="M159.1,5.8C76.7,5.8,9.9,72.6,9.9,155s66.8,149.2,149.2,149.2S342.5,155,342.5,155S241.5,5.8,159.1,5.8z
	 M525.9,5.8C443.5,5.8,342.5,155,342.5,155s101,149.2,183.4,149.2S675.1,237.4,675.1,155S608.3,5.8,525.9,5.8z"/>
</svg>
</div>

【问题讨论】:

  • 请准确描述您希望破折号末端如何移动。它的长度应该随时间变化吗? - 你的&lt;animateMotion&gt; 缺少keyPoints 属性,只要你不设置calcMode="spline",定义keySplines 就没有效果
  • 谢谢@ccprog 我只是希望它不应该停止一秒钟。它应该像在左侧一样继续旋转,但在右侧它会停止一段时间,然后再次继续旋转。我也想在右侧进行相同的旋转,这样尾巴在移动时不应该停止。谢谢

标签: css animation svg css-animations svg-animate


【解决方案1】:

使用stroke-dashoffset 动画获得流畅运动的第一个先决条件是以适当的精度知道路径的长度:

document.querySelector('#loop-normal').getTotalLength() // approx. 1910
  1. 省略stroke-dasharray 的动画。而是将路径的总长度静态划分为一个破折号和一个间隙:stroke-dasharray="200 1710"。 (只要总和等于 1910,任何其他比例都可以。)
  2. 您的&lt;animateMotion&gt; 缺少keyPoints 属性。将其设置为“0;1”
  3. 只要不设置calcMode="spline",定义keySplines 就无效。别管它。

剩下的有点简化:只定义一次路径,并将背景和动画使用定位在同一个 SVG 中;在stroke-dashoffset 动画中命名正确的路径长度以确保平滑移动;删除未使用的 CSS。

.svg-main {
    width: 700px;
    margin: 30px auto;
    position: relative;
}
svg#svga {
    position: absolute;
    top: 0;
    left: auto;
    bottom: auto;
    right: auto;
}

.st2{fill:none;stroke:#cccccc;stroke-width:6;}
.sd1{fill:none;stroke:#000000; stroke-width:6;stroke-linecap:round;}
.circ{fill:#000000; }
<div class="svg-main">
<svg id="svga" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
	 width="300px" height="200px" viewBox="0 0 685 310">
  <g class="st2">
	<path id="loop-normal" d="M343.6,156.5c11,15.9,104.6,147.2,181.9,147.6c0.1,0,0.8,0,1,0c82.1-0.3,148.6-67,148.6-149.2
	c0-82.4-66.8-149.2-149.2-149.2c-77.2,0-170.8,131-182.2,147.5c-0.8,1.1-1.6,2.3-2.1,3.1c-10.6,15.3-104.8,147.8-182.4,147.8
	C76.7,304.2,9.9,237.4,9.9,155S76.7,5.8,159.1,5.8c77.2,0,170.7,130.9,182.2,147.5C342.1,154.4,342.9,155.6,343.6,156.5z"/>
  </g>
	<use class="sd1" stroke-dasharray="200 1710" xlink:href="#loop-normal">
		<animate attributeName="stroke-dashoffset"
	    	from="200"  to="-1710"
	    	begin="0s" dur="3s"
	    	repeatCount="indefinite"/>  
	</use>

	<circle id="plug" class="circ" cx="0" cy="0" r="7"/> 
	<animateMotion
		xlink:href="#plug"
	  	dur="3s"
		rotate="auto"
		repeatCount="indefinite"
		calcMode="linear"
		keyTimes="0;1"    
		keyPoints="0;1">
		<mpath xlink:href="#loop-normal"/>
	</animateMotion> 
</svg>
</div>

【讨论】:

  • 再次感谢,我只想问一件事,请您帮忙,
  • 我想让 dasharray (tail) 像这个截图一样透明,请看,prnt.sc/k7ziu0 如果可能请帮助我。再次感谢。
  • 那是一个完全不同的问题。请提出一个新问题。
  • 请您回答这个问题好吗? stackoverflow.com/questions/51417587/…
猜你喜欢
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
  • 2021-06-11
  • 1970-01-01
  • 2012-03-03
  • 2017-08-06
相关资源
最近更新 更多