Canvas 绘制渐进式圆头圆环图

移动端 native 效果图

当前项目大量 Web 需求,目前需要将 native 的效果同步到 Web,一顿百度各种图表库,F2·移动端可视化引擎 | AntV (antgroup.com)
Apache ECharts
一顿百度,发现添加圆头很麻烦,也没找到如何解决,最后只能用 canvas 自己来绘制了
Tab-环形图 (online-video-cutter.com).gif

具体实现代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>环形图</title>
    <style>
        canvas {
            border: 1px solid pink;
        }
    </style>
</head>

<body>
    <canvas width="400" height="500"></canvas>
    <script type="text/javascript">
        const canvas = document.querySelector('canvas');
        const ctx = canvas.getContext('2d');
        const size = Math.min(canvas.width, canvas.height)
        // 解决 canvas 绘制毛边问题
        const sizePX = `${size}px`
        canvas.style.width = sizePX;
        canvas.style.height = sizePX;
        canvas.height = size * window.devicePixelRatio;
        canvas.width = size * window.devicePixelRatio;
        ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
        // 2秒动画
        const duration = 2000; 
        let startTime = null;
        const lineWidth = 20;
        ctx.lineWidth = lineWidth;
        // 设置线条末端为圆头效果
        ctx.lineCap = 'round'; 
        const data = [{
            "value": 100,
            "color": "red"
        }, {
            "value": 50,
            "color": "blue"
        }, {
            "value": 15,
            "color": "green"
        }, {
            "value": 20,
            "color": "#ccc"
        }];
        const sum = 185;
        //设置角度的中间变量
        const x0 = size / 2;
        const y0 = x0;
        const radius = (size - lineWidth) / 2;
        console.log(`size=${size} y0=${y0} radius=${radius} lineWidth=${lineWidth}`)
        const mathPI = Math.PI / 180
        function draw(progress) {
            // 从-90度开始绘制
            let tempAngle = -90;
            const currAngle = 360 * progress
            for (let i = 0; i < data.length; i++) {
                //因为设置不同的颜色,所以每次绘制完起一个新状态
                ctx.beginPath();
                //当前扇形的角度
                const angle = data[i].value / sum * currAngle;
                ctx.strokeStyle = data[i].color;
                //开始从 tempAngle 绘制
                const startAngle = tempAngle * mathPI;
                //从tempAngle 绘制到 我们自己的angle区域
                const endAngle = (tempAngle + angle) * mathPI;
                //x0,y0 圆心坐标,radius:半径,startAngle:开始绘制的弧度,endAngle:结束绘制的弧度
                ctx.arc(x0, y0, radius, startAngle, endAngle);
                ctx.stroke();
                tempAngle += angle; //下一次绘制的起始角度
            }
        }
        function animate(timestamp) {
            if (!startTime) startTime = timestamp;
            let progress = (timestamp - startTime) / duration;
            if (progress > 1) progress = 1;
            draw(progress);
            if (progress < 1) {
                requestAnimationFrame(animate);
            }
        }
        requestAnimationFrame(animate);
    </script>
</body>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,854评论 2 59
  • 2018web前端最新面试题总结 一、Html/Css基础模块 基础部分 什么是HTML?答:​ HTML并不是...
    duans_阅读 4,680评论 3 27
  • 为了省去制作图片的麻烦,我就直接拿YYWebImage里面的图片了,我个人也是建议使用这个图片框架来做渐进式下载。...
    Raindew阅读 4,182评论 5 17
  • 介绍 最近出现的 React Native 再次让跨平台移动端开发这个话题火起来了,曾经大家以为在手机上可以像桌面...
    cosWriter阅读 2,331评论 0 12
  • 目录介绍 25.0.0.0 请说一下RecyclerView?adapter的作用是什么,几个方法是做什么用的?如...
    杨充211阅读 1,036评论 1 10