canvas实现的loading效果

<!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>
</head> 
<body>
    <canvas id="myCanvas" width="600" height="600" style="border:1px solid red"></canvas>
    <script>
        var ctx = myCanvas.getContext('2d');
        var deg = Math.PI / 180;
        var index = 0; 
        fram() 
        function fram() {
            index++;
            if (index <= 360) {
                requestAnimationFrame(fram)
            }
            var txt = Math.round(index / 360 * 100);
            ctx.beginPath();
            ctx.clearRect(0, 0, 600, 600);
            ctx.arc(300, 300, 200, 0, index * deg);
            ctx.strokeStyle = "pink";
            ctx.lineWidth = 10;
            ctx.stroke();
            ctx.beginPath();
            ctx.lineWidth = 1;
            ctx.strokeText(txt + "%", 300, 300);
            ctx.font = '30px normal';
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";
            ctx.closePath();
        }
    </script>
</body>

</html>

你可能感兴趣的:(canvas)