Files
dsProject/dsLightRag/static/YunXiao/live2d_widget.js
2025-08-28 15:39:13 +08:00

74 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 看板娘模型配置(基于原始代码提取)
const models = {
shizuku: { jsonPath: "https://unpkg.com/live2d-widget-model-shizuku@1.0.5/assets/shizuku.model.json", name: "小智" },
koharu: { jsonPath: "https://unpkg.com/live2d-widget-model-koharu@1.0.5/assets/koharu.model.json", name: "小荷" },
wanko: { jsonPath: "https://unpkg.com/live2d-widget-model-wanko@1.0.5/assets/wanko.model.json", name: "汪喵" }
};
// 获取URL参数工具函数
function getUrlParam(name) {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
const r = window.location.search.substr(1).match(reg);
return r ? unescape(r[2]) : null;
}
// 看板娘初始化函数
function initL2Dwidget() {
const modelId = getUrlParam('id') || 'koharu';
const model = models[modelId] || models.koharu;
// 设置下拉框选中状态
if (document.getElementById('model-select')) {
document.getElementById('model-select').value = modelId;
}
console.log('加载模型:', model.name, model.jsonPath);
// 初始化L2Dwidget保留原始配置
L2Dwidget.init({
"model": { "jsonPath": model.jsonPath, "scale": 1 },
"display": {
"position": "right",
"width": 150,
"height": 300,
"hOffset": 0,
"vOffset": -20
},
"mobile": { "show": true, "scale": 0.5 },
"react": { "opacityDefault": 0.8, "opacityOnHover": 1 },
"dialog": {
"enable": true,
"script": {
'tap body': `你好啊,我是${model.name}`,
'tap face': '有什么问题或者烦心事都可以和我聊聊~'
},
"hitokoto": false
}
});
// 初始化完成后延迟显示欢迎消息
setTimeout(() => {
if (window.L2Dwidget && window.L2Dwidget.dialog) {
window.L2Dwidget.dialog.show(`你好啊,我是${model.name}`);
}
}, 1000);
}
// 页面加载完成后初始化看板娘
window.addEventListener('load', function() {
initL2Dwidget();
// 为学伴选择器绑定change事件监听器
const modelSelect = document.getElementById('model-select');
if (modelSelect) {
modelSelect.addEventListener('change', function() {
const selectedModelId = this.value;
console.log('用户选择学伴:', selectedModelId);
switchL2DModel(selectedModelId);
});
}
});
// 暴露模型切换功能接口
window.switchL2DModel = function(modelId) {
window.location.search = '?id=' + modelId;
};