alex k
alex k

Reputation: 315

How to animate the SVG path for an arc?

I want to animate SVG path based on approved answer - How to calculate the SVG Path for an arc (of a circle) I copied code from that answer and added only setInterval function. But animation is wrong. My goal is to draw (animate) path as normal circle. Fiddle - https://jsfiddle.net/alexcat/64w2hc31/

Thanks.

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle){
    var start = polarToCartesian(x, y, radius, endAngle);
    var end = polarToCartesian(x, y, radius, startAngle);

    var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

    var d = [
        "M", start.x, start.y, 
        "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
    ].join(" ");

    return d;       
}

  var i = 0;
  setInterval(function(){
     i++
    document.getElementById("arc1").setAttribute("d", describeArc(150, 150, 100, i, 270));
    if (i === 360) {
      i = 0;
    }
 }, 10);
svg {
  height: 1000px;
  width: 1000px;
}
<svg>
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" />
</svg>

<script src="https://d3js.org/d3.v3.min.js"></script>

Upvotes: 1

Views: 667

Answers (2)

arlendp
arlendp

Reputation: 95

there is kind of a problem in describeArc(150, 150, 100, i, 270) and change it to describeArc(150, 150, 100, 0, i) and call clearInterval when it's done.

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle){
    var start = polarToCartesian(x, y, radius, endAngle);
    var end = polarToCartesian(x, y, radius, startAngle);

    var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

    var d = [
        "M", start.x, start.y, 
        "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
    ].join(" ");

    return d;       
}

  var i = 0;
  var intervalId = setInterval(function(){
     i++
    document.getElementById("arc1").setAttribute("d", describeArc(150, 150, 100, 0, i));
    if (i === 360) {
      clearInterval(intervalId);
    }
 }, 10);
svg {
  height: 1000px;
  width: 1000px;
}
<svg>
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" />
</svg>

<script src="https://d3js.org/d3.v3.min.js"></script>

Upvotes: 3

Umesh Maharshi
Umesh Maharshi

Reputation: 1591

describeArc(150, 150, 100, i, 270) change this to describeArc(150, 150, 100, i, 360)

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle){
    var start = polarToCartesian(x, y, radius, endAngle);
    var end = polarToCartesian(x, y, radius, startAngle);

    var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

    var d = [
        "M", start.x, start.y, 
        "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
    ].join(" ");

    return d;       
}

  var i = 0;
  setInterval(function(){
     i++
    document.getElementById("arc1").setAttribute("d", describeArc(150, 150, 100, i, 360));
    if (i === 360) {
      i = 0;
    }
 }, 10);
svg {
  height: 1000px;
  width: 1000px;
}
<svg>
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" />
</svg>

<script src="https://d3js.org/d3.v3.min.js"></script>

Upvotes: 2

Related Questions