66.环形加载动画(canvas/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>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>

 

 

转载于:https://www.cnblogs.com/famLiu/p/10878093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值