SVG逆时针 [英] SVG Counterclockwise

查看:91
本文介绍了SVG逆时针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在从事的项目,我必须能够描述从-100%到100%的值.我有什么办法可以采用现有的百分比圈(例如下面的那个)并使进度指示器向后-逆时针旋转?

For the project I'm working on, I have to be able to depict values from -100% to 100%. Is there any way that I can take an existing percentage circle such as the one below and make the progress indicator go backwards - as in counterclockwise?

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

以下是指向代码笔的链接: https://codepen.io/killia15/pen/zRqvxP

Here's a link to the codepen: https://codepen.io/killia15/pen/zRqvxP

所需输出图像

推荐答案

UPDATE :由于某些浏览器的兼容性,第一个答案似乎仅适用于chrome,您可以在下面找到更多信息合适的答案

UPDATE : the first answer seems to work only on chrome due to some browser compatibility, you can find below a more suitable answer

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

You may combine the use of rotation. So if you want -20% you make the stroke-dasharray="20, 100" and you apply a negative rotation calculated in this way = 20% of 360 degree which will be in this case -72. Then add to the animation rotate(0) to create the anti-clockwise movement:

.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)来反转形状,因此它将移到左侧:

Here is another easier idea without the need of animating the rotation. I simply use a scale(-1) in order to inverse the shape and thus it will go to the left:

.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>

这篇关于SVG逆时针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆