/
tool-box.vue
executable file
·253 lines (237 loc) · 6.87 KB
/
tool-box.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<template>
<div id="tools">
<div class="container">
<div class="tools-list">
<button class="webrtc"
:title="$i18n.text.webrtc"
@click="toggleWebrtc"
:disabled="barrageState || powerSavingMode"
:class="{
active: webrtcState,
close: barrageState
}">
<i class="iconfont icon-webrtc"></i>
</button>
<button class="barrage"
:title="$i18n.text.barrage.name"
@click="toggleBarrage"
:disabled="webrtcState"
:class="{
active: barrageState,
close: webrtcState
}">
<i class="iconfont icon-barrage"></i>
</button>
<a class="feedback"
:title="$i18n.text.feedback"
href="mailto:surmon@foxmail.com"
target="_blank">
<i class="iconfont icon-mail"></i>
</a>
<button class="to-top"
:title="$i18n.text.totop"
@click="totop"
@mouseover="setButtonState('top', true, true)"
@mouseleave="setButtonState('top', false)">
<i class="iconfont icon-totop"></i>
</button>
<button class="to-bottom"
:title="$i18n.text.tobottom"
@click="toBottom"
@mouseover="setButtonState('bottom', true, true)"
@mouseleave="setButtonState('bottom', false)">
<i class="iconfont icon-tobottom"></i>
</button>
</div>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import underscore from '~/utils/underscore-simple'
import { scrollTo, Easing } from '~/utils/scroll-to-anywhere'
export default {
name: 'tool-box',
data() {
return {
topBtnMouseOver: false,
bottomBtnMouseOver: false,
toggleWebrtcFn: null,
firstOpenWeRtc: true,
animationFrameId: null
}
},
computed: {
...mapState('option', {
language: 'language',
webrtcState: 'openWebrtc',
barrageState: 'openBarrage',
powerSavingMode: 'powerSavingMode'
})
},
methods: {
totop() {
scrollTo('body', 300, { easing: Easing['ease-in'] })
},
toBottom() {
scrollTo(window.scrollY + window.innerHeight, 300, { easing: Easing['ease-in'] })
},
setButtonState(position, state, start) {
this[position === 'bottom' ? 'bottomBtnMouseOver' : 'topBtnMouseOver'] = state
window.cancelAnimationFrame(this.animationFrameId)
start && this.slowMoveToAnyWhere()
},
slowMoveToAnyWhere() {
const step = () => {
let targetScrollY = window.scrollY
const currentScrollY = document.body.scrollHeight - window.innerHeight
if (this.bottomBtnMouseOver) targetScrollY += 1
if (this.topBtnMouseOver) targetScrollY -= 1
if (targetScrollY < 0) {
targetScrollY = 0
} else if (targetScrollY >= currentScrollY) {
targetScrollY = currentScrollY
}
const canScrollTo = targetScrollY > 0 && targetScrollY < currentScrollY
if (!canScrollTo) {
return false
}
window.scrollTo(0, targetScrollY)
if (this.bottomBtnMouseOver || this.topBtnMouseOver) {
this.animationFrameId = window.requestAnimationFrame(step)
} else {
window.cancelAnimationFrame(this.animationFrameId)
return false
}
}
this.animationFrameId = window.requestAnimationFrame(step)
},
toggleBarrage() {
this.$store.commit('option/UPDATE_BARRAGE_STATE')
},
toggleWebrtc() {
const isEn = this.$store.getters['option/langIsEn']
if (this.firstOpenWeRtc && !this.webrtcState) {
const confirmText = isEn
? 'Will open WebRTC、WebGL、Canvas, ready?'
: '实验室功能需要 WebRTC、WebGL、Canvas 等技术的支持,可能占用较多 CPU/GPU 资源,甚至死机、起火、爆炸、毁灭,继续?'
if (!window.confirm(confirmText)) {
this.$store.commit('option/UPDATE_WEBRTC_STATE', false)
window.alert(isEn ? 'Sorry~' : '滚滚滚')
return false
}
this.firstOpenWeRtc = false
}
if (this.toggleWebrtcFn) {
this.toggleWebrtcFn()
} else {
this.toggleWebrtcFn = underscore.throttle(() => {
this.$store.commit('option/UPDATE_WEBRTC_STATE')
}, 1666)
this.toggleWebrtcFn()
}
}
}
}
</script>
<style lang="scss" scoped>
#tools {
position: fixed;
z-index: 9;
width: 100%;
bottom: 30rem;
> .container {
position: relative;
> .tools-list {
position: absolute;
right: -10em;
width: 3em;
height: 7em;
> .webrtc,
> .barrage,
> .to-top,
> .to-bottom,
> .feedback {
display: block;
width: 3em;
height: 3em;
line-height: 3em;
text-align: center;
background-color: $module-bg;
&:hover {
background-color: $module-hover-bg;
}
}
@keyframes default-btn-bg {
0% {
color: white;
background: chartreuse;
}
12% {
color: white;
background: green;
}
24% {
color: white;
background: red;
}
36% {
color: white;
background: darkviolet;
}
60% {
color: white;
background: pink;
}
72% {
color: $text;
background: yellow;
}
86% {
color: $text;
background: white;
}
100% {
color: white;
background: black;
}
}
@keyframes webrtc {
0% {
color: white;
background: $primary;
}
100% {
color: $primary;
background: white;
}
}
> .webrtc {
animation: webrtc 3s infinite;
&.active {
animation: webrtc .5s infinite;
}
&.close {
animation: none;
}
}
> .barrage {
color: white;
animation: default-btn-bg 10s infinite;
&.active {
background-color: $module-hover-bg;
animation: default-btn-bg steps(1) 1.666s infinite;
}
&.close {
color: $link-color;
animation: none;
}
}
> .to-bottom {
height: 2em;
line-height: 2em;
}
}
}
}
</style>