Home > Web Front-end > H5 Tutorial > Detailed explanation of the example method of realizing arc and ring progress bars on canvas

Detailed explanation of the example method of realizing arc and ring progress bars on canvas

零下一度
Release: 2017-05-27 15:26:02
Original
3308 people have browsed it

The following summarizes how to achieve the circular progress bar effect in my project. I hope it will be helpful to everyone:

This method draws a circle through canvas To realize the dynamic ring progress bar using the shape method, enter the code directly. If you have any questions, please see the comments:

The HTML code is as follows. Just create a canvas on the page:

<canvas id="canvas" width="300" height="300">
    <p>抱歉,您的浏览器不支持canvas</p>
 </canvas>
Copy after login

JS is divided into two parts,
The first part implements the overall function, and the second part calls:
The first part:
The functional principle of the first part is roughly to draw two circles, one is the background color Circle, the second one is a dynamically loaded arc, the progress is loaded through timer; the color is added with gradient color;

 function toCanvas(id ,progress){                canvas进度条
                var canvas = document.getElementById(id),
                ctx = canvas.getContext("2d"),
                percent = progress,  最终百分比                circleX = canvas.width / 2,  中心x坐标                circleY = canvas.height / 2,  中心y坐标                radius = 100, 圆环半径                lineWidth = 5,  圆形线条的宽度                fontSize = 20; 字体大小
                 两端圆点
                function smallcircle1(cx, cy, r) {
                    ctx.beginPath();                    //ctx.moveTo(cx + r, cy);                    ctx.lineWidth = 1;
                    ctx.fillStyle = &#39;#06a8f3&#39;;
                    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 = &#39;#00f8bb&#39;;
                     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 = &#39;#eee&#39;;
                     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, &#39;#06a8f3&#39;);                     //linGrad.addColorStop(0.5, &#39;#9bc4eb&#39;);                     linGrad.addColorStop(1.0, &#39;#00f8bb&#39;);
                     ctx.strokeStyle = linGrad;                      圆弧两端的样式                     ctx.lineCap = &#39;round&#39;;                     圆弧                     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 + &#39;px April&#39;;
                     ctx.textAlign = &#39;center&#39;;
                     ctx.textBaseline = &#39;middle&#39;;
                     ctx.fillStyle = &#39;#999&#39;;
                     ctx.fillText(parseFloat(process).toFixed(0) + &#39;%&#39;, circleX, circleY);    
                  
                     圆形                     circle(circleX, circleY, radius);                     
                      圆弧                     sector(circleX, circleY, radius, Math.PI*2/3, process);
                     两端圆点                     smallcircle1(150+Math.cos(2*Math.PI/360*120)*100, 150+Math.sin(2*Math.PI/360*120)*100, 5);
                     smallcircle2(150+Math.cos(2*Math.PI/360*(120+process*3))*100, 150+Math.sin(2*Math.PI/360*(120+process*3))*100, 5);                     控制结束时动画的速度
                     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);
                     
            }
第二部分,调用封装好的代码:
            toCanvas(&#39;canvas&#39;,50);
Copy after login

[Related recommendations]

1. Canvas implements a circular progress bar and displays a digital percentage

2. Use CSS clip to implement an audio playback circular progress bar tutorial example

3. Share the example code of h5 canvas circle progress bar

4. H5 canvas realizes the example of circular dynamic loading progress

The above is the detailed content of Detailed explanation of the example method of realizing arc and ring progress bars on canvas. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template