canvas画一个渐变色的环形进度环

11 篇文章 0 订阅
2 篇文章 0 订阅

» 介绍

之前用canvas画了一个环形进度条,想要实现从0到100颜色渐变效果,先看效果:

在这里插入图片描述
预览地址:https://code.juejin.cn/pen/7355311802202193958


» 方法一:圆锥渐变

createConicGradient(startAngle, x, y)

startAngle:起点角度
x,y:圆锥中心点坐标

注意:createConicGradient只支持chrome99以上
在这里插入图片描述

» 代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>canvas环形进度条</title>
    <style>
      body {
        margin: 30px 0;
        text-align: center;
      }

      #canvas {
        background: #f6f6f6;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="300" height="300">您的浏览器不支持 HTML5 canvas 标签。</canvas>
    <script>
      let c = document.getElementById('canvas')
      let ctx = c.getContext('2d')
      let precent = 80,
        num = 0

        const gradient = ctx.createConicGradient(-90, 150, 150);
	    gradient.addColorStop(0, "red");
		gradient.addColorStop(1, "blue");

      function options(color, x, y, radius, start, end, order) {
        ctx.save()
        ctx.beginPath()
        ctx.lineCap = 'round'
        ctx.lineWidth = 8
        ctx.strokeStyle = color

        ctx.arc(x, y, radius, start, end, order) //x坐标,y坐标,半径,起始角,结束角,顺时针/逆时针
        ctx.stroke()
        ctx.closePath()
        ctx.restore()
      }

      //绘制文字
      function drawText(num) {
        ctx.save()
        ctx.fillStyle = '#2ba0fb'
        ctx.font = '40px Helvetica'
        ctx.textAlign = 'center'
        ctx.fillText(num + '%', 150, 160)
        ctx.restore()
      }

      //动画
      (function draw() {
        num += 1
        if (num < precent) window.requestAnimationFrame(draw)
        else num = precent
        ctx.clearRect(0, 0, 300, 300)
        options('#e5f1fa', 150, 150, 100, 0, 2 * Math.PI) //绘制轨道
        options(gradient, 150, 150, 100, -Math.PI / 2, -Math.PI / 2 + ((2 * num) / 100) * Math.PI) //绘制进度环
        drawText(num)
      })()
    </script>
  </body>
</html>

» 方法二:圆环分割

原理:把圆环分成n份,计算初始颜色到终止颜色中间n份颜色的值,每一份圆环一个颜色,从而达到渐变效果。

在这里插入图片描述


» 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas环形进度条</title>
    <style>
        body {
            margin: 30px 0;
            text-align: center;
        }

        #canvas {
            background: #f6f6f6;
        }
    </style>
</head>
<body>
<canvas id="canvas" width="300" height="300">您的浏览器不支持 HTML5 canvas 标签。</canvas>
<script>
    let c = document.getElementById('canvas')
    let ctx = c.getContext('2d')
    let precent = 80, num = 0
    let startColor = '#29d6b5', endColor = '#3396c7', colorArr = [];

    ctx.fillStyle="#FF0000";

    //计算渐变色数组
    (function getColorArr() {
        let startRgb = hexToRgb(startColor)
        let endRgb = hexToRgb(endColor)

        let stepR = (endRgb[0] - startRgb[0]) / precent
        let stepG = (endRgb[1] - startRgb[1]) / precent
        let stepB = (endRgb[2] - startRgb[2]) / precent

        for (let i = 0; i <= precent; i++) {
            let hex = rgbToHex(startRgb[0] + stepR * i, startRgb[1] + stepG * i, startRgb[2] + stepB * i)
            hex = hex.length == 4 ? ('00' + hex) : hex.length == 5 ? ('0' + hex) : hex
            colorArr.push('#' + hex)
        }
    }())

    function hexToRgb(hex) {
        if (hex.length == 4)
            return [parseInt('0x' + hex.slice(1, 2).repeat(2)), parseInt('0x' + hex.slice(2, 3).repeat(2)), parseInt('0x' + hex.slice(3, 4).repeat(2))]
        else if (hex.length == 7)
            return [parseInt('0x' + hex.slice(1, 3)), parseInt('0x' + hex.slice(3, 5)), parseInt('0x' + hex.slice(5, 7))]
        else
            alert('颜色错误')
    }

    function rgbToHex(r, g, b) {
        return ((r << 16) | (g << 8) | b).toString(16)
    }

    //绘制轨道
    drawTrack()
    function drawTrack() {
        ctx.save();
        ctx.beginPath()
        ctx.lineCap = 'round'
        ctx.lineWidth = 8
        ctx.strokeStyle = '#e5f1fa'
        ctx.arc(150, 150, 100, 0, 2 * Math.PI)
        ctx.stroke()
        ctx.closePath();
        ctx.restore();
    }

    //绘制进度环
    function drawProgress(num) {
        ctx.save();
        ctx.beginPath()
        ctx.lineCap = 'round'
        ctx.lineWidth = 8
        // ctx.strokeStyle = '#2ba0fb'
        ctx.strokeStyle = colorArr[num]
        // context.arc(x,y,r,sAngle,eAngle,counterclockwise);   //x坐标,y坐标,半径,起始角,结束角,顺时针/逆时针
        ctx.arc(150, 150, 100, -Math.PI / 2 + 2 * (num - 1) / 100 * Math.PI, -Math.PI / 2 + 2 * num / 100 * Math.PI)
        ctx.stroke()
        ctx.closePath();
        ctx.restore();
    }

    //绘制文字
    function drawText(num) {
        ctx.save();
        ctx.fillStyle = '#3396c7'

        ctx.font = '40px Helvetica'
        ctx.textAlign = 'center';
        ctx.fillText(num + '%', 150, 160)
        ctx.restore();
    }

    //动画
    (function draw() {
        ctx.clearRect(85, 85, 130, 130)
        drawProgress(num)
        drawText(num)
        num += 1
        if (num <= precent)
            window.requestAnimationFrame(draw)
        else
            num = precent
    }())


</script>
</body>
</html>

相关推荐:

🚀svg画一个环形进度条:https://blog.csdn.net/qq_34241004/article/details/120550831

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下使用 Canvas 绘制渐变色立方体的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas Gradient Cube</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); // 绘制立方体的六个面 drawFace(100, 100, 100, 200, 200, 200, "#ff0000", "#00ff00", "#0000ff"); // 前面 drawFace(0, 100, 100, 100, 200, 200, "#0000ff", "#00ff00", "#ff0000"); // 左面 drawFace(200, 100, 100, 300, 200, 200, "#00ff00", "#0000ff", "#ff0000"); // 右面 drawFace(100, 0, 100, 200, 100, 200, "#ff0000", "#0000ff", "#00ff00"); // 上面 drawFace(100, 200, 100, 200, 300, 200, "#ff0000", "#00ff00", "#0000ff"); // 下面 drawFace(100, 100, 0, 200, 200, 100, "#00ff00", "#ff0000", "#0000ff"); // 后面 // 绘制一个立方体面 function drawFace(x1, y1, z1, x2, y2, z2, color1, color2, color3) { // 计算面的中心点 var centerX = (x1 + x2) / 2; var centerY = (y1 + y2) / 2; var centerZ = (z1 + z2) / 2; // 开始绘制面 context.beginPath(); // 绘制面的左下角点 context.moveTo(projectX(x1, z1), projectY(y1, z1)); // 绘制面的其它三个点 context.lineTo(projectX(x2, z1), projectY(y1, z1)); context.lineTo(projectX(x2, z2), projectY(y2, z2)); context.lineTo(projectX(x1, z2), projectY(y2, z2)); // 使用渐变色填充面 var gradient = context.createLinearGradient(0, 0, 0, canvas.height); gradient.addColorStop(0, color1); gradient.addColorStop(0.5, color2); gradient.addColorStop(1, color3); context.fillStyle = gradient; context.fill(); // 结束绘制面 context.closePath(); } // 将 3D 坐标投影到 2D 平面上 function projectX(x, z) { return canvas.width / 2 + (x - canvas.width / 2) * (200 / (200 - z)); } function projectY(y, z) { return canvas.height / 2 - (y - canvas.height / 2) * (200 / (200 - z)); } </script> </body> </html> ``` 上述代码使用了三个颜色值,可以根据需要进行调整。同时,也可以根据需要修改立方体的大小和位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qsya

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值