js简单插件(圆环)

html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圆环图封装</title>
</head>
<body>
<div id="container"></div>
</body>
</html>

js代码如下

<script src="jrChart.js"></script>
<script>
    var obj = {
        id:"container",
        width:1000,
        height:500,
        data:[
            {
                num:63,
                color:"yellow",
                angle:60
            }
        ],
        circleX:250,
        circleY:250,
        radius:100,//半径
        startType:4,
        fontColor:"red",
        bottomColor:"red"

    };

    drawCirque(obj);
</script>

引入名叫jrChart的js文件

function drawCirque(obj) {
    var width = obj.width;
    var height = obj.height;
    var id = obj.id;
    var data = obj.data;
    var circleX = obj.circleX;
    var circleY = obj.circleY;
    var radius = obj.radius;
    var startType = obj.startType;
    var fontColor = obj.fontColor;
    var bottomColor = obj.bottomColor;
    var container = document.getElementById(id);
    container.style.width = width + "px";
    container.style.height = height + "px";
    var canvas = document.createElement("canvas");
    canvas.width = width;
    canvas.height = height;
    canvas.style.border = "1px solid red";
    var cvs = canvas.getContext("2d");
    container.appendChild(canvas);
    var interval;
    var start;
    var end = 0;
    var increment = 2 * Math.PI / 100;
    var i = 0;
    if (startType === 1) {
        start = -0.5 * Math.PI;
        interval = setInterval(function () {
            i++;
            end = start + increment * i;
            cvs.clearRect(0, 0, 1000, 500);
            cvs.beginPath();
            cvs.strokeStyle = bottomColor;
            cvs.lineWidth = 10;
            cvs.lineCap = "round";
            cvs.arc(circleX, circleY, radius, 0, Math.PI * 2);
            cvs.closePath();
            cvs.stroke();
            //
            cvs.strokeStyle = data[0].color;
            cvs.beginPath();
            cvs.arc(circleX, circleY, radius, start, end);
            cvs.stroke();
            var text = i + "%";
            cvs.font = "30px 宋体";
            cvs.fillStyle = fontColor;
            cvs.fillText(text, 230, 240);
            if (end >= data[0].num * increment - 0.5 * Math.PI) {
                clearInterval(interval);
            }
        }, 10);

    } else if (startType === 2) {
        interval = setInterval(function () {
            start = 0;
            i++;
            end = start + increment * i;
            cvs.clearRect(0, 0, 1000, 500);
            cvs.beginPath();
            cvs.strokeStyle = bottomColor;
            cvs.lineWidth = 10;
            cvs.arc(circleX, circleY, radius, 0, Math.PI * 2);
            cvs.closePath();
            cvs.stroke();
            //
            cvs.strokeStyle = data[0].color;
            cvs.beginPath();
            cvs.arc(circleX, circleY, radius, start, end);
            cvs.stroke();
            var text = i + "%";
            cvs.font = "30px 宋体";
            cvs.fillStyle = fontColor;
            cvs.fillText(text, 230, 240);
            if (end >= data[0].num * increment ) {
                clearInterval(interval);
            }
        }, 10);
    }
    else if (startType === 3) {
        start = 0.5 * Math.PI;
        interval = setInterval(function () {
            i++;
            end = start + increment * i;
            cvs.clearRect(0, 0, 1000, 500);
            cvs.beginPath();
            cvs.strokeStyle = bottomColor;
            cvs.lineWidth = 10;
            cvs.arc(circleX, circleY, radius, 0, Math.PI * 2);
            cvs.closePath();
            cvs.stroke();

            cvs.strokeStyle = data[0].color;
            cvs.beginPath();
            cvs.arc(circleX, circleY, radius, start, end);
            cvs.stroke();
            var text = i + "%";
            cvs.font = "30px 宋体";
            cvs.fillStyle = fontColor;
            cvs.fillText(text, 230, 240);
            if (end >= data[0].num * increment + 0.5 * Math.PI) {
                clearInterval(interval);
            }
        }, 10);

结果如下
在这里插入图片描述

注:插件是有过渡动画效果的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值