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() {
  77. const { prefixCls } = this.data
  78. const wrap = classNames(prefixCls)
  79. const inner = `${prefixCls}__inner`
  80. return {
  81. wrap,
  82. inner,
  83. }
  84. },
  85. },
  86. methods: {
  87. /**
  88. * 更新样式
  89. */
  90. updateStyle(size = this.data.size) {
  91. const style = `width: ${size}px; height: ${size}px;`
  92. this.setData({
  93. style,
  94. })
  95. },
  96. /**
  97. * 着帧绘制 canvas
  98. */
  99. redraw(value = this.data.percent) {
  100. const endAngle = percent(value)
  101. const now = Date.now()
  102. const decrease = this.data.currentAngle > endAngle
  103. const startAngle = !decrease ? this.data.currentAngle : this.data.endAngle
  104. this.cancelNextCallback()
  105. this.clearTimer()
  106. this.safeSetData({ startAngle, endAngle }, () => {
  107. this.animate(now, now, decrease)
  108. })
  109. },
  110. /**
  111. * 绘制 canvas
  112. */
  113. draw(line = true) {
  114. const { lineCap, backgroundColor, color, size, strokeWidth, counterclockwise, background } = this.data
  115. const position = size / 2
  116. const radius = position - strokeWidth / 2
  117. const p = 2 * Math.PI
  118. const startAngle = counterclockwise ? p - this.data.beginAngle : this.data.beginAngle
  119. const endAngle = counterclockwise ? p - (this.data.beginAngle + this.data.currentAngle) : this.data.beginAngle + this.data.currentAngle
  120. // 创建 canvas 绘图上下文
  121. this.ctx = this.ctx || wx.createCanvasContext('circle', this)
  122. // 清除画布
  123. this.ctx.clearRect(0, 0, size, size)
  124. // 绘制背景
  125. if (background) {
  126. this.ctx.beginPath()
  127. this.ctx.arc(position, position, radius, 0, 2 * Math.PI)
  128. this.ctx.setLineWidth(strokeWidth)
  129. this.ctx.setStrokeStyle(backgroundColor)
  130. this.ctx.stroke()
  131. }
  132. // 绘制进度
  133. if (line) {
  134. this.ctx.beginPath()
  135. this.ctx.arc(position, position, radius, startAngle, endAngle)
  136. this.ctx.setLineWidth(strokeWidth)
  137. this.ctx.setStrokeStyle(color)
  138. this.ctx.setLineCap(lineCap)
  139. this.ctx.stroke()
  140. }
  141. // 绘制完成
  142. this.ctx.draw(false, () => {
  143. this.triggerEvent('change', { value: this.data.currentAngle })
  144. })
  145. },
  146. /**
  147. * 开始动画
  148. */
  149. animate(c, d, e) {
  150. const now = Date.now()
  151. const f = now - c < 1 ? 1 : now - c
  152. const { animate, speed, startAngle, endAngle } = this.data
  153. const isEnd = !e && 1000 * this.data.currentAngle <= Math.floor(1000 * endAngle) || e && 1000 * this.data.currentAngle >= Math.floor(1000 * endAngle)
  154. if (animate && c - d < 1.05 * speed && isEnd) {
  155. const value = easeInOutCubic((c - d) / f, startAngle, endAngle - startAngle, speed / f)
  156. const currentAngle = value < 0 ? 0 : value
  157. c = Date.now()
  158. this.safeSetData({ currentAngle }, () => {
  159. this.draw(currentAngle !== 0)
  160. this.timer = setTimeout(() => this.animate(c, d, e), 1000 / 60)
  161. })
  162. } else {
  163. this.safeSetData({ currentAngle: endAngle }, () => this.draw(endAngle !== 0))
  164. }
  165. },
  166. /**
  167. * 清除定时器
  168. */
  169. clearTimer() {
  170. if (this.timer) {
  171. clearTimeout(this.timer)
  172. this.timer = null
  173. }
  174. },
  175. },
  176. attached() {
  177. this.updateStyle()
  178. if (this.data.percent === 0) {
  179. this.draw(false)
  180. }
  181. },
  182. detached() {
  183. this.ctx = null
  184. this.clearTimer()
  185. },
  186. })