Skip to content Skip to sidebar Skip to footer

Svg Progressbar Animation With Start Circle

Is it possible to animate circle along with the line path ? I have tried following code.can i merge the two line path and circle and apply transition for the same

Solution 1:

That library allows you to pass a step function that is called for every step in the animation.

Using that, the value returned by the value() function, and a couple of handy SVG path functions, you can calculate the coordinates of the end of the progress line. You can then use those coordinates to position the circle.

Demo below:

$(document).ready(function(){
  
  var svgPath = document.getElementById('heart'); 
  
  var shape = newProgressBar.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 elementvar 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;
}
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="https://rawgit.com/kimmobrunfeldt/progressbar.js/1.0.0/dist/progressbar.js"></script><divid="container"style="width:200px;height:200px;"><svgid="Layer_1"data-name="Layer 1"xmlns="http://www.w3.org/2000/svg"viewBox="0 0 100.08 100"width="500"height="500"><pathd="M50,2A48,48,0,1,1,2,50,48,48,0,0,1,50,2"fill-opacity="0"stroke="#eee"/><circleid="circle"cx="95.87"cy="64.33"r="2.66"/><pathid="heart"fill-opacity="0"d="M95.6,65.16A48,48,0,1,1,50,2"fill="none"stroke="#000" /></svg></div>

Note that the library seems to have a bug. According to the docs, the value() function is supposed to return a value between 0 and 1. However it returns a value between 0 and -1. Plus it's back-to-front. So to get the right progress value, I had to add 1 to the value of shape(). If you ever update to a new version of the library that fixes this bug, you may have to alter this code.

Post a Comment for "Svg Progressbar Animation With Start Circle"