详解canvas实现圆弧、圆环进度条的实例方法


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

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 = '#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 = '#00f8bb';

                     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.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(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('canvas',50);

【相关推荐】

1. Canvas实现圆形进度条并显示数字百分比

2. 利用CSS clip 实现音频播放圆环进度条教程实例

3. 分享h5 canvas圆圈进度条的实例代码

4. H5 canvas实现圆形动态加载进度实例

以上就是详解canvas实现圆弧、圆环进度条的实例方法的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

html5 canvas常用属性方法(介绍)

canvas中beginpath()和closepath()作用的实例解析

html5 canvas基本绘图之绘制矩形的示例代码详解

html5联合canvas实现图片压缩

使用html5的canvas和javascript创建一个绘图程序的示例代码

html5如何实现简单进度条效果?动态进度条的实现(代码示例)

如何使用canvas画出平滑的曲线?(代码)

canvas图片跨域会遇到的问题及解决方法总结

html5中canvas元素如何绘制图形

canvas实现雪花随机动态飘落效果(代码示例)

更多相关阅读请进入《canvas》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...