风险测评小程序前端代码仓库
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.

150 lines
3.3 KiB

1 week 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
  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 ctx = uni.createCanvasContext(`cpbar${this.code}`)
  46. let Ratio = 1 / 2
  47. let w = 14 * Ratio
  48. ctx.setLineWidth(w); // 设置圆环的宽度
  49. ctx.setStrokeStyle(color); // 设置圆环的颜色
  50. let width = 126 * Ratio
  51. let height = 126 * Ratio
  52. ctx.beginPath();
  53. let x = width / 2
  54. let y = height / 2
  55. let r = 62 * Ratio - w / 2
  56. ctx.arc(x, y, r, START, END, false);
  57. ctx.stroke();
  58. ctx.draw()
  59. return
  60. uni.createSelectorQuery().in(this)
  61. .select('.progress_bar')
  62. .fields({
  63. node: true,
  64. size: true
  65. })
  66. .exec(async (res) => {
  67. // const canvas = res[0].node
  68. console.log('res', res)
  69. // Canvas 画布的实际绘制宽高
  70. // const width = res[0].width
  71. // const height = res[0].height
  72. const width = res.width
  73. const height = res.height
  74. // //根据dpr调整
  75. // const dpr = wx.getWindowInfo().pixelRatio
  76. // canvas.width = width * dpr
  77. // canvas.height = height * dpr
  78. // let Ratio = width / 126
  79. let Ratio = 1
  80. console.log('Ratio', Ratio)
  81. // 渲染上下文
  82. const ctx = uni.createCanvasContext('myCanvas', this)
  83. console.log('ctx', ctx)
  84. // ctx.scale(dpr, dpr)
  85. // ctx.clearRect(0, 0, width, height)
  86. let w = 14 * Ratio
  87. ctx.setLineWidth(w); // 设置圆环的宽度
  88. ctx.setStrokeStyle(color); // 设置圆环的颜色
  89. // ctx.lineCap = 'round'; // 设置圆环端点的形状
  90. ctx.beginPath();
  91. let x = width / 2
  92. let y = height / 2
  93. let r = 62 * Ratio - w / 2
  94. ctx.arc(x, y, r, START, END, false);
  95. ctx.stroke();
  96. ctx.draw()
  97. })
  98. },
  99. },
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. $size: 126rpx;
  104. .progress_box {
  105. position: relative;
  106. width: $size;
  107. // height: $size;
  108. height: 154rpx;
  109. align-items: flex-end;
  110. font-size: 22rpx;
  111. line-height: 40rpx;
  112. }
  113. .progress_bar {
  114. position: absolute;
  115. top: 0;
  116. left: 0;
  117. width: $size;
  118. height: $size;
  119. }
  120. .value {
  121. position: absolute;
  122. top: 40rpx;
  123. left: 50%;
  124. transform: translateX(-50%);
  125. font-size: 32rpx;
  126. font-weight: 600;
  127. }
  128. </style>