Dashboard canvas

Time:2021-10-21

The effect is as follows:
Dashboard canvas

The source code is as follows. You can preview it by directly copying and saving it as an HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  
</head>
<body>
<canvas id="canvas" width="300" height="300">
    <p>Sorry, your browser does not support canvas</p>
 </canvas>

 <script>

    
    
    /**
     *Generate circular progress bar
     */
    function toCanvas(id, color, progress) {
        //Canvas progress bar
        var canvas = document.getElementById(id),
            ctx = canvas.getContext("2d"),
            Percent = progress, // final percentage
            Circlex = canvas.width/2, // center X coordinate
            Circley = canvas.height/2, // center y coordinate
            Radius = 80, // ring radius
            Cradius = 75, // Canvas radius
            Linewidth = 6, // the width of the circular line
            fontSize = 20; // font size
        //Dots at both ends
        // function smallcircle1(cx, cy, r) {
        //     ctx.beginPath();
        //     //ctx.moveTo(cx + r, cy);
        //     ctx.lineWidth = 1;
        //     ctx.fillStyle = '#06a8f3';
        //     ctx.arc(cx, cy, r, 0, Math.PI * 2);
        //     ctx.fill();
        // }
    
        // function smallcircle2(cx, cy, r) {
        //     ctx.beginPath();
        //     //ctx.moveTo(cx + r, cy);
        //     ctx.lineWidth = 1;
        //     ctx.fillStyle = '#fff';
        //     ctx.arc(cx, cy, r, 0, Math.PI * 2);
        //     ctx.fill();
        // }
    
        //Draw a circle
        function circle(cx, cy, r, sAngle, eAngle, bColor = '#eee') {
            ctx.beginPath();
            //ctx.moveTo(cx + r, cy);
            ctx.lineWidth = lineWidth;
            ctx.strokeStyle = bColor;
            // ctx.lineCap = 'round';
            // ctx.arc(cx, cy, r, Math.PI * 1, Math.PI * 2);
            ctx.arc(cx, cy, r, sAngle, eAngle);
            ctx.stroke();
        }
    
        //Draw an arc
        function sector(cx, cy, r, startAngle, endAngle, _lineWidth) {
            ctx.beginPath();
            //ctx.moveTo(cx, cy + r); //  Start at the bottom of the circle
            ctx.lineWidth = _lineWidth;
    
            //Gradient - customizable
            //                      var linGrad = ctx.createLinearGradient(
            //                          circleX-radius-lineWidth, circleY, circleX+radius+lineWidth, circleY
            //                      );
            //                      linGrad.addColorStop(0.0, '#06a8f3');
            //                      //linGrad.addColorStop(0.5, '#9bc4eb');
            //                      linGrad.addColorStop(1.0, '#00f8bb');
            //                      ctx.strokeStyle = linGrad;
            //Progress bar color
            ctx.strokeStyle = color;
            //Style at both ends of the arc
            // ctx.lineCap = 'round';
    
            //Circular arc
            ctx.arc(
                cx, cy, r,
                startAngle,
                (Math.PI * (1.5 - ((1.5 / 100) * process))),
                true
            );
            ctx.stroke();
        }
    
        //Refresh
        function loading() {
            if (process >= percent) {
                clearInterval(circleLoading);
            }
    
            //Clear canvas content
            ctx.clearRect(0, 0, circleX * 2, circleY * 2);
    
            //Middle word
            ctx.font = fontSize + 'px April';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillStyle = '#999';
            ctx.fillText(parseFloat(process).toFixed(0) + '%', circleX, circleY - 10);
            
            ctx.font = (fontSize - 4) + 'px April';
            ctx.fillStyle = '##a7a7a7';
            Ctx.filltext ('safe ', circlex, circley + 20);
            
            
            circle(circleX, circleY, radius + 14, 0, Math.PI * 2, '#e1f7f3');

            //Round
            circle(circleX, circleY, radius, 0, Math.PI * 1.5, '#6bd8b3');

            //Circular arc
            sector(circleX, circleY, radius-8, Math.PI * 1.5, 0, lineWidth * 2);
            //Dots at both ends
            // smallcircle1(cradius + Math.cos(2 * Math.PI / 360 * 120) * radius, cradius + Math.sin(2 * Math.PI / 360 * 120) *
            //     radius, 0);
            // smallcircle2(cradius + Math.cos(2 * Math.PI / 360 * (120 + process * 3)) * radius, cradius + Math.sin(2 * Math.PI /
            //     360 * (120 + process * 3)) * radius, 2);
            //Controls the speed of the animation at the end
            if (process / percent > 0.90) {
                process += 0.30;
            } else if (process / percent > 0.80) {
                process += 0.55;
            } else if (process / percent > 0.70) {
                process += 0.75;
            } else {
                process += 1.0;
            }
        }
    
        var process = 0.0; // speed of progress
        var circleLoading = window.setInterval(function() {
            loading();
            console.log(11)
        }, 20);
        // loading();
    }
    
    toCanvas('canvas','#6bd8b3', 32);
  
  </script>
</body>
</html>