Skip to content

Commit 1989aae

Browse files
committed
feat: 优化新版Demo
1 parent 65e7dca commit 1989aae

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

scripts/deploy.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
set -e
55

66
# build
7-
npm run build
7+
npm run demo:build
88

99
# navigate into the build output directory
10-
cd docs
10+
cd dist
1111

1212
# if you are deploying to a custom domain
1313
# echo 'www.example.com' > CNAME
@@ -20,6 +20,6 @@ git commit -m 'docs: deploy'
2020
# git push -f [email protected]:<USERNAME>/<USERNAME>.github.io.git master
2121

2222
# if you are deploying to https://<USERNAME>.github.io/<REPO>
23-
git push -f [email protected]:hellodigua/vue-danmaku.git master:gh-pages
23+
git push -f [email protected]:hellodigua/vue-danmaku.git main:gh-pages
2424

2525
cd -

src/Demo.vue

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,22 @@
3232
<span class="title">速度:</span>
3333
<button class="btn" @click="handleChange('speeds', -10)">减速</button>
3434
<button class="btn" @click="handleChange('speeds', 10)">增速</button>
35-
<span>当前速度:{{ config.speeds }}像素/s</span>
35+
<span>速度:{{ config.speeds }}像素/s</span>
3636
</p>
3737
<p>
3838
<span class="title">轨道:</span>
3939
<button class="btn" @click="handleChange('channels', -1)">-1</button>
4040
<button class="btn" @click="handleChange('channels', 1)">+1</button>
4141
<button class="btn" @click="handleChange('channels', -config.channels)">填满</button>
42-
<span>当前轨道:{{ config.channels }}</span>
42+
<span>轨道:{{ config.channels }}</span>
4343
</p>
4444
<p>
45-
<span class="title">发送:</span>
46-
<input class="ipt" type="text" v-model="danmuMsg" />
47-
<button class="btn" @click="addDanmu">发送</button>
45+
<span class="title">刷新频率:</span>
46+
<button class="btn" @click="handleChange('debounce', -currentDebounceStep)">
47+
-{{ currentDebounceStep }}ms
48+
</button>
49+
<button class="btn" @click="handleChange('debounce', currentDebounceStep)">+{{ currentDebounceStep }}ms</button>
50+
<span>频率:{{ config.debounce }}ms</span>
4851
</p>
4952
<p>
5053
<span class="title">性能模式:</span>
@@ -56,6 +59,16 @@
5659
<button class="btn" @click="toggleMirror">{{ config.mirror ? '关闭' : '开启' }}</button>
5760
<span>{{ config.mirror ? '已开启' : '已关闭' }}</span>
5861
</p>
62+
<p>
63+
<span class="title">随机轨道:</span>
64+
<button class="btn" @click="toggleRandomChannel">{{ config.randomChannel ? '关闭' : '开启' }}</button>
65+
<span>{{ config.randomChannel ? '已开启' : '已关闭' }}</span>
66+
</p>
67+
<p>
68+
<span class="title">发送:</span>
69+
<input class="ipt" type="text" v-model="danmuMsg" />
70+
<button class="btn" @click="addDanmu">发送</button>
71+
</p>
5972
</div>
6073
</div>
6174
<a
@@ -87,7 +100,7 @@
87100
</a>
88101
</template>
89102
<script lang="ts">
90-
import { defineComponent, reactive, ref } from 'vue'
103+
import { defineComponent, reactive, ref, computed } from 'vue'
91104
import { getDanmuData } from './assets/danmu.js'
92105
import VueDanmaku from './lib/Danmaku.vue'
93106
@@ -102,12 +115,12 @@ export default defineComponent({
102115
const danmuMsg = ref<string>('')
103116
let timer: number = 0
104117
const config = reactive({
105-
channels: 5, // 轨道数量,为0则弹幕轨道数会撑满容器
118+
channels: 8, // 轨道数量,为0则弹幕轨道数会撑满容器
106119
loop: true, // 是否开启弹幕循环
107120
speeds: 200, // 弹幕速度,实际为弹幕滚动完一整屏的秒数,值越小速度越快
108121
top: 10, // 弹幕轨道间的垂直间距
109122
right: 0, // 同一轨道弹幕的水平间距
110-
debounce: 10, // 弹幕刷新频率(多少毫秒插入一条弹幕,建议不小于50)
123+
debounce: 100, // 弹幕刷新频率(多少毫秒插入一条弹幕,建议不小于50)
111124
randomChannel: true, // 随机弹幕轨道
112125
performanceMode: true, // 性能模式,使用requestAnimationFrame代替CSS动画
113126
autoResize: true, // 启用内部resize监听
@@ -152,8 +165,37 @@ export default defineComponent({
152165
}
153166
config.channels += val
154167
}
168+
169+
if (type === 'debounce') {
170+
const step = getDebounceStep(config.debounce)
171+
const newValue = config.debounce + val
172+
173+
if (newValue < 10) {
174+
return
175+
}
176+
177+
config.debounce = newValue
178+
179+
// 自动重启弹幕以使新频率生效
180+
if (danmaku.value) {
181+
danmaku.value.reset()
182+
danmaku.value.play()
183+
}
184+
}
155185
}
156186
187+
function getDebounceStep(currentValue: number): number {
188+
if (currentValue <= 100) {
189+
return 10 // 10-100ms范围用10ms步长
190+
} else if (currentValue <= 500) {
191+
return 50 // 100-500ms范围用50ms步长
192+
} else {
193+
return 100 // 500ms+范围用100ms步长
194+
}
195+
}
196+
197+
const currentDebounceStep = computed(() => getDebounceStep(config.debounce))
198+
157199
function addDanmu() {
158200
if (!danmuMsg.value) return
159201
const _danmuMsg = {
@@ -162,7 +204,7 @@ export default defineComponent({
162204
text: danmuMsg.value,
163205
}
164206
165-
danmaku.value.add(_danmuMsg)
207+
danmaku.value.addDanmu(_danmuMsg)
166208
danmuMsg.value = ''
167209
}
168210
@@ -174,22 +216,31 @@ export default defineComponent({
174216
}
175217
/**
176218
* 切换镜像
177-
*/
178-
function toggleMirror() {
179-
config.mirror = !config.mirror;
180-
}
219+
*/
220+
function toggleMirror() {
221+
config.mirror = !config.mirror
222+
}
223+
224+
/**
225+
* 切换随机轨道
226+
*/
227+
function toggleRandomChannel() {
228+
config.randomChannel = !config.randomChannel
229+
}
181230
182231
return {
183232
danmaku,
184233
danmus,
185234
config,
186235
danmuMsg,
236+
currentDebounceStep,
187237
188238
handleInvoke,
189239
handleChange,
190240
addDanmu,
191241
togglePerformanceMode,
192242
toggleMirror,
243+
toggleRandomChannel,
193244
}
194245
},
195246
})
@@ -301,6 +352,12 @@ body {
301352
}
302353
303354
@media (max-width: 500px) {
355+
.main {
356+
transform: scale(0.8);
357+
transform-origin: center;
358+
transition: transform 0.3s ease;
359+
}
360+
304361
.github-corner:hover .octo-arm {
305362
animation: none;
306363
}

0 commit comments

Comments
 (0)