半圓形進度條(html)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>半環形進度條</title>
</head>
<style>
  
</style>

<body>
  <canvas id="canvas" width="150" height="150">
    <p>抱歉,您的瀏覽器不支持canvas</p>
  </canvas>
  <script>
    toCanvas('canvas','red',30);
    
    /**
     *  生成環形進度條
     */
    function toCanvas(id, color, progress) {
        //canvas進度條
        var canvas = document.getElementById(id),
            ctx = canvas.getContext("2d"),
            percent = progress, //最終百分比
            circleX = canvas.width / 2, //中心x座標
            circleY = canvas.height / 2, //中心y座標
            radius = 60, //圓環半徑
            cradius = 75, // canvas半徑
            lineWidth = 6, //圓形線條的寬度
            fontSize = 20; //字體大小
        //兩端圓點
        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();
        }
    
        //畫圓
        function circle(cx, cy, r) {
            ctx.beginPath();
            //ctx.moveTo(cx + r, cy);
            ctx.lineWidth = lineWidth;
            ctx.strokeStyle = '#eee';
            ctx.arc(cx, cy, r, Math.PI * 2 / 3, Math.PI * 1 / 3);
            ctx.stroke();
        }
    
        //畫弧線
        function sector(cx, cy, r, startAngle, endAngle, anti) {
            ctx.beginPath();
            //ctx.moveTo(cx, cy + r); // 從圓形底部開始畫
            ctx.lineWidth = lineWidth;
    
            // 漸變色 - 可自定義
            // 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;
            // 進度條顏色
            ctx.strokeStyle = color;
            //圓弧兩端的樣式
            ctx.lineCap = 'round';
    
            //圓弧
            ctx.arc(
                cx, cy, r,
                (Math.PI * 2 / 3),
                (Math.PI * 2 / 3) + endAngle / 100 * (Math.PI * 5 / 3),
                false
            );
            ctx.stroke();
        }
    
        //刷新
        function loading() {
            if (process >= percent) {
              clearInterval(circleLoading);
            }
            //清除canvas內容
            ctx.clearRect(0, 0, circleX * 2, circleY * 2);
            //中間的字
            ctx.font = fontSize + 'px April';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillStyle = '#999';
            ctx.fillText(parseFloat(process).toFixed(0) + '%', circleX, circleY);
            //圓形
            circle(circleX, circleY, radius);
            //圓弧
            sector(circleX, circleY, radius, Math.PI * 2 / 3, process);
            //兩端圓點
            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);
            //控制結束時動畫的速度
            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; //進度
        var circleLoading = window.setInterval(function() {
            loading();
        }, 20);
    }
  </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章