index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. const toAngle = (a) => a / 180 * Math.PI
  4. const percent = (a) => toAngle(a / 100 * 360)
  5. const easeInOutCubic = (a, b, c, d) => {
  6. a /= d / 2
  7. if (a < 1) return c / 2 * a * a * a + b
  8. a -= 2
  9. return c / 2 * (a * a * a + 2) + b
  10. }
  11. baseComponent({
  12. properties: {
  13. prefixCls: {
  14. type: String,
  15. value: 'wux-circle',
  16. },
  17. percent: {
  18. type: Number,
  19. value: 0,
  20. observer: 'redraw',
  21. },
  22. strokeWidth: {
  23. type: Number,
  24. value: 10,
  25. },
  26. size: {
  27. type: Number,
  28. value: 120,
  29. observer: 'updateStyle',
  30. },
  31. lineCap: {
  32. type: String,
  33. value: 'round',
  34. },
  35. backgroundColor: {
  36. type: String,
  37. value: '#f3f3f3',
  38. },
  39. color: {
  40. type: String,
  41. value: '#33cd5f',
  42. },
  43. sAngle: {
  44. type: Number,
  45. value: 0,
  46. observer(newVal) {
  47. this.setData({
  48. beginAngle: toAngle(newVal),
  49. })
  50. },
  51. },
  52. counterclockwise: {
  53. type: Boolean,
  54. value: false,
  55. },
  56. speed: {
  57. type: Number,
  58. value: 2000,
  59. },
  60. animate: {
  61. type: Boolean,
  62. value: true,
  63. },
  64. background: {
  65. type: Boolean,
  66. value: true,
  67. },
  68. },
  69. data: {
  70. beginAngle: 0,
  71. startAngle: 0,
  72. endAngle: 0,
  73. currentAngle: 0,
  74. },
  75. computed: {
  76. classes: ['prefixCls', function(prefixCls) {
  77. const wrap = classNames(prefixCls)
  78. const inner = `${prefixCls}__inner`
  79. return {
  80. wrap,
  81. inner,
  82. }
  83. }],
  84. },
  85. methods: {
  86. /**
  87. * 更新样式
  88. */
  89. updateStyle(size = this.data.size) {
  90. const style = `width: ${size}px; height: ${size}px;`
  91. this.setData({
  92. style,
  93. })
  94. },
  95. /**
  96. * 着帧绘制 canvas
  97. */
  98. redraw(value = this.data.percent) {
  99. const endAngle = percent(value)
  100. const now = Date.now()
  101. const decrease = this.data.currentAngle > endAngle
  102. const startAngle = !decrease ? this.data.currentAngle : this.data.endAngle
  103. this.cancelNextCallback()
  104. this.clearTimer()
  105. this.safeSetData({ startAngle, endAngle }, () => {
  106. this.animate(now, now, decrease)
  107. })
  108. },
  109. /**
  110. * 绘制 canvas
  111. */
  112. draw(line = true) {
  113. const { lineCap, backgroundColor, color, size, strokeWidth, counterclockwise, background } = this.data
  114. const position = size / 2
  115. const radius = position - strokeWidth / 2
  116. const p = 2 * Math.PI
  117. const startAngle = counterclockwise ? p - this.data.beginAngle : this.data.beginAngle
  118. const endAngle = counterclockwise ? p - (this.data.beginAngle + this.data.currentAngle) : this.data.beginAngle + this.data.currentAngle
  119. // 创建 canvas 绘图上下文
  120. this.ctx = this.ctx || wx.createCanvasContext('circle', this)
  121. // 清除画布
  122. this.ctx.clearRect(0, 0, size, size)
  123. // 绘制背景
  124. if (background) {
  125. this.ctx.beginPath()
  126. this.ctx.arc(position, position, radius, 0, 2 * Math.PI)
  127. this.ctx.setLineWidth(strokeWidth)
  128. this.ctx.setStrokeStyle(backgroundColor)
  129. this.ctx.stroke()
  130. }
  131. // 绘制进度
  132. if (line) {
  133. this.ctx.beginPath()
  134. this.ctx.arc(position, position, radius, startAngle, endAngle)
  135. this.ctx.setLineWidth(strokeWidth)
  136. this.ctx.setStrokeStyle(color)
  137. this.ctx.setLineCap(lineCap)
  138. this.ctx.stroke()
  139. }
  140. // 绘制完成
  141. this.ctx.draw(false, () => {
  142. this.triggerEvent('change', { value: this.data.currentAngle })
  143. })
  144. },
  145. /**
  146. * 开始动画
  147. */
  148. animate(c, d, e) {
  149. const now = Date.now()
  150. const f = now - c < 1 ? 1 : now - c
  151. const { animate, speed, startAngle, endAngle } = this.data
  152. const isEnd = !e && 1000 * this.data.currentAngle <= Math.floor(1000 * endAngle) || e && 1000 * this.data.currentAngle >= Math.floor(1000 * endAngle)
  153. if (animate && c - d < 1.05 * speed && isEnd) {
  154. const value = easeInOutCubic((c - d) / f, startAngle, endAngle - startAngle, speed / f)
  155. const currentAngle = value < 0 ? 0 : value
  156. c = Date.now()
  157. this.safeSetData({ currentAngle }, () => {
  158. this.draw(currentAngle !== 0)
  159. this.timer = setTimeout(() => this.animate(c, d, e), 1000 / 60)
  160. })
  161. } else {
  162. this.safeSetData({ currentAngle: endAngle }, () => this.draw(endAngle !== 0))
  163. }
  164. },
  165. /**
  166. * 清除定时器
  167. */
  168. clearTimer() {
  169. if (this.timer) {
  170. clearTimeout(this.timer)
  171. this.timer = null
  172. }
  173. },
  174. },
  175. attached() {
  176. this.updateStyle()
  177. if (this.data.percent === 0) {
  178. this.draw(false)
  179. }
  180. },
  181. detached() {
  182. this.ctx = null
  183. this.clearTimer()
  184. },
  185. })