-
Notifications
You must be signed in to change notification settings - Fork 6
/
autoload.js
46 lines (44 loc) · 1.44 KB
/
autoload.js
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
const live2d_path = "https://jsd.onmicrosoft.cn/gh/wuuconix/[email protected]/";
const modelTextures = "https://jsd.onmicrosoft.cn/gh/wuuconix/[email protected]/model/xiaomai/umaru2048/texture_00.png";
// 封装异步加载资源的方法
function loadExternalResource(url, type) {
return new Promise((resolve, reject) => {
let tag;
if (type === "css") {
tag = document.createElement("link");
tag.rel = "stylesheet";
tag.href = url;
}
else if (type === "js") {
tag = document.createElement("script");
tag.src = url;
} else if (type === "png") {
tag = new Image()
tag.src = url;
}
if (tag) {
tag.onload = () => resolve(url);
tag.onerror = () => reject(url);
if (["js", "css"].includes(type)) {
document.head.appendChild(tag);
}
}
});
}
// 加载 waifu.css live2d.min.js waifu-tips.js
if (screen.width >= 768) {
loadExternalResource(modelTextures, "png").then(() => { // 首先确保模型图片已经加载完成
Promise.all([
loadExternalResource(live2d_path + "waifu.css", "css"),
loadExternalResource(live2d_path + "live2d.min.js", "js"),
loadExternalResource(live2d_path + "waifu-tips.js", "js")
]).then(() => {
// 配置选项的具体用法见 https://github.com/stevenjoezhang/live2d-widget/blob/master/README.md
initWidget({
waifuPath: live2d_path + "waifu-tips.json",
cdnPath: live2d_path,
tools: ["hitokoto", "asteroids", "photo", "info", "quit"]
});
});
});
}