逆时针SVG

时间:2018-02-03 20:48:59

标签: css svg css-animations stroke-dasharray

对于我正在进行的项目,我必须能够描绘-100%到100%的值。有没有什么方法可以采用现有的百分比圈,如下面的那个,让进度指示器向后移动 - 就像逆时针一样?

<div class="flex-wrapper">

  <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart orange">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle"
        stroke-dasharray="50, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">30%</text>
    </svg>
  </div>

</div> 

CSS:

.flex-wrapper {
  display: flex;
  flex-flow: row nowrap;
}

.single-chart {
  width: 5%;
  justify-content: space-around ;
}

.circular-chart {
  display: block;
  margin: 10px auto;
  max-width: 80%;
  max-height: 250px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 2.0;
}

.circle {
  fill: none;
  stroke-width: 2.0;
  stroke-linecap: square;
  animation: progress 1s ease-out forwards;
}

@keyframes progress {
  0% {
    stroke-dasharray: 0 100;
  }
}

@keyframes antiprogress {
  0% {
    stroke-dasharray: 0 100;
  }
}


.circular-chart.orange .circle {
  stroke: #f80;
}

.circular-chart.green .circle {
  stroke: #00c851;
}

.circular-chart.blue .circle {
  stroke: #33b5e5;
}

.circular-chart.blue .red {
  stroke: #ff3547;
}

.percentage {
  fill: #666;
  font-family: sans-serif;
  font-size: 0.5em;
  text-anchor: middle;
}

这是指向codepen的链接:https://codepen.io/killia15/pen/zRqvxP

Image of Desired Output

1 个答案:

答案 0 :(得分:1)

更新 :由于某些浏览器兼容性,第一个答案似乎仅适用于Chrome,您可以在下面找到更合适的答案 < / p>

您可以结合使用旋转。因此,如果你想要-20%你做stroke-dasharray="20, 100"而你应用以这种方式计算的负旋转= 360度的20%,在这种情况下-72。然后添加到动画rotate(0)以创建逆时针运动:

.flex-wrapper {
  display: flex;
  flex-flow: row nowrap;
}

.single-chart {
  width: 33%;
  justify-content: space-around;
}

.circular-chart {
  display: block;
  margin: 10px auto;
  max-width: 80%;
  max-height: 250px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3.8;
}

.circle {
  fill: none;
  stroke-width: 3.8;
  stroke-linecap: square;
  transform-origin:center;
  animation: progress 1s ease-out forwards;
}
.circle-anti {
  animation: antiprogress 1s ease-out forwards;
}

@keyframes progress {
  0% {
    stroke-dasharray: 0 100;
  }
}

@keyframes antiprogress {
  0% {
    stroke-dasharray: 0 100;
    transform: rotate(0deg);
  }
}

.circular-chart.orange .circle {
  stroke: #f80;
}

.circular-chart.green .circle {
  stroke: #00c851;
}

.circular-chart.blue .circle {
  stroke: #33b5e5;
}

.circular-chart.blue .red {
  stroke: #ff3547;
}

.percentage {
  fill: #666;
  font-family: sans-serif;
  font-size: 0.5em;
  text-anchor: middle;
}
<div class="flex-wrapper">

  <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart orange">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle circle-anti"
        stroke-dasharray="50, 100" style="transform:rotate(-180deg)"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">-50%</text>
    </svg>
  </div>

  <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart green">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle"
        stroke-dasharray="60, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">60%</text>
    </svg>
  </div>
  
   <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart green">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path style="transform:rotate(-72deg)" class="circle circle-anti"
        stroke-dasharray="20, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">-20%</text>
    </svg>
  </div>

</div>

<强>更新

这是另一个更简单的想法,无需动画旋转。我只是使用scale(-1)来反转形状,因此它将向左移动:

.flex-wrapper {
  display: flex;
  flex-flow: row nowrap;
}

.single-chart {
  width: 33%;
  justify-content: space-around;
}

.circular-chart {
  display: block;
  margin: 10px auto;
  max-width: 80%;
  max-height: 250px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3.8;
}

.circle {
  fill: none;
  stroke-width: 3.8;
  stroke-linecap: square;
  transform-origin:center;
  animation: progress 1s ease-out forwards;
}
.circle-anti {
  animation: antiprogress 1s ease-out forwards;
}

@keyframes progress {
  0% {
    stroke-dasharray: 0 100;
  }
}

@keyframes antiprogress {
  0% {
    stroke-dasharray: 0 100;
    transform: rotate(0deg);
  }
}

.circular-chart.orange .circle {
  stroke: #f80;
}

.circular-chart.green .circle {
  stroke: #00c851;
}

.circular-chart.blue .circle {
  stroke: #33b5e5;
}

.circular-chart.blue .red {
  stroke: #ff3547;
}

.percentage {
  fill: #666;
  font-family: sans-serif;
  font-size: 0.5em;
  text-anchor: middle;
}
<div class="flex-wrapper">

  <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart orange">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle"
        stroke-dasharray="50, 100" transform="scale(-1,1)"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">-50%</text>
    </svg>
  </div>

  <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart green">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle"
        stroke-dasharray="60, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">60%</text>
    </svg>
  </div>
  
   <div class="single-chart">
    <svg viewbox="0 0 36 36" class="circular-chart green">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path transform="scale(-1,1)" class="circle"
        stroke-dasharray="20, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">-20%</text>
    </svg>
  </div>

</div>

相关问题