index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { VantComponent } from '../common/component';
  2. import { isObj } from '../common/utils';
  3. import { BLUE, WHITE } from '../common/color';
  4. function format(rate) {
  5. return Math.min(Math.max(rate, 0), 100);
  6. }
  7. const PERIMETER = 2 * Math.PI;
  8. const BEGIN_ANGLE = -Math.PI / 2;
  9. const STEP = 1;
  10. VantComponent({
  11. props: {
  12. text: String,
  13. lineCap: {
  14. type: String,
  15. value: 'round'
  16. },
  17. value: {
  18. type: Number,
  19. value: 0,
  20. observer: 'reRender'
  21. },
  22. speed: {
  23. type: Number,
  24. value: 50
  25. },
  26. size: {
  27. type: Number,
  28. value: 100,
  29. },
  30. fill: String,
  31. layerColor: {
  32. type: String,
  33. value: WHITE
  34. },
  35. color: {
  36. type: [String, Object],
  37. value: BLUE,
  38. observer: 'setHoverColor'
  39. },
  40. type: {
  41. type: String,
  42. value: ''
  43. },
  44. strokeWidth: {
  45. type: Number,
  46. value: 4
  47. },
  48. clockwise: {
  49. type: Boolean,
  50. value: true
  51. }
  52. },
  53. data: {
  54. hoverColor: BLUE
  55. },
  56. methods: {
  57. getContext() {
  58. if (!this.ctx) {
  59. this.ctx = wx.createCanvasContext('van-circle', this);
  60. }
  61. return this.ctx;
  62. },
  63. setHoverColor() {
  64. const { color, size, type } = this.data;
  65. const context = type ? this.getContext(type) : this.getContext();
  66. let hoverColor = color;
  67. if (isObj(color)) {
  68. const LinearColor = context.createLinearGradient(size, 0, 0, 0);
  69. Object.keys(color)
  70. .sort((a, b) => parseFloat(a) - parseFloat(b))
  71. .map(key => LinearColor.addColorStop(parseFloat(key) / 100, color[key]));
  72. hoverColor = LinearColor;
  73. }
  74. this.setData({ hoverColor });
  75. },
  76. presetCanvas(context, strokeStyle, beginAngle, endAngle, fill) {
  77. const { strokeWidth, lineCap, clockwise, size } = this.data;
  78. const position = size / 2;
  79. const radius = position - strokeWidth / 2;
  80. context.setStrokeStyle(strokeStyle);
  81. context.setLineWidth(strokeWidth);
  82. context.setLineCap(lineCap);
  83. context.beginPath();
  84. context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
  85. context.stroke();
  86. if (fill) {
  87. context.setFillStyle(fill);
  88. context.fill();
  89. }
  90. },
  91. renderLayerCircle(context) {
  92. const { layerColor, fill } = this.data;
  93. this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
  94. },
  95. renderHoverCircle(context, formatValue) {
  96. const { clockwise, hoverColor } = this.data;
  97. // 结束角度
  98. const progress = PERIMETER * (formatValue / 100);
  99. const endAngle = clockwise
  100. ? BEGIN_ANGLE + progress
  101. : 3 * Math.PI - (BEGIN_ANGLE + progress);
  102. this.presetCanvas(context, hoverColor, BEGIN_ANGLE, endAngle);
  103. },
  104. drawCircle(currentValue) {
  105. const { size, type } = this.data;
  106. const context = type ? this.getContext(type) : this.getContext();
  107. context.clearRect(0, 0, size, size);
  108. this.renderLayerCircle(context);
  109. const formatValue = format(currentValue);
  110. if (formatValue !== 0) {
  111. this.renderHoverCircle(context, formatValue);
  112. }
  113. context.draw();
  114. },
  115. reRender() {
  116. // tofector 动画暂时没有想到好的解决方案
  117. const { value, speed } = this.data;
  118. if (speed <= 0 || speed > 1000) {
  119. this.drawCircle(value);
  120. return;
  121. }
  122. this.clearInterval();
  123. this.currentValue = this.currentValue || 0;
  124. this.interval = setInterval(() => {
  125. if (this.currentValue !== value) {
  126. if (this.currentValue < value) {
  127. this.currentValue += STEP;
  128. }
  129. else {
  130. this.currentValue -= STEP;
  131. }
  132. this.drawCircle(this.currentValue);
  133. }
  134. else {
  135. this.clearInterval();
  136. }
  137. }, 1000 / speed);
  138. },
  139. clearInterval() {
  140. if (this.interval) {
  141. clearInterval(this.interval);
  142. this.interval = null;
  143. }
  144. }
  145. },
  146. created() {
  147. const { value } = this.data;
  148. this.currentValue = value;
  149. this.drawCircle(value);
  150. },
  151. destroyed() {
  152. this.ctx = null;
  153. this.clearInterval();
  154. }
  155. });