-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
176 lines (154 loc) · 5.81 KB
/
index.html
File metadata and controls
176 lines (154 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live2D Native Demo</title>
<style>
body {
margin: 20px;
padding: 0;
font-family: sans-serif;
}
.control-group {
margin-bottom: 10px;
}
.control-group span {
font-weight: bold;
margin-right: 10px;
}
button {
margin-right: 5px;
padding: 5px 10px;
cursor: pointer;
}
</style>
</head>
<body>
<!-- 交互控件,模仿 vue-live2d/example/App.vue 的布局 -->
<div style="margin: 2px 0; color: #666;">注意:如果有时候按钮不能点击,是因为模型 zIndex 挡住了按钮。</div>
<div class="control-group">
<span>position style:</span>
<button id="pos-center">呆在中间</button>
<button id="pos-tl">呆在左上角</button>
<button id="pos-tr">呆在右上角</button>
<button id="pos-bl">呆在左下角</button>
<button id="pos-br">呆在右下角</button>
<button id="pos-flow">跟随流</button>
</div>
<div class="control-group">
<span>direction:</span>
<button id="dir-right">呆在右边</button>
<button id="dir-left">呆在左边</button>
</div>
<div class="control-group">
<span>size:</span>
<button id="size-dec">减小</button>
<button id="size-inc">增大</button>
</div>
<!-- 挂件容器,初始为空,通过 JS 绑定 -->
<div id="live2d-widget-target"></div>
<!-- Scripts: 直接使用源码,配合 Vite 进行调试 -->
<script type="module">
import Live2dWidget from './src/index.js';
// 初始化变量,对应 App.vue data()
let direction = 'right';
let customStyle = ''; // 对应 vue 中的 style 变量
let size = 300; // 对应 vue 中的 size 变量
// 1. 初始化 Widget
let widget = null;
try {
// Widget 默认会插入到 el 中。如果 el 是 body,它通常是 fixed 的
// 模仿 App.vue,这里我们创建一个宿主元素
const targetEl = document.getElementById('live2d-widget-target');
widget = new Live2dWidget({
el: targetEl,
size: size,
direction: direction,
model: ['Potion-Maker/Pio', 'school-2017-costume-yellow'],
apiPath: 'https://cdn.jsdelivr.net/npm/live2d-static-api@latest/indexes',
tips: {
mouseover: [{
selector: 'button',
texts: ['点我试试看?', '要修改什么属性吗?']
}]
}
});
console.log('✅ Live2D Widget Ready');
} catch (e) {
console.error('❌ Init Error:', e);
}
// 2. 交互逻辑
// Helper: 更新 widget 的样式 prop (对应 vue 中的 :style 绑定)
function updateWidgetStyle() {
if (!widget || !widget.el) return;
// 仅应用 customStyle 内容,不重置所有样式 (由 widget 内部 Proxy 处理 size)
if (customStyle) {
widget.el.style.cssText += customStyle;
} else {
// Follow flow
widget.el.style.position = 'relative';
widget.el.style.bottom = 'auto';
widget.el.style.right = 'auto';
widget.el.style.left = 'auto';
widget.el.style.top = 'auto';
}
}
// Actions: 现在直接修改 options 即可触发响应式更新
const actions = {
// Position: 这个依然由外部样式控制 stage 容器
'pos-center': () => {
customStyle = 'position: fixed; top: calc(50% - 250px); left: calc(50% - 200px); bottom: auto; right: auto;';
updateWidgetStyle();
},
'pos-tl': () => {
customStyle = 'position: fixed; top: 0; left: 0; z-index: 1; bottom: auto; right: auto;';
updateWidgetStyle();
},
'pos-tr': () => {
customStyle = 'position: fixed; top: 0; right: 0; z-index: 1; bottom: auto; left: auto;';
updateWidgetStyle();
},
'pos-bl': () => {
customStyle = 'position: fixed; bottom: 0; left: 0; z-index: 1; top: auto; right: auto;';
updateWidgetStyle();
},
'pos-br': () => {
customStyle = 'position: fixed; bottom: 0; right: 0; top: auto; left: auto;';
updateWidgetStyle();
},
'pos-flow': () => {
customStyle = '';
updateWidgetStyle();
},
// Direction:使用 config 方法更新
'dir-right': () => {
widget.config({ direction: 'right' });
},
'dir-left': () => {
widget.config({ direction: 'left' });
},
// Size:使用 config 方法更新
'size-dec': () => {
size -= 100;
widget.config({ size: size });
updateWidgetStyle();
},
'size-inc': () => {
size += 100;
widget.config({ size: size });
updateWidgetStyle();
}
};
// Bind Events
Object.keys(actions).forEach(id => {
const btn = document.getElementById(id);
if (btn) {
btn.addEventListener('click', actions[id]);
}
});
// Init style behavior
updateWidgetStyle();
</script>
</body>
</html>