he wei
71 分钟以前 7102b09561443f8c66bdcbecafac1706afc113fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<script setup name="SvgSwitch">
import { ref, computed, onMounted, watch, nextTick, } from "vue";
const props = defineProps({
  r: {
    type: Number,
    default: 3,
  },
  len: {
    type: Number,
    default: 60,
  },
  offset: {
    type: Array,
    default() {
      return [0, 0];
    },
  },
  isVertical: {
    type: Boolean,
    default: false,
  },
  state: {
    type: Boolean,
    defalut: false,
  },
  points: {
    type: Array,
    default() {
      return [
        [0, 0],
        [10, 10],
      ];
    },
  },
  color: {
    type: String,
    default: "#f59a23",
  },
  currColor: {
    type: String,
    default: "#0f0",
  },
  lineWidth: {
    type: [Number, String],
    default: 2,
  },
  turnStart: {
    type: Boolean,
    default: true
  }
});
 
const cx = computed(() => {
  let { isVertical, len } = props;
  return isVertical ? 0 : len;
});
const cy = computed(() => {
  let { isVertical, len } = props;
  return isVertical ? len : 0;
});
 
const pointsStr = computed(() => {
  return props.points.join(" ");
});
 
const fromDeg = ref(-2);
const toDeg = ref(-38);
 
watch(
  () => props.state,
  (n) => {
    nextTick(() => {
      changeSwitch();
    });
  },
  { immediate: true }
);
 
const anim = ref(null);
 
function changeSwitch() {
  if (props.state) {
    fromDeg.value = -38;
    toDeg.value = -2;
  } else {
    fromDeg.value = -2;
    toDeg.value = -38;
  }
  // 重新开启动画
  anim.value?.beginElement();
}
 
</script>
 
<template>
  <g ref="g" :transform="'translate(' + offset.join(',') + ')'">
    <circle :r="r" style="fill: #fff; stroke: none"></circle>
    <circle :r="r" :cx="cx" :cy="cy" style="fill: #fff; stroke: none"></circle>
    <line :x1="turnStart ? 0 : cx" :y1="turnStart ? 0 : cy" :x2="turnStart ? cx : 0" :y2="turnStart ? cy : 0" :stroke-width="lineWidth" :stroke="color">
      <animateTransform
        ref="anim"
        attributeName="transform"
        attributeType="XML"
        type="rotate"
        :from="[fromDeg, turnStart ? 0 : cx, turnStart ? 0 : cy].join(',')"
        :to="[toDeg, turnStart ? 0 : cx, turnStart ? 0 : cy].join(',')"
        dur="0.3s"
        repeatCount="1"
        fill="freeze"
      />
    </line>
  </g>
</template>
 
 
<style scoped lang="less">
</style>