graceCirProgress.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template name="graceCirProgress">
  2. <view :style="{width:width+'px', height:width+'px'}">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: "graceCirProgress",
  9. props: {
  10. canvasId:{
  11. type:String,
  12. default:'',
  13. },
  14. width : {
  15. type : Number,
  16. default : 200
  17. },
  18. lineWidth: {
  19. type: Number,
  20. default: 10
  21. },
  22. bgColor:{
  23. type : String,
  24. default : "#F1F1F1"
  25. },
  26. progressColor:{
  27. type: String,
  28. default: "#00C777"
  29. },
  30. fontColor: {
  31. type: String,
  32. default: "#00C777"
  33. },
  34. value : {
  35. type : Number,
  36. default : 0
  37. },
  38. fontSize : {
  39. type: Number,
  40. default: 30
  41. },
  42. speed : {
  43. type : Number,
  44. default : 20
  45. }
  46. },
  47. data() {
  48. return {
  49. ctx : null,
  50. sets : {},
  51. oldVal : 0,
  52. canvas : {}
  53. }
  54. },
  55. created:function(){
  56. var _self = this;
  57. var sets = {};
  58. sets.center = this.width / 2; // 圆心
  59. sets.lineWidth = this.lineWidth; // 外层圆环宽
  60. sets.r = (this.width / 2) - (sets.lineWidth / 2); // 背景半径 = 外层
  61. sets.lineWidth2 = sets.lineWidth - 2; // 内层圆环宽
  62. this.sets = sets;
  63. // 创建画布
  64. this.ctx = wx.createCanvasContext(this.canvasId);
  65. this.drawAnimate(this.value);
  66. },
  67. watch:{
  68. value(val, val2){
  69. this.drawAnimate(val);
  70. }
  71. },
  72. methods: {
  73. drawAnimate: function (num){
  74. var _self = this;
  75. var step = 0;
  76. if (_self.oldVal > num) {
  77. for (var i = _self.oldVal; i >= num; i--) {
  78. (function(i){setTimeout(function(){_self.draw(i);}, step);})(i);
  79. step += _self.speed;
  80. }
  81. } else {
  82. for (var i = _self.oldVal; i <= num; i++) {
  83. (function(i){setTimeout(function(){_self.draw(i);}, step);})(i);
  84. step += _self.speed;
  85. }
  86. }
  87. _self.oldVal = num;
  88. },
  89. draw : function (num){
  90. if (num < 0) { num = 0;}
  91. if(num > 100){num = 100;}
  92. // 背景圆
  93. this.ctx.setLineWidth(this.sets.lineWidth);
  94. this.ctx.setStrokeStyle(this.bgColor);
  95. this.ctx.setLineCap('round');
  96. this.ctx.beginPath();
  97. this.ctx.arc(this.sets.center, this.sets.center, this.sets.r, 0, 2 * Math.PI, false);
  98. this.ctx.stroke();
  99. // 进度圆
  100. this.ctx.setLineWidth(this.sets.lineWidth2);
  101. this.ctx.setStrokeStyle(this.progressColor);
  102. this.ctx.setLineCap('round')
  103. this.ctx.beginPath();//开始一个新的路径
  104. num = (num / 100);
  105. this.ctx.arc(this.sets.center, this.sets.center, this.sets.r, 0 * Math.PI, num * 2 * Math.PI, false);
  106. this.ctx.stroke();
  107. // 文字
  108. this.ctx.setFillStyle(this.fontColor);
  109. this.ctx.setFontSize(this.fontSize);
  110. this.ctx.setTextAlign("center");
  111. this.ctx.setTextBaseline('middle');
  112. this.ctx.fillText(Math.round(num * 100) + "%", this.sets.center, this.sets.center);
  113. this.ctx.draw();
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped>
  119. </style>