【问题标题】:How to change the "starting point" of the stroke-dasharray in a SVG image?如何更改 SVG 图像中 stroke-dasharray 的“起点”?
【发布时间】:2014-10-01 02:27:09
【问题描述】:

我想为自定义徽标制作动画,例如典型的进度圈(例如百分比)。 因此,我创建了一个带有笔划的圆圈,并为 svg 徽标设置了一个剪辑路径。

我快完成了,但我需要将笔画动画的起点从左侧(参见代码)更改为顶部 - 或箭头旁边。这样进度就从箭头之一开始。 stroke-dasharray 和 stroke-dashoffset 的不同值将不起作用。

<html>
<head>
    <title></title>
  <style type="text/css">
  path.progress-logo {
    stroke-dasharray: 204.243;
    stroke-dashoffset: 122.763;
  }
</style>
</head>
<body>
<svg id="svg2">

<path fill="#E2E2E1" d="M100,39.414L81.975,21.982L63.89,39.414h9.86v30.97H17.164V23.659h42.563l-0.033,11.304l18.028-17.729
  L59.799-0.28l-0.031,10.737H18.281c-7.164,0-12.001,1.232-14.512,3.702C1.257,16.627,0,21.212,0,27.917v38.904
  c0,6.752,1.257,11.35,3.768,13.794c2.511,2.443,7.348,3.665,14.512,3.665h54.283c7.165,0,12.003-1.222,14.514-3.665
  c2.511-2.444,3.769-7.042,3.769-13.794V39.414H100z"/>

  <defs>
    <clipPath id="cut-off-bottom">
<path fill="#E2E2E1" d="M100,39.414L81.975,21.982L63.89,39.414h9.86v30.97H17.164V23.659h42.563l-0.033,11.304l18.028-17.729
  L59.799-0.28l-0.031,10.737H18.281c-7.164,0-12.001,1.232-14.512,3.702C1.257,16.627,0,21.212,0,27.917v38.904
  c0,6.752,1.257,11.35,3.768,13.794c2.511,2.443,7.348,3.665,14.512,3.665h54.283c7.165,0,12.003-1.222,14.514-3.665
  c2.511-2.444,3.769-7.042,3.769-13.794V39.414H100z"/>
    </clipPath>
  </defs>
  <path class="progress-logo" d="m0.66666,46.55556c0,-22.98956 18.62155,-41.61111 41.61111,-41.61111c22.98957,0 41.61111,18.62155 41.61111,41.61111c0,22.98956 -18.62154,41.61111 -41.61111,41.61111c-22.98956,0 -41.61111,-18.62155 -41.61111,-41.61111z" clip-path="url(#cut-off-bottom)" stroke-width="40" fill="none" stroke="#1ECD97"  />



</svg>

</body>
</html>

这是我当前的代码:http://jsfiddle.net/w5huatyf/

感谢您的帮助!

【问题讨论】:

    标签: html css animation svg stroke-dasharray


    【解决方案1】:

    你的答案在这里:

    http://jsfiddle.net/w5huatyf/2/

    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 arcSweep = endAngle - startAngle <= 180 ? "0" : "1";
    
        var d = [
            "M", start.x, start.y, 
            "A", radius, radius, 0, arcSweep, 0, end.x, end.y,
            "L", x,y,
            "L", start.x, start.y
        ].join(" ");
    
        return d;       
    }
    
    var percent = 0;
    var startValue = 43;
    var endValue = 360+startValue-0.01;
    function count(){
        if(percent < startValue)
        percent = startValue;
        else
            percent++;
        if(percent > endValue)
            percent = endValue;
        document.getElementById("arc1").setAttribute("d", describeArc(50, 50, 60, startValue, percent)); 
       document.getElementById("vals").innerHTML = percent + ' Degree';
        if(percent < endValue)
        window.setTimeout(function() {  count(); }, 30);
    }
    
    window.setTimeout(function() {  count( ); }, 30);
    
    count(0,359); 
    

    这是一个简单的计时器,每次使用计时器替换弧值。

    我使用了其他一些答案。以下是一些:

    定时器:https://stackoverflow.com/a/33676171/2655623(我自己回答)

    画饼:https://stackoverflow.com/a/24569190/2655623(结果对我不起作用,但函数很有用)

    如果您需要帮助解释代码,请告诉我。

    【讨论】:

    • 如果你画一个完整的弧线,它会消失(0度到360度=无),所以你必须从(0度到[360-0.01]度)画它。跨度>
    猜你喜欢
    • 2018-05-12
    • 2021-08-08
    • 2019-04-03
    • 2019-08-18
    • 2014-04-02
    • 2013-12-23
    • 2020-09-16
    • 2019-01-21
    • 2016-03-13
    相关资源
    最近更新 更多