This commit is contained in:
2022-02-23 20:41:12 +01:00
parent c6461ddda9
commit b51d1eb1d2
2 changed files with 42 additions and 25 deletions

View File

@@ -62,8 +62,13 @@ const canvasSizing = (cnv) => {
resizeCanvas();
}
function waitFor(conditionFunction) {
const poll = resolve => {
if(conditionFunction()) resolve();
else setTimeout(_ => poll(resolve), 400);
}
return new Promise(poll);
}
@@ -95,6 +100,23 @@ $(async function () {
return loadedimages;
}
const loadImagesTwitch = async(emotesId) => {
let loadcount = 0, loadtotal = emotesId.length;
let localPreloaded = false;
for (let i=0; i<emotesId.length; i++) {
var image = new Image();
image.onload = function () {
loadcount++;
if (loadcount == loadtotal) localPreloaded = true;
};
image.src = "https://static-cdn.jtvnw.net/emoticons/v2/" + emotesId[i] + "/default/dark/2.0";
img_map[emotesId[i]] = image;
}
await waitFor(_ => localPreloaded === true);
return true;
}
const randEmote = (set) => {
if(!Array.isArray(set))
set = Object.keys(img_map) || []
@@ -299,21 +321,6 @@ $(async function () {
}, 100);
}
async function init() {
main(0);
sleep(1000).then(()=>{
createParticule(BounceGP, randEmote(), {}, {})
createParticule(BounceP, randEmote(), {}, {})
createParticule(BounceP, randEmote(), {}, {})
createParticule(BombP, randEmote(), {}, {})
createParticule(FireworkP, randEmote(), {}, {})
genRain();
genVolcano();
})
}
function main(tframe) {
window.requestAnimationFrame(main);
@@ -329,7 +336,17 @@ $(async function () {
}
}
init()
main(0);
const handleEmoteData = (d) =>{
let te = d.twitch, ce = d.custom.filter(e=> img_map[e]);
if(!te || te.length == 0){
return ce;
}else{
let cleanTE = te.map(e => e.split(":")[0]);
return ce.concat(loadImagesTwitch(cleanTE))
}
}
const handleSocketMessage = (e)=>{
try {