原生Canvas实现柱状体

<!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>Document</title>
    <style>
        html,
        body {
    
    
            margin: 0;
            padding: 0;
        }
        
        main {
    
    
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #e0e3ed;
        }
        
        canvas {
    
    
            border-radius: 0;
            box-shadow: 0 0 18px rgba(0, 0, 0, 0.2);
        }
    </style>
</head>

<body onload="draw()">
    <main>
        <canvas width="600" height="300" id="bar-chart"></canvas>
    </main>
    <script type="text/javascript">
        const months = ["一月", '二月', "三月", '四月', '五月', '六月', "七月", '八月', '九月', "十一月", '十二月'];

        function draw() {
    
    
            var canvas = document.getElementById("bar-chart");
            var ctx = canvas.getContext("2d");
            // 获取canvas宽和高
            let cWidth = canvas.width;
            let cHeight = canvas.height;
            ctx.fillStyle = "#f8fcff";
            ctx.fillRect(0, 0, cWidth, cHeight);
            // 计算9个柱子的位置
            // canvas 内边距
            let padding = 40;

            // 柱子的高度
            let width = 8;


            // 柱子最大高度
            let maxHeight = (cHeight - padding * 2) / 2;
            // 柱子最小高度
            let minHeight = maxHeight / 2;
            // 柱子间距
            let barGap = (cWidth - 2 * padding - 9 * width) / 8;

            // 柱子圆角
            let radius = 5;
            // y坐标起始于canvas中间
            let y = cHeight / 2;
            for (let i = 0; i < 9; i++) {
    
    
                // 柱子上半部分高度,随机产生[minHeight,maxHeight]区间的数字
                let height1 = Math.floor(Math.random() * (maxHeight - minHeight + 1) + minHeight);
                // 同样的下半部分高度
                let height2 = Math.floor(Math.random() * (maxHeight - minHeight + 1) + minHeight);
                // 计算每个柱子起始x坐标的位置
                let x = padding + (barGap + width) * i;
                // 画柱状图
                drawBar(ctx, x, y, width, height1, height2, radius);
                // 画月份
                ctx.fillStyle = "#747d8c";
                ctx.textAlign = "center";
                // 设置文字粗细
                ctx.font = "300 12px sans-serif";
                // 在柱子中间画文字
                ctx.fillText(months[i], x + width / 2, y + maxHeight + 20)

            }




        }

        function drawBar(ctx, x, y, width, height1, height2, radius) {
    
    
            ctx.beginPath();
            // 画上半部分,四条边,两个角
            ctx.moveTo(x, y);
            ctx.lineTo(x, y - height1 + radius);
            ctx.arcTo(x, y - height1, x + radius, y - height1, radius);
            ctx.lineTo(x + width - radius, y - height1);
            ctx.arcTo(x + width, y - height1, x + width, y - height1 + radius, radius);
            ctx.lineTo(x + width, y);
            ctx.lineTo(x, y);
            ctx.fillStyle = "#341F97";
            ctx.fill();

            // 下半部分
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(x, y + height2 - radius);
            ctx.arcTo(x, y + height2, x + radius, y + height2, radius);
            ctx.lineTo(x + width - radius, y + height2);
            ctx.arcTo(x + width, y + height2, x + width, y + height2 - radius, radius);
            ctx.lineTo(x + width, y);
            ctx.lineTo(x, y);
            ctx.fillStyle = "#54A0FF";
            ctx.fill();

            // ctx.stroke();
        }
    </script>
</body>

</html>

效果图:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45932157/article/details/135228968