1. 程式人生 > >66.canvas 環形加載動畫

66.canvas 環形加載動畫

animation ogre javascrip return svg pro cal utf device

技術分享圖片

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

 

66.canvas 環形加載動畫