Circle.mjs 4.5 KB

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