66.环形加载动画(canvas/svg)
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 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> <style> body { margin: 0; padding: 0; text-align: center; } </style> </head> <body> <canvas id= "canvas" ></canvas> <script> let canvasWidth = 200; let canvasHeight = 200; let centerX = canvasWidth / 2; let centerY = canvasHeight / 2; let r = canvasWidth / 2 - 10; let canvas = document.getElementById( 'canvas' ); canvas.width = canvasWidth; canvas.height = canvasHeight; let ctx = canvas.getContext( '2d' ); let sRad = Math.PI * 2; let step = 0; function loading() { ctx.beginPath(); ctx.clearRect(0, 0, canvasWidth, canvasHeight); ctx.strokeStyle = "orange" ; ctx.font = "26px Arial" ; ctx.textAlign = "center" ; ctx.fillStyle = "orange" ; ctx.lineWidth = 10; let text = parseInt(parseFloat(step) * 100) + '%' ctx.fillText(text, centerX, centerY); ctx.arc(centerX, centerY, r, 0, sRad * step, false ); ctx.stroke(); } function render() { if (step > 1) { cancelAnimationFrame(render) return } requestAnimationFrame(render); step += 0.01; loading() } render() </script> </body> </html> |
//svg
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>SVG</title> <style> body { background: #1a1a1a; } body>div { text-align: center; } .ring_circle { stroke-dasharray: 10 20; transition: stroke-dasharray 0.35s; } </style> </head> <body> <div> <svg class="progress-ring" width="120px" height="120px"> <circle class="ring_circle" stroke="white" stroke-width="4" fill="transparent" r="52" cx="60" cy="60" /> </svg> </div> <script> let circle = document.querySelector('.ring_circle'); let radius = circle.r.baseVal.value; var circrumference = radius * Math.PI * 2; circle.style.strokeDasharray = circrumference + " " + circrumference; circle.style.strokeDashoffset = circrumference; var i = 1; var ite = null; function setProgress(percent) { const offset = circrumference - percent / 100 * circrumference; circle.style.strokeDashoffset = offset; } if (ite) clearInterval(ite); ite = setInterval(() => { i += 2; if (i > 100){ i = 100; clearInterval(ite) } setProgress(i) }, 16) </script> </body> </html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步