canvas环型比例图

dom部分

 <div class='process-container circle-progress'>
                <canvas id="canvas" class="canvas" width="100%" height="100%"></canvas>
            </div>

js

 DrawRound(percent) {
            const canvas = document.getElementById("canvas")
            const ctx = canvas.getContext("2d");
            const box=document.getElementsByClassName("process-container")[0]
            canvas.width =box.clientHeight
            canvas.height=box.clientHeight
            const cx = canvas.width / 2, cy = canvas.height / 2, r = (canvas.width / 4) + 16
            var img = new Image();     //创建图片对象
            import("./images/Union.png").then(res => {
                img.src = res.default
            })     //设置绘制图像路径
            img.onload = function () {
                ctx.drawImage(img, cx / 2-10 , cy / 2 -10 , cx + 20, cy + 20)
            }
            //外边框
            var img1 = new Image();     //创建图片对象
            import("./images/outline.png").then(res => {
                img1.src = res.default
            })     //设置绘制图像路径
            img1.onload = function () {
                ctx.drawImage(img1, -2, -2, cx * 2, cy * 2)
            }
            //环形图的进度条
            ctx.beginPath();
            ctx.arc(cx , cy , r, Math.PI / 2, Math.PI / 2 - percent * (Math.PI * 2), false);
            ctx.lineWidth = 5;
            ctx.lineCap = "round";
            ctx.strokeStyle = "#06FFA5";
            ctx.stroke();

            ctx.beginPath();
            ctx.arc(cx + 2, cy, r, Math.PI / 2 - percent * (Math.PI * 2) + 0.3, Math.PI / 2 - 0.3, false);
            ctx.lineWidth = 5;
            ctx.lineCap = "round";
            ctx.strokeStyle = "#f0f6f9";
            ctx.stroke();

            ctx.font="12px Open Sans-SemiBold, Open Sans"
            ctx.fillStyle = "#ffffff";
            ctx.fillText("设备总数",cx-25,cy-15);
        },
        //清空画布
        clearCanvas(){
            const theCanvas = document.getElementById("canvas")
            const ctx = canvas.getContext("2d");
            var w = theCanvas.width;
            var h = theCanvas.height;
            ctx.clearRect(0, 0, w, h);
        },
//比如在组件挂载时启动定时器
let count=0
setInterval(()=>{
count++
this.clearCanvas()
this.DrawRound(0.1*count)
},3000)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值