如何为弧的SVG路径设置动画?

时间:2017-05-02 09:03:05

标签: javascript d3.js svg

我希望根据已批准的答案制作SVG路径动画 - How to calculate the SVG Path for an arc (of a circle) 我从该答案中复制了代码并仅添加了setInterval函数。但动画是错误的。我的目标是将(动画)路径绘制为正常圆。 小提琴 - https://jsfiddle.net/alexcat/64w2hc31/

感谢。

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>

2 个答案:

答案 0 :(得分:2)

describeArc(150, 150, 100, i, 270)中存在某种问题,并将其更改为describeArc(150, 150, 100, 0, i)并在完成后调用clearInterval

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>

答案 1 :(得分:1)

describeArc(150,150,100,i,270)将此更改为describeArc(150,150,100,i,360)

&#13;
&#13;
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);
&#13;
svg {
  height: 1000px;
  width: 1000px;
}
&#13;
<svg>
<path id="arc1" fill="none" stroke="#446688" stroke-width="20" />
</svg>

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