/**
* 載入畫面(index.html 的 #splash):首頁等 3D 小鎮搭好才淡出,其他畫面一顯示就淡出。
* 首頁的主視覺用 holdSplash() 擋住,搭好或離開首頁時放開;最多擋 8 秒,免得卡在載入畫面。
*/
const MAX_MS = 8000;
let holders = 0;
const el = () => document.getElementById("splash");
export function splashProgress(text: string, fraction: number) {
const s = el();
if (!s) return;
const fill = s.querySelector(".splash-fill");
const label = s.querySelector(".splash-text");
if (fill) fill.style.width = `${Math.round(8 + Math.min(1, Math.max(0, fraction)) * 92)}%`;
if (label) label.textContent = text;
}
export function hideSplash() {
const s = el();
if (!s || s.classList.contains("is-done")) return;
s.classList.add("is-done");
window.setTimeout(() => s.remove(), 350);
}
/** 先別淡出(首頁的 3D 小鎮還在搭);回傳放開的函式 */
export function holdSplash(): () => void {
holders++;
let released = false;
return () => {
if (released) return;
released = true;
holders--;
releaseSplashIfFree();
};
}
/** 沒有人擋著就淡出(每個畫面顯示時呼叫) */
export function releaseSplashIfFree() {
// 晚一拍再看:React 開發模式會先卸載再重新掛上,中間那一下不能淡出
window.setTimeout(() => holders === 0 && hideSplash(), 0);
}
window.setTimeout(hideSplash, MAX_MS);