资讯专栏INFORMATION COLUMN

基于canvas原生js的圆形进度环(progressBar)

mushang / 2259人阅读

摘要:代码如下创建画布对象开始绘画微软雅黑效果如图使用说明地址

代码如下:
(function(w) {
                function Cycle(options) {
                    this.id = options.id;
                    this.width = options.width;
                    this.height = options.height;
                    this.percent = options.percent;
                    this.border = options.border;
                    this.bgColor = options.bgColor;
                    this.barColor = options.barColor;
                    this.textColor = options.textColor;
                };
                Cycle.prototype = {
                    contructor: Cycle,
                    init: function() {
                        //创建画布对象
                        var html = "";
                        document.getElementById(this.id).innerHTML = html;
                        var time = setInterval(function() {
                            this.percent++;
                            if(this.percent >= 100) {
                                clearInterval(time);
                            }
                            this.setOptions();
                        }.bind(this), 1000);
                        this.setOptions()
                    },
                    setOptions: function() {
                        var degree = this.percent;
                        var canvas = document.getElementById("canvas_" + this.id);
                        var context = canvas.getContext("2d");
                        context.clearRect(0, 0, this.width, this.height);
                        //开始绘画
                        context.beginPath();
                        context.lineWidth = this.border;
                        context.strokeStyle = this.bgColor;
                        context.arc(this.width / 2, this.height / 2, (this.width / 2 - this.border / 2), 0, 2 * Math.PI);
                        context.stroke();
                        var deg = degree * 3.6 / 180 * Math.PI
                        context.beginPath();
                        context.lineWidth = this.border;
                        context.strokeStyle = this.barColor;
                        context.arc(this.width / 2, this.height / 2, (this.width / 2 - this.border / 2), 0 - Math.PI / 2, deg - Math.PI / 2);
                        context.stroke();
                        context.beginPath();
                        context.fillStyle = this.textColor;
                        context.font = "18px 微软雅黑";
                        var text = degree + "%";
                        var textWidth = context.measureText(text).width;
                        context.fillText(text, this.width / 2 - textWidth / 2, this.height / 2 + 9);
                    }
                }
                w.Cycle=Cycle;
            }(window))
效果如图:


使用说明:

var opts1 = {
            id: "cycle1",
            width: "300",
            height: "300",
            percent: "20",
            border: "30",
            bgColor: "green",
            barColor: "yellow",
            textColor: "pink"
        };
        var opts2 = {
            id: "cycle2",
            width: "400",
            height: "400",
            percent: "60",
            border: "30",
            bgColor: "gray",
            barColor: "red",
            textColor: "pink"
        };
        new Cycle(opts1).init();
        new Cycle(opts2).init();
github地址:

https://github.com/jeromehan/...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82760.html

相关文章

  • css3实现圆形进度

    摘要:在开发微信小程序的时候,遇到圆形进度条的需求。但使用和实现进度条就很容易的避免了这方面的问题。如下图最下面的圆形是进度条的背景,在上面有和两个长方形,用来覆盖不要显示的进度条。在两个长方形的里面分别有一个半圆形用来显示进度。 在开发微信小程序的时候,遇到圆形进度条的需求。使用canvas绘图比较麻烦: 1、为了实现在不同屏幕上面的适配,必须动态的计算进度条的大小; 2、在小程序...

    leanote 评论0 收藏0
  • css3实现圆形进度

    摘要:在开发微信小程序的时候,遇到圆形进度条的需求。但使用和实现进度条就很容易的避免了这方面的问题。如下图最下面的圆形是进度条的背景,在上面有和两个长方形,用来覆盖不要显示的进度条。在两个长方形的里面分别有一个半圆形用来显示进度。 在开发微信小程序的时候,遇到圆形进度条的需求。使用canvas绘图比较麻烦: 1、为了实现在不同屏幕上面的适配,必须动态的计算进度条的大小; 2、在小程序...

    enali 评论0 收藏0

发表评论

0条评论

mushang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<