chocolate-progress-bar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: 100
  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. var ctx = uni.createCanvasContext('cpbar', this);
  43. // 进度条的渐变(中心x坐标-半径-边宽,中心Y坐标,中心x坐标+半径+边宽,中心Y坐标)
  44. var gradient = ctx.createLinearGradient(28, 55, 192, 55);
  45. gradient.addColorStop('0', '#2A82E4');
  46. gradient.addColorStop('1.0', '#2A82E4');
  47. ctx.setLineWidth(8);
  48. ctx.setStrokeStyle(gradient);
  49. ctx.setLineCap('round');
  50. ctx.beginPath();
  51. // 参数step 为绘制的百分比
  52. step = 0.015 * step + 0.75;
  53. if (step >= 2) {
  54. step = step % 2;
  55. }
  56. ctx.arc(80, 60, 55, 0.85 * Math.PI, step * Math.PI, false);
  57. ctx.stroke();
  58. ctx.draw();
  59. }
  60. }
  61. };
  62. </script>
  63. <style lang="scss" scoped>
  64. .progress_box {
  65. position: relative;
  66. width: 100%;
  67. height: 100%;
  68. display: flex;
  69. align-items: center;
  70. justify-content: center;
  71. text-align: center;
  72. .progress_bg {
  73. position: absolute;
  74. width: 100%;
  75. height: 100%;
  76. }
  77. .progress_bar {
  78. position: absolute;
  79. width: 100%;
  80. height: 100%;
  81. }
  82. .progress_txt {
  83. position: absolute;
  84. padding-right: 20rpx;
  85. .progress_info {
  86. letter-spacing: 2rpx;
  87. font-size: 36rpx;
  88. }
  89. text {
  90. font-size: 22rpx;
  91. color: #A6A6A6;
  92. }
  93. }
  94. }
  95. /* .progress_dot {
  96. width: 16upx;
  97. height: 16upx;
  98. border-radius: 50%;
  99. background-color: #fb9126;
  100. } */
  101. </style>