原生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>

效果图:

在这里插入图片描述

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答你的问题。首先,Vue3是一种流行的JavaScript框架,用于构建现代化的Web应用程序。而Canvas是HTML5中的一个元素,可以用于动态绘制图形和动画。 要实现抖音头像抠图效果,可以通过以下步骤: 1. 在Vue3中创建一个Canvas元素,并获取它的上下文。 ```html <template> <canvas ref="canvas"></canvas> </template> <script> export default { mounted() { const canvas = this.$refs.canvas const ctx = canvas.getContext('2d') // 设置Canvas的宽度和高度 canvas.width = 400 canvas.height = 400 } } </script> ``` 2. 加载要抠图的头像图片,并在Canvas上绘制它。 ```javascript const img = new Image() img.src = 'https://example.com/avatar.png' img.onload = () => { // 在Canvas上绘制头像图片 ctx.drawImage(img, 0, 0, canvas.width, canvas.height) } ``` 3. 使用Canvas的globalCompositeOperation属性实现抠图效果。 ```javascript // 将globalCompositeOperation设置为destination-in ctx.globalCompositeOperation = 'destination-in' // 绘制圆形路径 ctx.beginPath() ctx.arc(canvas.width / 2, canvas.height / 2, canvas.width / 2, 0, 2 * Math.PI) ctx.closePath() // 剪切出圆形区域 ctx.clip() // 再次绘制头像图片 ctx.drawImage(img, 0, 0, canvas.width, canvas.height) ``` 4. 最后,将Canvas的数据导出为Base64编码的图片,或者直接将Canvas插入到页面中。 ```javascript // 将Canvas导出为Base64编码的图片 const dataURL = canvas.toDataURL('image/png') console.log(dataURL) // 或者直接将Canvas插入到页面中 document.body.appendChild(canvas) ``` 希望这些步骤能够帮助你实现抖音头像抠图效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值