-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnBalloonCircle.vue
152 lines (141 loc) · 3.51 KB
/
AnBalloonCircle.vue
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<template>
<div class="an-balloon-circle" :class="{ isSafari: isSafari }">
<div class="an-balloon-circle__shape-left" />
<div class="an-balloon-circle__shape-right" />
<div ref="text" class="an-balloon-circle__text">
<span ref="textInner">{{ text }}</span>
</div>
</div>
</template>
<script>
export default {
name: 'AnBalloonKugel',
props: {
text: { type: String, required: true }
},
data() {
return {
isSafari: false
};
},
created() {
const ua = navigator.userAgent.toLowerCase();
if (ua.includes('safari')) {
if (!ua.includes('chrome')) {
this.isSafari = true;
}
}
// https://stackoverflow.com/a/7944490
},
mounted() {
setTimeout(this.calculateLabels, 500);
setTimeout(this.calculateLabels, 5000);
if (this.isSafari) {
setTimeout(this.setCircleDimensions, 400);
window.addEventListener('resize', this.setCircleDimensions);
}
},
destroyed() {
window.removeEventListener('resize', this.setCircleDimensions);
},
methods: {
setCircleDimensions() {
const { height, width } = this.$el.parentElement.getBoundingClientRect();
this.$el.style.width = width;
this.$el.style.height = height;
},
calculateLabels() {
const getComputedStyle = (el, prop) => {
if (el)
return Number(window.getComputedStyle(el)[prop].replace('px', ''));
};
const circleHeight = getComputedStyle(this.$el, 'height');
const text = this.$refs.text;
const textInner = this.$refs.textInner;
let fontSize = getComputedStyle(text, 'font-size');
if (getComputedStyle(text, 'height') < circleHeight) {
// add padding until text centred in circle
let paddingTop = 1;
while (
paddingTop + textInner.offsetHeight / 2 <
this.$el.offsetHeight / 2
) {
text.style.paddingTop = `${paddingTop}px`;
paddingTop++;
}
} else {
console.log('resize', this.text);
// reduce font size until text fits inside circle
while (getComputedStyle(text, 'height') > circleHeight) {
fontSize -= 0.1;
text.style.fontSize = `${fontSize}px`;
}
}
}
}
};
</script>
<style lang="scss" scoped>
.an-balloon-circle {
position: relative;
height: 100%;
width: 100%;
background-color: $color-theme-lightblue;
border-radius: 50%;
&.isSafari {
position: fixed;
height: 260%;
width: 260%;
// foreignObject isn't supported in safari for 100%, that's why the circles need position fixed and 260% as width and height
// in safari elements inside of foreignObject use html coordinate system instead of svg coordinate system
// https://stackoverflow.com/a/62082096 https://bugs.webkit.org/show_bug.cgi?id=23113
}
&__shape-left,
&__shape-right {
width: 50%;
height: 100%;
}
&__shape-left {
float: left;
shape-outside: polygon(
0 0,
98% 0,
50% 6%,
23.4% 17.3%,
6% 32.6%,
0 50%,
6% 65.6%,
23.4% 82.7%,
50% 94%,
98% 100%,
0 100%
);
shape-margin: 0%;
}
&__shape-right {
float: right;
shape-outside: polygon(
2% 0%,
100% 0%,
100% 100%,
2% 100%,
50% 94%,
76.6% 82.7%,
94% 65.6%,
100% 50%,
94% 32.6%,
76.6% 17.3%,
50% 6%
);
shape-margin: 0%;
}
&__text {
font-size: 6px;
text-align: center;
margin: 0;
.isSafari & {
font-size: 12px;
}
}
}
</style>