index.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { BLUE, WHITE } from '../common/color';
  2. import { VantComponent } from '../common/component';
  3. import { getSystemInfoSync } from '../common/utils';
  4. import { isObj } from '../common/validator';
  5. import { canIUseCanvas2d } from '../common/version';
  6. import { adaptor } from './canvas';
  7. function format(rate) {
  8. return Math.min(Math.max(rate, 0), 100);
  9. }
  10. const PERIMETER = 2 * Math.PI;
  11. const BEGIN_ANGLE = -Math.PI / 2;
  12. const STEP = 1;
  13. VantComponent({
  14. props: {
  15. text: String,
  16. lineCap: {
  17. type: String,
  18. value: 'round',
  19. },
  20. value: {
  21. type: Number,
  22. value: 0,
  23. observer: 'reRender',
  24. },
  25. speed: {
  26. type: Number,
  27. value: 50,
  28. },
  29. size: {
  30. type: Number,
  31. value: 100,
  32. observer() {
  33. this.drawCircle(this.currentValue);
  34. },
  35. },
  36. fill: String,
  37. layerColor: {
  38. type: String,
  39. value: WHITE,
  40. },
  41. color: {
  42. type: null,
  43. value: BLUE,
  44. observer() {
  45. this.setHoverColor().then(() => {
  46. this.drawCircle(this.currentValue);
  47. });
  48. },
  49. },
  50. type: {
  51. type: String,
  52. value: '',
  53. },
  54. strokeWidth: {
  55. type: Number,
  56. value: 4,
  57. },
  58. clockwise: {
  59. type: Boolean,
  60. value: true,
  61. },
  62. },
  63. data: {
  64. hoverColor: BLUE,
  65. },
  66. methods: {
  67. getContext() {
  68. const { type, size } = this.data;
  69. if (type === '' || !canIUseCanvas2d()) {
  70. const ctx = wx.createCanvasContext('van-circle', this);
  71. return Promise.resolve(ctx);
  72. }
  73. const dpr = getSystemInfoSync().pixelRatio;
  74. return new Promise((resolve) => {
  75. wx.createSelectorQuery()
  76. .in(this)
  77. .select('#van-circle')
  78. .node()
  79. .exec((res) => {
  80. const canvas = res[0].node;
  81. const ctx = canvas.getContext(type);
  82. if (!this.inited) {
  83. this.inited = true;
  84. canvas.width = size * dpr;
  85. canvas.height = size * dpr;
  86. ctx.scale(dpr, dpr);
  87. }
  88. resolve(adaptor(ctx));
  89. });
  90. });
  91. },
  92. setHoverColor() {
  93. const { color, size } = this.data;
  94. if (isObj(color)) {
  95. return this.getContext().then((context) => {
  96. const LinearColor = context.createLinearGradient(size, 0, 0, 0);
  97. Object.keys(color)
  98. .sort((a, b) => parseFloat(a) - parseFloat(b))
  99. .map((key) => LinearColor.addColorStop(parseFloat(key) / 100, color[key]));
  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.clearMockInterval();
  154. this.currentValue = this.currentValue || 0;
  155. const run = () => {
  156. this.interval = setTimeout(() => {
  157. if (this.currentValue !== value) {
  158. if (Math.abs(this.currentValue - value) < STEP) {
  159. this.currentValue = value;
  160. }
  161. else if (this.currentValue < value) {
  162. this.currentValue += STEP;
  163. }
  164. else {
  165. this.currentValue -= STEP;
  166. }
  167. this.drawCircle(this.currentValue);
  168. run();
  169. }
  170. else {
  171. this.clearMockInterval();
  172. }
  173. }, 1000 / speed);
  174. };
  175. run();
  176. },
  177. clearMockInterval() {
  178. if (this.interval) {
  179. clearTimeout(this.interval);
  180. this.interval = null;
  181. }
  182. },
  183. },
  184. mounted() {
  185. this.currentValue = this.data.value;
  186. this.setHoverColor().then(() => {
  187. this.drawCircle(this.currentValue);
  188. });
  189. },
  190. destroyed() {
  191. this.clearMockInterval();
  192. },
  193. });