index.vue 7.2KB

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