Skip to content

Commit

Permalink
feat: 失焦后暂停游戏
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiaa12 committed Jun 14, 2023
1 parent 0607062 commit a3870ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/layout/Live2d.vue
Expand Up @@ -12,7 +12,7 @@ onMounted(() => {
new Live2d({
el: live2dContentRef.value,
showLoading: false,
maxWidth: 300,
maxWidth: 350,
})
})
</script>
21 changes: 15 additions & 6 deletions src/pages/components/game.vue
Expand Up @@ -11,14 +11,15 @@
</el-button>
<main>
<NesVue
ref="nesVueRef"
class="nes-vue"
label="开始游戏"
:url="BASE_URL + 'roms/' + curRom.url"
:width="screenSize.width"
:height="screenSize.height"
@error="nesErrorAlert"
/>
<div class="right">
<div class="options">
<div class="desc">
<el-descriptions
:title="curRom.title"
Expand Down Expand Up @@ -88,7 +89,7 @@
</template>

<script setup lang="ts">
import { PropType, computed, onBeforeUnmount, onMounted } from "vue"
import { PropType, computed, onBeforeUnmount, onMounted, watch } from "vue"
import { ref } from "vue"
import { NesVue, EmitErrorObj } from "nes-vue"
import { categorys } from "./game"
Expand Down Expand Up @@ -162,14 +163,25 @@ function nesErrorAlert(e: EmitErrorObj) {
errCode[e.code] && ElNotification.error(errCode[e.code])
}

const nesVueRef = ref()

const onFocus = () => nesVueRef.value?.play()
const onBlur = () => nesVueRef.value?.pause()

onMounted(() => {
window.addEventListener("resize", fullscreenHandler)
window.addEventListener("focus", onFocus)
window.addEventListener("blur", onBlur)
})

onBeforeUnmount(() => {
window.removeEventListener("resize", fullscreenHandler)
window.removeEventListener("focus", onFocus)
window.removeEventListener("blur", onBlur)
})

watch(curRom, fullscreenHandler)

const getCategory = (id: string) => categorys.find(i => i.id === id)?.name
</script>
<style lang="scss">
Expand Down Expand Up @@ -247,12 +259,9 @@ const getCategory = (id: string) => categorys.find(i => i.id === id)?.name

main {
display: flex;
flex-wrap: wrap;
.nes-vue {
// flex: 1;
margin: 0 1em 0 0 !important;
}
.list {
flex: 1;
}
}
</style>

0 comments on commit a3870ce

Please sign in to comment.