fixed error

This commit is contained in:
2024-09-20 00:18:47 +02:00
parent ea5e227b43
commit 1fe6c80b37
2 changed files with 33 additions and 35 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,3 +1,6 @@
import * as PIXI from "pixi.js";
import { Live2DModel } from "pixi-live2d-display";
function ap_init() { function ap_init() {
$(".aplayer-body").addClass("my-hide"); $(".aplayer-body").addClass("my-hide");
ap.lrc.hide(); //初始化时隐藏歌词 ap.lrc.hide(); //初始化时隐藏歌词
@@ -192,8 +195,7 @@ function loadWidget() {
} }
modelTexturesId = 0; // 材质ID modelTexturesId = 0; // 材质ID
} }
// loadModel(modelId, modelTexturesId); loadModel(modelId, modelTexturesId);
loadModelPixi(modelId, modelTexturesId);
fetch(`${live2d_path}waifu-tips.json`) fetch(`${live2d_path}waifu-tips.json`)
.then(response => response.json()) .then(response => response.json())
.then(registerEventListener); .then(registerEventListener);
@@ -211,7 +213,8 @@ function loadWidget() {
showMessage(message, 4000, 10); showMessage(message, 4000, 10);
if (!modelList) await loadModelList(); if (!modelList) await loadModelList();
let target = modelList.models[modelId]; let target = modelList.models[modelId];
loadlive2d("live2d", `${live2d_path}model/${target}/index.json`); // loadlive2d("live2d", `${live2d_path}model/${target}/index.json`);
loadModelPixi("live2d", `${live2d_path}model/${target}/index.json`);
console.log(`Live2D 模型 ${modelId}-${target} 加载完成`); console.log(`Live2D 模型 ${modelId}-${target} 加载完成`);
} }
@@ -219,40 +222,35 @@ function loadWidget() {
let modelId = localStorage.getItem("modelId"); let modelId = localStorage.getItem("modelId");
if (!modelList) await loadModelList(); if (!modelList) await loadModelList();
let index = (++modelId >= modelList.models.length) ? 0 : modelId; let index = (++modelId >= modelList.models.length) ? 0 : modelId;
// loadModel(index, 0, modelList.messages[index]); loadModel(index, 0, modelList.messages[index]);
loadModelPixi(index, 0, modelList.messages[index]);
} }
async function loadModelPixi(modelId, modelTexturesId, message) { window.PIXI = PIXI;
localStorage.setItem("modelId", modelId);
localStorage.setItem("modelTexturesId", modelTexturesId);
showMessage(message, 4000, 10);
if (!modelList) await loadModelList();
let target = modelList.models[modelId];
const jsonpath = `${live2d_path}model/${target}/index.json`;
const element = document.getElementById(modelId);
const app = new PIXI.Application({
view: element,
transparent: true,
});
const model = await Live2DModel.from(jsonpath);
app.stage.addChild(model); async function loadModelPixi(id, jsonpath) {
const element = document.getElementById(id);
const parentWidth = element.width; const app = new PIXI.Application({
const parentHeight = element.height; view: element,
// Scale to fit the stage transparent: true,
const ratio = Math.min( });
parentWidth / model.width, const model = await Live2DModel.from(jsonpath);
parentHeight / model.height
); app.stage.addChild(model);
model.scale.set(ratio, ratio);
// Align bottom and center horizontally const parentWidth = element.width;
const parentHeight = element.height;
model.x = (parentWidth - model.width) / 2; // Scale to fit the stage
model.y = parentHeight - model.height; const ratio = Math.min(
parentWidth / model.width,
} parentHeight / model.height
);
model.scale.set(ratio, ratio);
// Align bottom and center horizontally
model.x = (parentWidth - model.width) / 2;
model.y = parentHeight - model.height;
}
} }