Circle.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { createVNode as _createVNode } from "vue";
  2. import { watch, computed, defineComponent } from "vue";
  3. import { raf, cancelRaf } from "@vant/use";
  4. import { isObject, truthProp, numericProp, getSizeStyle, makeStringProp, makeNumberProp, makeNumericProp, createNamespace } from "../utils/index.mjs";
  5. const [name, bem] = createNamespace("circle");
  6. let uid = 0;
  7. const format = (rate) => Math.min(Math.max(+rate, 0), 100);
  8. function getPath(clockwise, viewBoxSize) {
  9. const sweepFlag = clockwise ? 1 : 0;
  10. return `M ${viewBoxSize / 2} ${viewBoxSize / 2} m 0, -500 a 500, 500 0 1, ${sweepFlag} 0, 1000 a 500, 500 0 1, ${sweepFlag} 0, -1000`;
  11. }
  12. const circleProps = {
  13. text: String,
  14. size: numericProp,
  15. fill: makeStringProp("none"),
  16. rate: makeNumericProp(100),
  17. speed: makeNumericProp(0),
  18. color: [String, Object],
  19. clockwise: truthProp,
  20. layerColor: String,
  21. currentRate: makeNumberProp(0),
  22. strokeWidth: makeNumericProp(40),
  23. strokeLinecap: String,
  24. startPosition: makeStringProp("top")
  25. };
  26. var stdin_default = defineComponent({
  27. name,
  28. props: circleProps,
  29. emits: ["update:currentRate"],
  30. setup(props, {
  31. emit,
  32. slots
  33. }) {
  34. const id = `van-circle-${uid++}`;
  35. const viewBoxSize = computed(() => +props.strokeWidth + 1e3);
  36. const path = computed(() => getPath(props.clockwise, viewBoxSize.value));
  37. const svgStyle = computed(() => {
  38. const ROTATE_ANGLE_MAP = {
  39. top: 0,
  40. right: 90,
  41. bottom: 180,
  42. left: 270
  43. };
  44. const angleValue = ROTATE_ANGLE_MAP[props.startPosition];
  45. if (angleValue) {
  46. return {
  47. transform: `rotate(${angleValue}deg)`
  48. };
  49. }
  50. });
  51. watch(() => props.rate, (rate) => {
  52. let rafId;
  53. const startTime = Date.now();
  54. const startRate = props.currentRate;
  55. const endRate = format(rate);
  56. const duration = Math.abs((startRate - endRate) * 1e3 / +props.speed);
  57. const animate = () => {
  58. const now = Date.now();
  59. const progress = Math.min((now - startTime) / duration, 1);
  60. const rate2 = progress * (endRate - startRate) + startRate;
  61. emit("update:currentRate", format(parseFloat(rate2.toFixed(1))));
  62. if (endRate > startRate ? rate2 < endRate : rate2 > endRate) {
  63. rafId = raf(animate);
  64. }
  65. };
  66. if (props.speed) {
  67. if (rafId) {
  68. cancelRaf(rafId);
  69. }
  70. rafId = raf(animate);
  71. } else {
  72. emit("update:currentRate", endRate);
  73. }
  74. }, {
  75. immediate: true
  76. });
  77. const renderHover = () => {
  78. const PERIMETER = 3140;
  79. const {
  80. strokeWidth,
  81. currentRate,
  82. strokeLinecap
  83. } = props;
  84. const offset = PERIMETER * currentRate / 100;
  85. const color = isObject(props.color) ? `url(#${id})` : props.color;
  86. const style = {
  87. stroke: color,
  88. strokeWidth: `${+strokeWidth + 1}px`,
  89. strokeLinecap,
  90. strokeDasharray: `${offset}px ${PERIMETER}px`
  91. };
  92. return _createVNode("path", {
  93. "d": path.value,
  94. "style": style,
  95. "class": bem("hover"),
  96. "stroke": color
  97. }, null);
  98. };
  99. const renderLayer = () => {
  100. const style = {
  101. fill: props.fill,
  102. stroke: props.layerColor,
  103. strokeWidth: `${props.strokeWidth}px`
  104. };
  105. return _createVNode("path", {
  106. "class": bem("layer"),
  107. "style": style,
  108. "d": path.value
  109. }, null);
  110. };
  111. const renderGradient = () => {
  112. const {
  113. color
  114. } = props;
  115. if (!isObject(color)) {
  116. return;
  117. }
  118. const Stops = Object.keys(color).sort((a, b) => parseFloat(a) - parseFloat(b)).map((key, index) => _createVNode("stop", {
  119. "key": index,
  120. "offset": key,
  121. "stop-color": color[key]
  122. }, null));
  123. return _createVNode("defs", null, [_createVNode("linearGradient", {
  124. "id": id,
  125. "x1": "100%",
  126. "y1": "0%",
  127. "x2": "0%",
  128. "y2": "0%"
  129. }, [Stops])]);
  130. };
  131. const renderText = () => {
  132. if (slots.default) {
  133. return slots.default();
  134. }
  135. if (props.text) {
  136. return _createVNode("div", {
  137. "class": bem("text")
  138. }, [props.text]);
  139. }
  140. };
  141. return () => _createVNode("div", {
  142. "class": bem(),
  143. "style": getSizeStyle(props.size)
  144. }, [_createVNode("svg", {
  145. "viewBox": `0 0 ${viewBoxSize.value} ${viewBoxSize.value}`,
  146. "style": svgStyle.value
  147. }, [renderGradient(), renderLayer(), renderHover()]), renderText()]);
  148. }
  149. });
  150. export {
  151. stdin_default as default
  152. };