canvas-progress.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. Vue.component('canvas-progress', {
  2. template: '<canvas id="time-graph-canvas" width="100%" height="100%"></canvas>',
  3. props: ["progress"],
  4. data: function() {
  5. return {
  6. canvas:null,
  7. speed:0
  8. }
  9. },
  10. mounted: function () {
  11. this.canvas=document.getElementById("time-graph-canvas")
  12. this.drawMain(this.canvas, this.progress, "#12b7f5", "#bec8d2");
  13. },
  14. methods: {
  15. dataFunction:function(v) {
  16. if(v) {
  17. this.canvas=document.getElementById("time-graph-canvas")
  18. this.drawMain(this.canvas, v, "#12b7f5", "#bec8d2");
  19. }
  20. },
  21. drawMain: function(drawing_elem, percent, forecolor, bgcolor) {
  22. /*
  23. @drawing_elem: 绘制对象
  24. @percent:绘制圆环百分比, 范围[0, 100]
  25. @forecolor: 绘制圆环的前景色,颜色代码
  26. @bgcolor: 绘制圆环的背景色,颜色代码
  27. */
  28. var context = drawing_elem.getContext("2d");
  29. var center_x = drawing_elem.width / 2;
  30. var center_y = drawing_elem.height / 2;
  31. var rad = Math.PI*2/100;
  32. var speed = this.speed;
  33. // 绘制背景圆圈
  34. function backgroundCircle(){
  35. context.save();
  36. context.beginPath();
  37. context.lineWidth = 8; //设置线宽
  38. var radius = center_x - context.lineWidth;
  39. context.lineCap = "round";
  40. context.strokeStyle = bgcolor;
  41. context.arc(center_x, center_y, radius, 0, Math.PI*2, false);
  42. context.stroke();
  43. context.closePath();
  44. context.restore();
  45. }
  46. //绘制运动圆环
  47. function foregroundCircle(n){
  48. context.save();
  49. context.strokeStyle = forecolor;
  50. context.lineWidth = 8;
  51. context.lineCap = "round";
  52. var radius = center_x - context.lineWidth;
  53. context.beginPath();
  54. context.arc(center_x, center_y, radius , -Math.PI/2, -Math.PI/2 +n*rad, false); //用于绘制圆弧context.arc(x坐标,y坐标,半径,起始角度,终止角度,顺时针/逆时针)
  55. context.stroke();
  56. context.closePath();
  57. context.restore();
  58. }
  59. //绘制文字
  60. function text(n){
  61. context.save(); //save和restore可以保证样式属性只运用于该段canvas元素
  62. context.fillStyle = forecolor;
  63. var font_size = 20;
  64. context.font = font_size + "px Helvetica";
  65. var number=n.toFixed(0);
  66. number=number>=100?100:number;
  67. var text_width = context.measureText(number+"%").width;
  68. context.fillText(number+"%", center_x-text_width/2, center_y + font_size/2);
  69. context.restore();
  70. }
  71. //执行动画
  72. (function drawFrame(){
  73. window.requestAnimationFrame(drawFrame);
  74. context.clearRect(0, 0, drawing_elem.width, drawing_elem.height);
  75. backgroundCircle();
  76. text(speed);
  77. foregroundCircle(speed);
  78. if(speed >= percent) return;
  79. speed += 1;
  80. }());
  81. }
  82. },watch:{
  83. progress:function(nval,oval){
  84. nval!=0 && (this.speed=oval);
  85. nval==0 && (this.speed=0);
  86. this.drawMain(this.canvas, nval, "#12b7f5", "#bec8d2");
  87. }
  88. }
  89. })