【问题标题】:SVG: filled arc animation?SVG:填充弧动画?
【发布时间】:2017-08-30 04:05:52
【问题描述】:

此问题是Define a circle / arc animation in SVGHow to calculate the SVG Path for an arc (of a circle) 的扩展。

我将@opsb的答案修改如下:

function calculateArcPath(x, y, radius, spread, startAngle, endAngle){
    var innerStart = polarToCartesian(x, y, radius, endAngle);
  	var innerEnd = polarToCartesian(x, y, radius, startAngle);
    var outerStart = polarToCartesian(x, y, radius + spread, endAngle);
    var outerEnd = polarToCartesian(x, y, radius + spread, startAngle);
    var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
		
    var d = [
        "M", outerStart.x, outerStart.y,
        "A", radius + spread, radius + spread, 0, largeArcFlag, 0, outerEnd.x, outerEnd.y,
        "L", innerEnd.x, innerEnd.y, 
        "A", radius, radius, 0, largeArcFlag, 1, innerStart.x, innerStart.y, 
        "L", outerStart.x, outerStart.y, "Z"
    ].join(" ");

    return d;
}

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))
  };
}

var startPath = calculateArcPath(250, 250, 50, 30, 0, 30)
var endPath = calculateArcPath(250, 250, 50, 30, 0, 150)

d3.select("path").attr("d", startPath)
d3.select("path").transition().duration(2000).attr("d", endPath)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg width="500" height="500" style="border:1px gray solid">  
  <path id="path" fill="blue" stroke="black"></path>
</svg>

但是,路径不是围绕圆圈的平滑过渡。

【问题讨论】:

    标签: javascript svg


    【解决方案1】:

    您的动画看起来很奇怪,因为您在两个曲线形状之间进行线性动画。一个端点保持固定,而另一个端点沿直线移动(而不是沿圆弧)。

    我认为您会发现使用stroke-dashoffset trick 为您的曲线设置动画会更容易。这是一个简单的例子:

    function update_arc() {
      /* Fetch angle from input field */
      angle = document.getElementById("ang").value;
      if (angle < 0) angle = 0;
      if (angle > 360) angle = 360;
      /* Convert to path length using formula r * θ (radians) */
      var arclen = Math.PI * 50 * (360-angle) / 180.0;
      /* Set stroke-dashoffset attribute to new arc length */
      document.getElementById("c").style.strokeDashoffset = arclen;
    }
    #c {
      stroke-dashoffset: 157.08;
      stroke-dasharray: 314.16;
      -webkit-transition: stroke-dashoffset 0.5s;
      transition: stroke-dashoffset 0.5s;
    }
    <svg width="120" height="120" viewBox="0 0 120 120">
      <!-- Circle element of radius 50 units. -->
      <!-- (Rotated 90° CCW to put start point is at top.) -->
      <circle id="c" cx="60" cy="60"
              r="50" fill="none" stroke="blue"
              stroke-width="15"
              stroke-dashoffset="157.08"
              stroke-dasharray="314.16"
              transform="rotate(-90 60 60)" />
    </svg>
    <p>Enter new angle (0–360):<br />
    <input type="text" id="ang" width="20" value="180" />
    <button onclick="return update_arc()">Set</button></p>

    【讨论】:

    • 最初我实际上是这样做的....但是,如果您使用粗笔画来制作弧线,那么您要么没有填充,要么无法创建轮廓(使用相同的 SVG 元素虽然你总是可以再做一个概述它)。使用不是通过粗笔画宽度制作的弧线段是问题的症结所在,以及它在 S.O. 中的合理性。鉴于已经有几个关于 stroke-dashoffset 技巧
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 2019-11-10
    • 2018-07-30
    • 2017-08-14
    • 2018-05-20
    • 1970-01-01
    • 2015-12-07
    • 2018-01-10
    相关资源
    最近更新 更多