简体   繁体   English

使用SVG绘制圆形路径并控制其开始/结束

[英]Drawing circular path with svg and controlling its start / end

Is it possible to draw a circular path like this 是否可以绘制这样的圆形路径

在此处输入图片说明

via svg paths in a way where it is possible to manipulate it to either be 100% (so full circle) or stop at 50%, so top to bottom forming half a circle. 通过svg路径以某种方式可以将其操纵为100%(这样一个完整的圆圈)或停在50%的位置,因此从上到下形成一个半圆。 Note 100% and 50% are examples used, in real world any percentage from 0 to 100 should be valid. 注意100%和50%是示例,在现实世界中,从0到100的任何百分比都是有效的。

After some research I found that following approach seems to be how you draw a circle https://stackoverflow.com/a/8193760/911930 but I don't see any way to implement this where I can draw / stop path at a given percentage. 经过研究后,我发现以下方法似乎是您绘制圆圈的方式https://stackoverflow.com/a/8193760/911930,但我看不到有什么方法可以在给定位置绘制/停止路径百分比。

There are numerous examples of how to do this, here on Stack Overflow, and elsewhere on the web. 在Stack Overflow和网络上的其他地方,有许多有关如何执行此操作的示例。 But they all will use one of two methods to draw the progress bar: 但是他们都将使用以下两种方法之一来绘制进度条:

  1. Construct a <path> element using one or more path arc ('A') commands 使用一个或多个路径弧('A')命令构造一个<path>元素
  2. Use a <circle> and use a dash pattern to draw part of a circle 使用<circle>并使用破折号图案绘制圆的一部分

The second one is the easier of the two to implement. 第二个是两者中较容易实现的。 Hopefully the example below is straightforward to follow: 希望以下示例可以直接执行:

 function setProgress(percent) { // Pointer to the <circle> element var progress = document.getElementById("progress"); // Get the length of the circumference of the circle var circumference = progress.r.baseVal.value * 2 * Math.PI; // How long our stroke dash has to be to cover <percent> of the circumference? var dashLength = percent * circumference / 100; // Set the "stroke-dasharray" property of the cicle progress.style.strokeDasharray = dashLength + ' ' + circumference; } setProgress(55); 
 svg { width: 300px } #progress { fill: lightgrey; stroke: orange; stroke-width: 10; } 
 <svg viewBox="-100 -100 200 200"> <circle id="progress" r="80" transform="rotate(-90)"/> </svg> 

i think you are approaching this problem in the wrong direction; 我认为您正在以错误的方向解决此问题;

you should search on the internet for a circular progress bar, you will find tons of answers. 您应该在互联网上搜索圆形进度栏,您会找到大量答案。

this is one link: https://kimmobrunfeldt.github.io/progressbar.js/ 这是一个链接: https : //kimmobrunfeldt.github.io/progressbar.js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM