chocolate-progress-bar.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view class="progress_box">
  3. <canvas class="progress_bg" canvas-id="cpbg"></canvas>
  4. <canvas class="progress_bar" canvas-id="cpbar"></canvas>
  5. <view class="progress_txt">
  6. <!-- <view class="progress_info">{{ progress_txt }}%</view> -->
  7. <view class="progress_info">{{progress_txt}}/{{progress_total}}</view>
  8. <text>每日完成占比</text>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. progress_txt: {
  16. type: Number,
  17. default: 0
  18. },
  19. progress_total: {
  20. type: Number,
  21. default: 0
  22. }
  23. },
  24. onReady() {
  25. this.drawProgressbg();
  26. this.drawCircle(this.progress_txt / this.progress_total * 100); //参数为1-100
  27. },
  28. methods: {
  29. drawProgressbg() {
  30. // 自定义组件实例 this ,表示在这个自定义组件下查找拥有 canvas-id 的 <canvas/>
  31. var ctx = uni.createCanvasContext('cpbg', this);
  32. ctx.setLineWidth(8); // 设置圆环的宽度
  33. ctx.setStrokeStyle('#CCCCCC'); // 设置圆环的颜色
  34. ctx.setLineCap('round'); // 设置圆环端点的形状
  35. ctx.beginPath(); //开始一个新的路径
  36. ctx.arc(80, 60, 55, 0.85 * Math.PI, 0.15 * Math.PI, false);
  37. //设置一个原点(110,110),半径为100的圆的路径到当前路径
  38. ctx.stroke(); //对当前路径进行描边
  39. ctx.draw();
  40. },
  41. drawCircle(step) {
  42. if (step == 0 || Object.is(step, NaN)) {
  43. return
  44. } else {
  45. var ctx = uni.createCanvasContext('cpbar', this);
  46. // 进度条的渐变(中心x坐标-半径-边宽,中心Y坐标,中心x坐标+半径+边宽,中心Y坐标)
  47. var gradient = ctx.createLinearGradient(28, 55, 192, 55);
  48. gradient.addColorStop('0', '#2A82E4');
  49. gradient.addColorStop('1.0', '#2A82E4');
  50. ctx.setLineWidth(8);
  51. ctx.setStrokeStyle(gradient);
  52. ctx.setLineCap('round');
  53. ctx.beginPath();
  54. // 参数step 为绘制的百分比
  55. step = 0.015 * step + 0.75;
  56. if (step >= 2.25) {
  57. step = 0.15
  58. }
  59. ctx.arc(80, 60, 55, 0.85 * Math.PI, step * Math.PI, false);
  60. ctx.stroke();
  61. ctx.draw();
  62. }
  63. }
  64. }
  65. };
  66. </script>
  67. <style lang="scss" scoped>
  68. .progress_box {
  69. position: relative;
  70. width: 100%;
  71. height: 100%;
  72. display: flex;
  73. align-items: center;
  74. justify-content: center;
  75. text-align: center;
  76. .progress_bg {
  77. z-index: 1;
  78. position: absolute;
  79. width: 100%;
  80. height: 100%;
  81. }
  82. .progress_bar {
  83. z-index: 1;
  84. position: absolute;
  85. width: 100%;
  86. height: 100%;
  87. }
  88. .progress_txt {
  89. position: absolute;
  90. padding-right: 20rpx;
  91. .progress_info {
  92. letter-spacing: 2rpx;
  93. font-size: 36rpx;
  94. }
  95. text {
  96. font-size: 22rpx;
  97. color: #A6A6A6;
  98. }
  99. }
  100. }
  101. /* .progress_dot {
  102. width: 16upx;
  103. height: 16upx;
  104. border-radius: 50%;
  105. background-color: #fb9126;
  106. } */
  107. </style>