forked from stevenjoezhang/live2d-widget
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1eb4ca
commit edbf98e
Showing
4 changed files
with
101 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import randomSelection from "./utils.js"; | ||
|
||
let messageTimer; | ||
|
||
function showMessage(text, timeout, priority) { | ||
if (!text || (sessionStorage.getItem("waifu-text") && sessionStorage.getItem("waifu-text") > priority)) return; | ||
if (messageTimer) { | ||
clearTimeout(messageTimer); | ||
messageTimer = null; | ||
} | ||
text = randomSelection(text); | ||
sessionStorage.setItem("waifu-text", priority); | ||
const tips = document.getElementById("waifu-tips"); | ||
tips.innerHTML = text; | ||
tips.classList.add("waifu-tips-active"); | ||
messageTimer = setTimeout(() => { | ||
sessionStorage.removeItem("waifu-text"); | ||
tips.classList.remove("waifu-tips-active"); | ||
}, timeout); | ||
} | ||
|
||
export default showMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import showMessage from "./message.js"; | ||
import randomSelection from "./utils.js"; | ||
|
||
class Model { | ||
constructor(useCDN, apiPath, cdnPath) { | ||
this.useCDN = useCDN; | ||
this.apiPath = apiPath; | ||
this.cdnPath = cdnPath; | ||
} | ||
|
||
async loadModelList() { | ||
const response = await fetch(`${this.cdnPath}model_list.json`); | ||
this.modelList = await response.json(); | ||
} | ||
|
||
async loadModel(modelId, modelTexturesId, message) { | ||
localStorage.setItem("modelId", modelId); | ||
localStorage.setItem("modelTexturesId", modelTexturesId); | ||
showMessage(message, 4000, 10); | ||
if (this.useCDN) { | ||
if (!this.modelList) await this.loadModelList(); | ||
const target = randomSelection(this.modelList.models[modelId]); | ||
loadlive2d("live2d", `${this.cdnPath}model/${target}/index.json`); | ||
} else { | ||
loadlive2d("live2d", `${this.apiPath}get/?id=${modelId}-${modelTexturesId}`); | ||
console.log(`Live2D 模型 ${modelId}-${modelTexturesId} 加载完成`); | ||
} | ||
} | ||
|
||
async loadRandModel() { | ||
const modelId = localStorage.getItem("modelId"), | ||
modelTexturesId = localStorage.getItem("modelTexturesId"); | ||
if (this.useCDN) { | ||
if (!this.modelList) await this.loadModelList(); | ||
const target = randomSelection(this.modelList.models[modelId]); | ||
loadlive2d("live2d", `${this.cdnPath}model/${target}/index.json`); | ||
showMessage("我的新衣服好看嘛?", 4000, 10); | ||
} else { | ||
// 可选 "rand"(随机), "switch"(顺序) | ||
fetch(`${this.apiPath}rand_textures/?id=${modelId}-${modelTexturesId}`) | ||
.then(response => response.json()) | ||
.then(result => { | ||
if (result.textures.id === 1 && (modelTexturesId === 1 || modelTexturesId === 0)) showMessage("我还没有其他衣服呢!", 4000, 10); | ||
else this.loadModel(modelId, result.textures.id, "我的新衣服好看嘛?"); | ||
}); | ||
} | ||
} | ||
|
||
async loadOtherModel() { | ||
let modelId = localStorage.getItem("modelId"); | ||
if (this.useCDN) { | ||
if (!this.modelList) await this.loadModelList(); | ||
const index = (++modelId >= this.modelList.models.length) ? 0 : modelId; | ||
this.loadModel(index, 0, this.modelList.messages[index]); | ||
} else { | ||
fetch(`${this.apiPath}switch/?id=${modelId}`) | ||
.then(response => response.json()) | ||
.then(result => { | ||
this.loadModel(result.model.id, 0, result.model.message); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default Model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function randomSelection(obj) { | ||
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj; | ||
} | ||
|
||
export default randomSelection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters