Making Circular Progress bar with html5 svg
I have made this circular graph with the canvas.
But I am finding it too dificult from the SVG. Can anyone tell me how to do it from svg. 1 SolutionSolution The arc command of canvas is pretty simple. But in SVG you have to use path to draw circular progress and it is a bit complex.
The idea behind drawing the progress is that you have to draw a path that passes from the circumference of a circle which starts from specified angle and ends at specified angle. There is a mathematical formula for finding point in the circumference of circle which you can use to create path in svg
To simplify things, i have found a function that calculates the path. You just have to write this:
The progress function takes the following parameters:
This draws a full progress.If you want to change the progress change the end angle i.e decrease it.This is just the idea behind drawing circular path. |
JSFiddle code
<svg id="svg" width="1000" height="500">
<circle cx="200" cy="200" r="150" stroke="#ccc" stroke-width="1" fill="none" />
<circle cx="200" cy="200" r="135" stroke="#ccc" stroke-width="1" fill="#777" />
<text x="180" y="200" fill="white">Power</text>
<path id="arc1" fill="none" stroke="#0f0" stroke-width="30" />
</svg>
var svg = document.getElementById("svg");
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 arcSweep = 1;
var d = [
"M", start.x, start.y,
"A", radius, radius, 0, arcSweep, 0, end.x, end.y
].join(" ");
return d;
}
document.getElementById("arc1").setAttribute("d", describeArc(200, 200, 120, -150, 150));
출처 - http://www.ggkf.com/html5/making-circular-progress-bar-with-html5-svg
'Development > HTML5' 카테고리의 다른 글
html5 - WebSocket javascript 설정 (0) | 2014.03.26 |
---|