vue 使用canvas画类似的百分比球形

<template>
    <div>
        <canvas id="myCanvas" :width="width" :height="height"></canvas>
    </div>
</template>

<script>
export default {
    data () {
        return {
            ele: null  
        }
    },
    props: {
        width: {
            type: String,
            default: '200'
        },
        height: {
            type: String,
            default: '200'
        },
        src: {
            type: String,
            default: require('./dashback.png')
        },
        percent: {
            type: String,
            default: 75
        },
        padwidth: {
            default: 0.75           
        },
        borderwidth: {
            default: 20           
        },       
             
    },
    methods: {
        PI (deg)
        {
            return (deg/100)*2*Math.PI;
        },
        draw () {
            let can = document.getElementById("myCanvas")
            let cxt = can.getContext("2d")
            cxt.clearRect(0,0,this.width,this.height)
            let img = new Image()
            img.src = this.src
            img.onload = () => {
                cxt.drawImage(img,0,0,this.width,this.height);

                /* 文字 */
                cxt.fillStyle="#ffffff";
                cxt.font = "47px Arial"
                cxt.fillText(this.percent,(parseInt(this.width)-70)/2,(parseInt(this.height)+29)/2)

                cxt.font = "18px Arial"
                cxt.fillText("",(parseInt(this.width)+36)/2,(parseInt(this.height)+29)/2)

                cxt.beginPath();
                cxt.arc(parseInt(this.width)/2, parseInt(this.height)/2, this.padwidth*this.width/2, this.PI(100), this.PI(0), true)
                cxt.lineWidth = this.borderwidth*0.6;
                cxt.strokeStyle = 'rgba(255,255,255,.35)';
                cxt.shadowOffsetX = 0
                cxt.shadowOffsetY = 0
                cxt.shadowColor = 'rgb(100,100,100)'
                cxt.shadowBlur = 10
                cxt.stroke();      

                cxt.beginPath();
                cxt.arc(parseInt(this.width)/2, parseInt(this.height)/2, this.padwidth*this.width/2, this.PI(this.percent), this.PI(0), true);
                cxt.lineWidth = this.borderwidth;
                cxt.strokeStyle = 'white';
                cxt.shadowOffsetX = 0
                cxt.shadowOffsetY = 0
                cxt.shadowColor = 'rgb(100,100,100)'
                cxt.shadowBlur = 10
                cxt.stroke();  
            }            
        }        
    },
    mounted: function () {          
        this.draw()        
    },
    created: function () {

    },
    watch: {
        'percent': function(newVal){
            this.draw()
        }
    }
}
</script>
使用
<dash-board percent="45"></dash-board>  
效果

原图

转载于:https://www.cnblogs.com/hzsu/p/10910455.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值