风险测评小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.4 KiB

2 days ago
1 week ago
1 week ago
2 days ago
1 week ago
2 days ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
  1. <template>
  2. <view class="flex progress_box" :style="style">
  3. <view>{{ label || '' }}</view>
  4. <canvas ref="canvas"
  5. class="progress_bar"
  6. :canvas-id="`cpbar${code}`"
  7. :id="`cpbar${code}`"
  8. type="2d"></canvas>
  9. <view class="value">{{ value || 0 }}</view>
  10. </view>
  11. </template>
  12. <script>
  13. const START = - Math.PI * 3 / 2 + Math.PI * 1 / 4 - Math.PI * 1 / 24
  14. const END = Math.PI * 1 / 4 + Math.PI * 1 / 24
  15. export default {
  16. props: {
  17. label: {
  18. type: String,
  19. default: '',
  20. },
  21. value: {
  22. type: String | Number,
  23. default: 0,
  24. },
  25. color: {
  26. type: String,
  27. default: '#014FA2'
  28. }
  29. },
  30. data() {
  31. return {
  32. code: Math.floor(Math.random() * 100).toString()
  33. }
  34. },
  35. computed: {
  36. style() {
  37. return `color: ${this.color};`
  38. }
  39. },
  40. mounted() {
  41. this.drawProgress(this.color)
  42. },
  43. methods: {
  44. drawProgress(color) {
  45. const clientWidth = this.$refs.canvas.$el.clientWidth
  46. const ctx = uni.createCanvasContext(`cpbar${this.code}`)
  47. let Ratio = clientWidth / 126
  48. let w = 14 * Ratio
  49. ctx.setLineWidth(w); // 设置圆环的宽度
  50. ctx.setStrokeStyle(color); // 设置圆环的颜色
  51. let width = 126 * Ratio
  52. let height = 126 * Ratio
  53. ctx.beginPath();
  54. let x = width / 2
  55. let y = height / 2
  56. let r = 62 * Ratio - w / 2
  57. ctx.arc(x, y, r, START, END, false);
  58. ctx.stroke();
  59. ctx.draw()
  60. return
  61. uni.createSelectorQuery().in(this)
  62. .select('.progress_bar')
  63. .fields({
  64. node: true,
  65. size: true
  66. })
  67. .exec(async (res) => {
  68. // const canvas = res[0].node
  69. console.log('res', res)
  70. // Canvas 画布的实际绘制宽高
  71. // const width = res[0].width
  72. // const height = res[0].height
  73. const width = res.width
  74. const height = res.height
  75. // //根据dpr调整
  76. // const dpr = wx.getWindowInfo().pixelRatio
  77. // canvas.width = width * dpr
  78. // canvas.height = height * dpr
  79. // let Ratio = width / 126
  80. let Ratio = 1
  81. console.log('Ratio', Ratio)
  82. // 渲染上下文
  83. const ctx = uni.createCanvasContext('myCanvas', this)
  84. console.log('ctx', ctx)
  85. // ctx.scale(dpr, dpr)
  86. // ctx.clearRect(0, 0, width, height)
  87. let w = 14 * Ratio
  88. ctx.setLineWidth(w); // 设置圆环的宽度
  89. ctx.setStrokeStyle(color); // 设置圆环的颜色
  90. // ctx.lineCap = 'round'; // 设置圆环端点的形状
  91. ctx.beginPath();
  92. let x = width / 2
  93. let y = height / 2
  94. let r = 62 * Ratio - w / 2
  95. ctx.arc(x, y, r, START, END, false);
  96. ctx.stroke();
  97. ctx.draw()
  98. })
  99. },
  100. },
  101. }
  102. </script>
  103. <style lang="scss" scoped>
  104. $size: 126rpx;
  105. .progress_box {
  106. position: relative;
  107. width: $size;
  108. // height: $size;
  109. height: 154rpx;
  110. align-items: flex-end;
  111. font-size: 22rpx;
  112. line-height: 40rpx;
  113. }
  114. .progress_bar {
  115. position: absolute;
  116. top: 0;
  117. left: 0;
  118. width: $size;
  119. height: $size;
  120. }
  121. .value {
  122. position: absolute;
  123. top: 40rpx;
  124. left: 50%;
  125. transform: translateX(-50%);
  126. font-size: 32rpx;
  127. font-weight: 600;
  128. }
  129. </style>