index.js 4.9 KB

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