circle-progress.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // components/circle/circle.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. // 新增一个进度百分比属性,外部可通过此属性传递进度值
  8. progress: {
  9. type: Number,
  10. value: 0,
  11. observer(newVal) {
  12. this.updateProgress(newVal);
  13. }
  14. },
  15. disabled: {
  16. type: Boolean,
  17. value: false,
  18. observer(newVal) {
  19. this.updateDisabled(newVal);
  20. }
  21. },
  22. width: {
  23. type: Number,
  24. value: 100,
  25. observer(newVal) {
  26. this.updateWidth(newVal);
  27. }
  28. },
  29. r: {
  30. type: Number,
  31. value: 90,
  32. observer(newVal) {
  33. this.updateR(newVal);
  34. }
  35. },
  36. lw: {
  37. type: Number,
  38. value: 5,
  39. observer(newVal) {
  40. this.updateLw(newVal);
  41. }
  42. },
  43. },
  44. /**
  45. * 组件的初始数据
  46. */
  47. data: {
  48. progress_txt: '加载中...', // 提示文字
  49. progress: 0,
  50. disabled: false,
  51. width:110,
  52. r:110,
  53. lw:7,
  54. },
  55. lifetimes: {
  56. attached() {
  57. // 组件加载后准备绘制
  58. this.initCanvas();
  59. },
  60. },
  61. /**
  62. * 组件的方法列表
  63. */
  64. methods: {
  65. /**
  66. * 初始化 Canvas 上下文
  67. */
  68. initCanvas() {
  69. // 获取两个canvas元素的上下文
  70. this.bgContext = wx.createCanvasContext('canvasProgressbg', this);
  71. this.progressContext = wx.createCanvasContext('canvasProgress', this);
  72. this.drawProgressbg();
  73. this.drawCircle(this.data.progress);
  74. },
  75. /**
  76. * 画progress底部背景
  77. */
  78. drawProgressbg() {
  79. let w = this.data.width;
  80. let r = this.data.r;
  81. let lw = this.data.lw;
  82. this.progressContext.setFillStyle('#FFFFFF');
  83. this.progressContext.beginPath();
  84. this.progressContext.arc(w, w, r, 0, 2 * Math.PI, false);
  85. this.progressContext.fill();
  86. this.bgContext.setLineWidth(lw);
  87. this.bgContext.setStrokeStyle('#FFFFFF');
  88. this.bgContext.setLineCap('round');
  89. this.bgContext.beginPath();
  90. this.bgContext.arc(w, w, r, 0, 2 * Math.PI, false);
  91. this.bgContext.stroke();
  92. this.bgContext.draw(false, () => {}); // 第二个参数用于兼容旧版API,新版不需要传入回调函数
  93. this.progressContext.setFillStyle('red');
  94. },
  95. /**
  96. * 更新并画progress进度
  97. */
  98. updateProgress(progressPercentage) {
  99. this.setData({
  100. progress:progressPercentage
  101. })
  102. //const normalizedProgress = (progressPercentage / 100) * 2; // 转换成弧度,最大值为2(对应100%)
  103. if(this.progressContext){
  104. this.drawCircle(this.data.progressPercentage);
  105. }
  106. },
  107. /**
  108. * 更新并画disabled
  109. */
  110. updateDisabled(dis) {
  111. this.setData({
  112. disabled:dis
  113. })
  114. //const normalizedProgress = (progressPercentage / 100) * 2; // 转换成弧度,最大值为2(对应100%)
  115. if(this.progressContext){
  116. this.drawCircle(this.data.progress);
  117. }
  118. },
  119. /**
  120. * 更新并画disabled
  121. */
  122. updateWidth(dis) {
  123. this.setData({
  124. width:dis
  125. })
  126. //const normalizedProgress = (progressPercentage / 100) * 2; // 转换成弧度,最大值为2(对应100%)
  127. if(this.progressContext){
  128. this.drawCircle(this.data.progressPercentage);
  129. }
  130. },
  131. updateR(dis) {
  132. this.setData({
  133. r:dis
  134. })
  135. //const normalizedProgress = (progressPercentage / 100) * 2; // 转换成弧度,最大值为2(对应100%)
  136. if(this.progressContext){
  137. this.drawCircle(this.data.progressPercentage);
  138. }
  139. },
  140. updateLw(dis) {
  141. this.setData({
  142. r:dis
  143. })
  144. //const normalizedProgress = (progressPercentage / 100) * 2; // 转换成弧度,最大值为2(对应100%)
  145. if(this.progressContext){
  146. this.drawCircle(this.data.progressPercentage);
  147. }
  148. },
  149. /**
  150. * 画progress进度
  151. */
  152. drawCircle(step) {
  153. step = step * 2 / 100;
  154. let w = this.data.width;
  155. let r = this.data.r;
  156. let lw = this.data.lw;
  157. this.progressContext.setLineWidth(lw+1);
  158. this.progressContext.setStrokeStyle(this.data.disabled?'#B6B3C1':'#21ADFF');
  159. this.progressContext.setLineCap('round');
  160. this.progressContext.beginPath();
  161. this.progressContext.arc(w, w, r, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
  162. this.progressContext.stroke();
  163. this.progressContext.draw(false, () => {}); // 同样添加false参数
  164. this.setData({
  165. progress_txt:this.data.progress + "%"
  166. })
  167. },
  168. }
  169. })