【问题标题】:SVG Progressbar Animation with start circle带有开始圆圈的 SVG 进度条动画
【发布时间】:2016-11-29 11:11:31
【问题描述】:

是否可以沿线路径为圆设置动画?

我已经尝试了以下代码。我可以合并两条线路径和圆圈并应用相同的过渡

$(document).ready(function () {

    var svgPath = document.getElementById('heart');

    var path = new ProgressBar.Path(svgPath, {
        duration: 3000
    });
    path.animate(-1, {
        easing: 'easeOutBounce',
        duration: 2000
    }, function () {
        console.log('Animation has finished');
    });
});
 #container {
	width:220px;
    position: relative;
 }
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
<div id="container" style="width:200px;height:200px;">

    <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100.08 100" width="500"
        height="500">
        <path d="M50,2A48,48,0,1,1,2,50,48,48,0,0,1,50,2" fill-opacity="0" stroke="#eee" />
        <circle cx="95.87" cy="64.33" r="2.66" />
        <path id="heart" fill-opacity="0" d="M95.6,65.16A48,48,0,1,1,50,2" fill="none" stroke="#000" />
    </svg>


</div>

【问题讨论】:

  • 不太清楚你所说的“动画圆圈”是什么意思。你能澄清一下吗?
  • 检查我的例子 - 圆没有动画路径只有描边路径被动画了
  • 所以你想自己绕圈旋转(像跳动者一样)?
  • 不像 throbber - 我想要用路径动画的圆圈,你只能看到动画路径而不是圆圈
  • 恐怕还不清楚您到底在寻找什么。我会选择一个作为答案,希望它能给你你想要的:)

标签: javascript jquery css animation svg


【解决方案1】:

该库允许您传递一个step 函数,该函数在动画的每个步骤中都会被调用。

使用 value() 函数返回的值和几个方便的 SVG 路径函数,您可以计算进度线末端的坐标。然后,您可以使用这些坐标来定位圆。

下面的演示:

$(document).ready(function(){
  
  var svgPath = document.getElementById('heart'); 
  
  var shape = new ProgressBar.Path(svgPath);
  var pathLen = shape.path.getTotalLength();

  shape.animate(-1, {
	easing: 'easeOutBounce',
    duration: 2000,
    attachment: document.getElementById("circle"),
    step: function(state, shape, attachment) {
      // 'attachment' is a DOM reference to the circle element
      var val =  1 + shape.value();  // buggy value() function?
      var endPosition = shape.path.getPointAtLength(val * pathLen);
      attachment.cx.baseVal.value = endPosition.x;
      attachment.cy.baseVal.value = endPosition.y;
    }
  }, function() {
    console.log('Animation has finished');
  });

});
#container {
  width:220px;
  position: relative;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script>
 <div id="container" style="width:200px;height:200px;">

    <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100.08 100" width="500" height="500">
    <path d="M50,2A48,48,0,1,1,2,50,48,48,0,0,1,50,2" fill-opacity="0" stroke="#eee"/>
    <circle id="circle" cx="95.87" cy="64.33" r="2.66"/>
    <path  id="heart" fill-opacity="0"  d="M95.6,65.16A48,48,0,1,1,50,2" fill="none" stroke="#000" />
    </svg>


    </div>

请注意,该库似乎有一个错误。根据文档,value() 函数应该返回一个介于01 之间的值。然而,它返回一个介于0-1 之间的值。另外,它是从后到前的。因此,为了获得正确的进度值,我必须将1 添加到shape() 的值中。如果您更新到修复此错误的库的新版本,您可能需要更改此代码。

【讨论】: