circle.js 4.7 KB
// var $ = require('jquery');
// require(['jquery'], function($){
var Circle = {
    initialize: function() {
        this.render();
    },
    render: function() {
        var _this = this;
        $('span[data-circle]').each(function() {
            var c = new _circle($(this));
            c.initialize();
        });
    }
};

function _circle(selector) {
    // var opt = {
    //     countdown: $.trim(selector.attr('data-circle')) == 'countdown',
    //     min: +$.trim(selector.attr('data-min')),
    //     max: +$.trim(selector.attr('data-max')),
    //     init: +$.trim(selector.attr('data-init')),
    //     r: +$.trim(selector.attr('data-r')),
    //     lineWidth: +$.trim(selector.attr('data-lineWidth')),
    //     hColor: $.trim(selector.attr('data-hColor')),
    //     percent: $.trim(selector.attr('data-percent')),
    // };

    var opt = selector.data();
    console.log(opt);
    var defaults = {
        countdown: false,
        min: 0,
        r: 45, //半径
        linewidth: 6,
        color: '#e2e2e2',
        fontsize: 26,
        hcolor: '#69b8dc',
        max: 100,
        init: 0,
        current: 0,
        percent: 0
    };

    this.selector           = selector;
    this.settings           = $.extend(true, defaults, opt);
    this.settings.current   = this.settings.init;
    this.settings.countdown = this.settings.circle > 0 ? true: false;
}

_circle.prototype = {
    initialize: function() {
        var canvas = document.createElement('canvas');
        canvas.setAttribute('width', this.settings.r * 2);
        canvas.setAttribute('height', this.settings.r * 2);
        var ctx = canvas.getContext('2d');
        this.canvas = canvas;
        this.show();
        this.ctx = ctx;
        var _this = this;
        if (this.settings.countdown) {
            this.timer = setInterval(function() {
                _this.countdown();
            }, 1000);

        }
        this.bindEvents();
        this.render();
    },
    bindEvents: function(){
        var _this = this;
        if(this.settings.input){
            $(_this.settings.input).on('input', function(e){
                var number = $.trim($(this).val());
                    number = number.replace(/^\.*$/, '');
                    number = number.replace(/[^0-9\.]/g, '');
                    number = number.replace(/(([1-9]\d*(\.\d{0,2})?)|0(\.\d{0,2})?).*/, "$1");
                    if(+number >  _this.settings.max){
                        number = _this.settings.max;
                    }
                    $(this).val(number);
                    _this.settings.current = +number;
                    _this.render();
            });
        }
    },
    show: function() {
        this.selector.append(this.canvas);
    },
    render: function() {
        this.update();
    },
    countdown: function() {
        if (this.settings.current <= 0) {
            clearInterval(this.timer);
            this.selector.trigger('circleFinish');
            return;
        }
        this.settings.current -= 1;
        this.render();
    },
    update: function() {
        var ctx = this.ctx;
        ctx.clearRect(0, 0, this.settings.r * 2, this.settings.r * 2);
        ctx.beginPath();
        ctx.strokeStyle = this.settings.color;
        ctx.lineWidth = this.settings.linewidth;
        ctx.arc(this.settings.r, this.settings.r, this.settings.r - this.settings.linewidth, 0, Math.PI * 2, true);
        ctx.stroke();
        // 设置字体
        ctx.font = "normal "+ this.settings.fontsize+"px Arial";
        // 设置对齐方式
        ctx.textAlign = "center";
        // 设置填充颜色
        ctx.fillStyle = this.settings.hcolor;
        var text = Math.floor(this.settings.current * 100 / this.settings.max);
        var y = this.settings.r + 11;
        if(this.settings.percent > 0){
        	text += '%';
        	y = this.settings.r + 7;
        }
        // 设置字体内容,以及在画布上的位置
        ctx.fillText(text, this.settings.r, y);
        // 绘制空心字
        // ctx.strokeText(this.settings.init, this.settings.r, this.settings.r + 8);
        var ctx = this.ctx;
        ctx.beginPath();
        ctx.strokeStyle = this.settings.hcolor;
        var end = -this.settings.current / this.settings.max * Math.PI * 2 - Math.PI / 180 * 90;
        ctx.arc(this.settings.r, this.settings.r, this.settings.r - this.settings.linewidth, -Math.PI / 180 * 90, end, true);
        ctx.stroke();
    }
};

// component
if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
    // AMD. Register as an anonymous module.
    define(function() {
        return Circle;
    });
} else if (typeof module !== 'undefined' && module.exports) {
    module.exports = Circle;
} else {
    window.Circle = Circle;
}