Skip to content

Commit af0ea50

Browse files
author
yqz
committed
feat:优化看板娘加载
1 parent a902fa5 commit af0ea50

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

vue/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
-->
2222
<link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="any" />
2323
<link rel="apple-touch-icon" href="/favicon.ico" />
24+
<link rel="preconnect" href="https://model.oml2d.com" crossorigin />
25+
<link rel="dns-prefetch" href="https://model.oml2d.com" />
26+
<link rel="preconnect" href="https://v1.hitokoto.cn" crossorigin />
27+
<link rel="dns-prefetch" href="https://v1.hitokoto.cn" />
2428
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
2529
<title>OpenBlog</title>
2630
</head>

vue/src/components/Live2dCharacter.vue

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
<button
55
type="button"
66
class="live2d-toggle"
7+
:class="{ 'live2d-toggle--loading': loading }"
78
:aria-label="dismissed ? '召唤看板娘' : '隐藏看板娘'"
8-
:title="dismissed ? '召唤看板娘' : '隐藏看板娘'"
9+
:title="loading ? '看板娘加载中…' : dismissed ? '召唤看板娘' : '隐藏看板娘'"
910
@click="toggle"
1011
>
11-
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
12+
<svg v-if="loading" class="live2d-spinner" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
13+
<circle cx="12" cy="12" r="10" stroke-dasharray="32" stroke-dashoffset="32" />
14+
</svg>
15+
<svg v-else width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
1216
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z" />
1317
<circle cx="8.5" cy="10" r="1.5" />
1418
<circle cx="15.5" cy="10" r="1.5" />
@@ -37,10 +41,12 @@ function isDark() {
3741
}
3842
3943
const dismissed = ref(readHidden())
44+
const loading = ref(false)
4045
const containerRef = ref(null)
4146
let oml2dInstance = null
4247
4348
function toggle() {
49+
if (loading.value) return
4450
dismissed.value = !dismissed.value
4551
try {
4652
sessionStorage.setItem(STORAGE_KEY, dismissed.value ? '1' : '0')
@@ -77,6 +83,8 @@ function makeTipsStyle(dark) {
7783
onMounted(async () => {
7884
if (!containerRef.value) return
7985
86+
loading.value = true
87+
8088
const { loadOml2d } = await import('oh-my-live2d')
8189
8290
oml2dInstance = loadOml2d({
@@ -91,9 +99,7 @@ onMounted(async () => {
9199
path: 'https://model.oml2d.com/Pio/model.json',
92100
position: [0, 10],
93101
scale: 0.1,
94-
stageStyle: {
95-
height: 340,
96-
},
102+
stageStyle: { height: 340 },
97103
mobileScale: 0.08,
98104
mobilePosition: [0, 5],
99105
},
@@ -102,21 +108,15 @@ onMounted(async () => {
102108
path: 'https://model.oml2d.com/HK416-1-normal/model.json',
103109
position: [0, 50],
104110
scale: 0.08,
105-
stageStyle: {
106-
height: 380,
107-
},
111+
stageStyle: { height: 380 },
108112
mobileScale: 0.06,
109113
mobilePosition: [0, 30],
110114
},
111115
],
112116
tips: {
113117
messageLine: 3,
114118
style: makeTipsStyle(isDark()),
115-
mobileStyle: {
116-
width: 180,
117-
height: 56,
118-
fontSize: '12px',
119-
},
119+
mobileStyle: { width: 180, height: 56, fontSize: '12px' },
120120
welcomeTips: {
121121
duration: 5000,
122122
message: {
@@ -136,23 +136,23 @@ onMounted(async () => {
136136
interval: 18000,
137137
},
138138
},
139-
statusBar: {
140-
enable: false,
141-
},
142-
menus: {
143-
disable: true,
144-
},
139+
statusBar: { enable: false },
140+
menus: { disable: true },
145141
parentElement: containerRef.value,
146142
})
147143
144+
oml2dInstance.onLoad(() => {
145+
loading.value = false
146+
})
147+
148148
if (dismissed.value) {
149-
const onLoad = () => {
150-
oml2dInstance?.stageSlideOut()
151-
oml2dInstance?.onStageSlideOut(() => {
152-
oml2dInstance?.onLoad(() => {})
153-
})
154-
}
155-
oml2dInstance.onLoad(onLoad)
149+
let firstLoad = true
150+
oml2dInstance.onLoad(() => {
151+
if (firstLoad) {
152+
oml2dInstance.stageSlideOut()
153+
firstLoad = false
154+
}
155+
})
156156
}
157157
})
158158
</script>
@@ -207,18 +207,31 @@ onMounted(async () => {
207207
transform: scale(1.1);
208208
}
209209
210+
.live2d-toggle--loading {
211+
cursor: wait;
212+
color: var(--accent);
213+
}
214+
215+
.live2d-toggle--loading:hover {
216+
transform: none;
217+
}
218+
210219
.live2d-wrapper--hidden .live2d-toggle {
211220
animation: live2d-toggle-pulse 2s ease-in-out infinite;
212221
}
213222
214223
@keyframes live2d-toggle-pulse {
215224
0%,
216-
100% {
217-
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
218-
}
219-
50% {
220-
box-shadow: 0 2px 18px rgba(170, 59, 255, 0.3);
221-
}
225+
100% { box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08); }
226+
50% { box-shadow: 0 2px 18px rgba(170, 59, 255, 0.3); }
227+
}
228+
229+
@keyframes live2d-spin {
230+
to { transform: rotate(360deg); }
231+
}
232+
233+
.live2d-spinner {
234+
animation: live2d-spin 1s linear infinite;
222235
}
223236
224237
@media (max-width: 680px) {

0 commit comments

Comments
 (0)