1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>SVG</title> </head> <body> <style> svg{ position: absolute; margin: auto; left: 0; right: 0; top: 0; bottom: 0; } .text{ text-anchor: middle; dominant-baseline: middle; } </style> <svg width="400" height="400"> <circle cx="200" cy="200" r="150" fill="none" stroke="grey" stroke-width="40"/> <circle class="progress" transform="rotate(-90,200,200)" cx="200" cy="200" r="150" fill="none" stroke="red" stroke-width="40" stroke-linecap="round" stroke-dasharray="0,10000" /> <text class="text" x="200" y="200" fill="red" font-size="50"></text> </svg> <script> var progressDom = document.querySelector(".progress"); var textDom = document.querySelector(".text"); function rotateCircle (persent){ var circleLength = Math.floor(2 * Math.PI * parseFloat(progressDom.getAttribute("r")) ); var value = persent * circleLength/total; var red = 255+parseInt((0-255)/100*persent); var green = 0 + parseInt((191-0)/100*persent); var blue = 0 + parseInt((255-0)/100*persent); progressDom.setAttribute("stroke-dasharray", value + ",10000"); progressDom.setAttribute("stroke", `rgb(${red},${green},${blue}`); textDom.innerHTML = persent+'%'; textDom.setAttribute("fill",`rgb(${red},${green},${blue})`); }
let num = 0, total = 60; setInterval(() => { num ++ ; if( num > total ){ num = 0 } rotateCircle(num) },100) </script> </body> </html>
|