Skip to content

Commit

Permalink
style: better and generic scale
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww committed Dec 4, 2024
1 parent f6e41e6 commit 1ce61d7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/stage/src/components/Live2DViewer.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import type { InternalModel } from 'pixi-live2d-display/cubism4'
import { Application } from '@pixi/app'
import { extensions } from '@pixi/extensions'
import { Ticker, TickerPlugin } from '@pixi/ticker'
Expand Down Expand Up @@ -62,6 +63,22 @@ function getCoreModel() {
return model.value!.internalModel.coreModel as any
}
function setScale(model: Ref<Live2DModel<InternalModel> | undefined>) {
if (!model.value)
return
let offsetFactor = 2
if (isMobile.value) {
offsetFactor = 3.5
}
const heightScale = canvasHeight.value * 0.95 / model.value.height * offsetFactor
const widthScale = canvasWidth.value * 0.95 / model.value.width * offsetFactor
const scale = Math.min(heightScale, widthScale)
model.value.scale.set(scale, scale)
}
async function initLive2DPixiStage(parent: HTMLDivElement) {
// https://guansss.github.io/pixi-live2d-display/#package-importing
Live2DModel.registerTicker(Ticker)
Expand All @@ -84,8 +101,8 @@ async function initLive2DPixiStage(parent: HTMLDivElement) {
model.value.y = canvasHeight.value
model.value.rotation = Math.PI
model.value.skew.x = Math.PI
model.value.scale.set(0.3, 0.3)
model.value.anchor.set(0.5, 0.5)
setScale(model)
model.value.on('hit', (hitAreas) => {
if (model.value && hitAreas.includes('body'))
Expand Down

0 comments on commit 1ce61d7

Please sign in to comment.