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
| <script setup name="SvgDiode">
| import { ref, computed } from "vue";
| const props = defineProps({
| len: {
| type: Number,
| default: 20,
| },
| offset: {
| type: Array,
| default() {
| return [0, 0];
| },
| },
| color: {
| type: String,
| default: "#f59a23",
| },
| lineWidth: {
| type: [Number, String],
| default: 2,
| },
| // 方向为二极管导通方向 三角形指向
| direct: {
| type: String,
| default: "left",
| validator: (v) => ["left", "right", "top", "bottom"].includes(v),
| },
| });
|
| const y1 = computed(() => {
| let len = props.len;
| return len / -2;
| });
|
| const y2 = computed(() => {
| let len = props.len;
| return len / 2;
| });
|
| const points = computed(() => {
| let len = props.len;
| let lineWidth = props.lineWidth;
| let len2 = len / 2;
| return [
| [lineWidth, 0],
| [lineWidth + Math.cos((30 * Math.PI) / 180) * len, len2],
| [lineWidth + Math.cos((30 * Math.PI) / 180) * len, -len2],
| ].join(" ");
| });
|
| const transform = computed(() => {
| let { offset, direct, lineWidth, len } = props;
| let rotate = "";
| switch (direct) {
| case "left":
| rotate = "";
| break;
| case "right":
| rotate = `rotate(180 0 0) translate(-${lineWidth + Math.cos((30 * Math.PI) / 180) * len} 0)`;
| break;
| case "top":
| rotate = "rotate(90 0 0)";
| break;
| case "bottom":
| rotate = `rotate(270 0 0) translate(-${lineWidth + Math.cos((30 * Math.PI) / 180) * len} 0)`;
| break;
| }
| return `translate(${offset.join(",")}) ${rotate}`;
| });
|
| </script>
|
| <template>
| <g :transform="transform">
| <polygon :points="points" stroke="none" :fill="color" />
| <line :y1="y1" :y2="y2" :stroke-width="lineWidth" :stroke="color"></line>
| </g>
| </template>
|
|