dsh-xinqi-live2d 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +25 -5
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -228,8 +228,9 @@ const canvas = document.getElementById('c');
|
|
|
228
228
|
|
|
229
229
|
function show(msg, isErr, retry) {
|
|
230
230
|
tip.innerHTML = '';
|
|
231
|
-
const s = document.createElement('
|
|
231
|
+
const s = document.createElement('pre');
|
|
232
232
|
if (isErr) s.className = 'e';
|
|
233
|
+
s.style.cssText = 'margin:0;white-space:pre-wrap;word-break:break-all;font:inherit;text-align:left';
|
|
233
234
|
s.textContent = msg;
|
|
234
235
|
tip.appendChild(s);
|
|
235
236
|
if (retry) {
|
|
@@ -243,12 +244,19 @@ function show(msg, isErr, retry) {
|
|
|
243
244
|
function hide() { tip.style.display = 'none'; }
|
|
244
245
|
|
|
245
246
|
/* 加载外部脚本 */
|
|
247
|
+
let STEP = 'init';
|
|
246
248
|
function loadScript(src) {
|
|
247
249
|
return new Promise((res, rej) => {
|
|
248
250
|
const el = document.createElement('script');
|
|
249
251
|
el.src = src;
|
|
250
252
|
el.onload = res;
|
|
251
|
-
el.onerror = () =>
|
|
253
|
+
el.onerror = (ev) => {
|
|
254
|
+
// 带上 HTTP 状态线索:能加载但脚本报错时 onerror 也会来
|
|
255
|
+
const msg = '脚本加载失败: ' + src.replace(window.location.origin, '');
|
|
256
|
+
const e = new Error(msg);
|
|
257
|
+
e.detail = '步骤=' + STEP + ' readyState=' + (el.readyState || '-');
|
|
258
|
+
rej(e);
|
|
259
|
+
};
|
|
252
260
|
document.head.appendChild(el);
|
|
253
261
|
});
|
|
254
262
|
}
|
|
@@ -257,10 +265,12 @@ let app = null, model = null;
|
|
|
257
265
|
|
|
258
266
|
async function boot() {
|
|
259
267
|
show('加载引擎…');
|
|
268
|
+
STEP = 'cubism-core';
|
|
260
269
|
// ① Cubism Core(必须先于 display)
|
|
261
270
|
if (!window.Live2DCubismCore) {
|
|
262
271
|
await loadScript('./vendor/live2dcubismcore.min.js');
|
|
263
272
|
}
|
|
273
|
+
STEP = 'pixi';
|
|
264
274
|
// ② pixi 7 —— 用 **UMD** 版(挂到 window.PIXI)。
|
|
265
275
|
// 不能用 .mjs:cubism4 的 UMD 构建只认 window.PIXI,
|
|
266
276
|
// 而 cubism4 的 .es.js 又引用了 @pixi/utils 这类裸模块名,浏览器解析不了。
|
|
@@ -269,21 +279,25 @@ async function boot() {
|
|
|
269
279
|
}
|
|
270
280
|
if (!window.PIXI) throw new Error('pixi 加载失败');
|
|
271
281
|
|
|
282
|
+
STEP = 'display';
|
|
272
283
|
// ③ display 的 cubism4 **UMD** 构建(依赖已内联,且会挂到 window.PIXI.live2d)
|
|
273
284
|
await loadScript('./vendor/cubism4.min.js');
|
|
274
285
|
const L2D = window.PIXI?.live2d;
|
|
275
286
|
if (!L2D || typeof L2D.Live2DModel?.from !== 'function') {
|
|
276
287
|
throw new Error('pixi-live2d-display 未就绪');
|
|
277
288
|
}
|
|
289
|
+
STEP = 'startup';
|
|
278
290
|
// ④ 显式启动框架
|
|
279
291
|
if (typeof L2D.startUpCubism4 === 'function') await L2D.startUpCubism4();
|
|
280
292
|
const pixi = window.PIXI;
|
|
281
293
|
|
|
294
|
+
STEP = 'info';
|
|
282
295
|
// ⑤ 拿模型路径
|
|
283
296
|
const info = await fetch('./info', { cache: 'no-store' }).then(r => r.json());
|
|
284
297
|
const file = info?.value?.model?.file;
|
|
285
298
|
if (!file) throw new Error('服务器上没找到模型');
|
|
286
299
|
|
|
300
|
+
STEP = 'model';
|
|
287
301
|
show('加载模型…');
|
|
288
302
|
app = new pixi.Application({
|
|
289
303
|
view: canvas,
|
|
@@ -321,9 +335,15 @@ async function boot() {
|
|
|
321
335
|
}
|
|
322
336
|
|
|
323
337
|
boot().catch(e => {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
338
|
+
let msg = '⚠ ';
|
|
339
|
+
if (e && e.message) msg += e.message;
|
|
340
|
+
else if (typeof e === 'string') msg += e;
|
|
341
|
+
else if (e && e.name) msg += e.name;
|
|
342
|
+
else msg += '未知异常(' + Object.prototype.toString.call(e) + ')';
|
|
343
|
+
msg += ['', '[' + STEP + ']'].join(String.fromCharCode(10));
|
|
344
|
+
if (e && e.detail) msg += ' ' + e.detail;
|
|
345
|
+
show(msg, true, true);
|
|
346
|
+
try { console.error('[live2d] step=' + STEP, e); } catch {}
|
|
327
347
|
});
|
|
328
348
|
</script></body></html>`
|
|
329
349
|
}
|