-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move credits to a separate component
- Loading branch information
Showing
4 changed files
with
117 additions
and
124 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,115 @@ | ||
<div class="grid grid-cols-2 grid-rows-2 h-screen"> | ||
<div class="bg-black text-credits-orange row-span-2" id="lyrics"></div> | ||
<div class="bg-lime-600 text-credits-orange" id="credits"></div> | ||
<div class="bg-fuchsia-800 text-credits-orange"> | ||
<pre id="art" class="leading-[1.2]"></pre> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
import { lyrics, type Lyric } from "./lyrics"; | ||
import { ASCII_ART } from "./ascii_art"; | ||
|
||
const audio = new Audio("/portal_still_alive.mp3"); | ||
|
||
const lyricsDiv = document.getElementById("lyrics")! as HTMLDivElement; | ||
|
||
const creditsDiv = document.getElementById("credits")! as HTMLDivElement; | ||
|
||
const artDiv = document.getElementById("art")! as HTMLDivElement; | ||
|
||
audio.addEventListener("canplaythrough", () => { | ||
const startTime = performance.now(); | ||
|
||
let expectedTime = 0; | ||
let lyricIndex = 0; | ||
|
||
let songStarted = false; | ||
let previousLineDone = true; | ||
|
||
function playSong() { | ||
const elasped = performance.now() - startTime; | ||
|
||
if (!songStarted && elasped >= 6850) { | ||
audio.play(); | ||
songStarted = true; | ||
|
||
console.log(`Started playing audio after ${elasped}ms`); | ||
} | ||
|
||
if (previousLineDone && elasped >= expectedTime) { | ||
const lyric = lyrics[lyricIndex]; | ||
|
||
if (lyric.asciiArt !== undefined) { | ||
artDiv.innerText = ASCII_ART[lyric.asciiArt]; | ||
} | ||
|
||
drawLine(lyric, elasped - expectedTime).then(() => { | ||
previousLineDone = true; | ||
}); | ||
|
||
expectedTime += lyric.duration; | ||
lyricIndex++; | ||
previousLineDone = false; | ||
} | ||
|
||
window.requestAnimationFrame(playSong); | ||
} | ||
|
||
function drawLine(lyric: Lyric, deviation: number): Promise<void> { | ||
switch (lyric.line) { | ||
case " ": | ||
return new Promise((resolve) => | ||
setTimeout(resolve, lyric.duration - deviation), | ||
); | ||
case "* ": | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
lyricsDiv.innerText += " "; | ||
resolve(); | ||
}, lyric.duration - deviation); | ||
}); | ||
case "&": | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
lyricsDiv.innerText = ""; | ||
resolve(); | ||
}, lyric.duration - deviation); | ||
}); | ||
case "^": | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
lyricsDiv.innerText += "\n"; | ||
resolve(); | ||
}, lyric.duration - deviation); | ||
}); | ||
|
||
default: | ||
return new Promise((resolve) => { | ||
let charIndex = 0; | ||
|
||
let id = setInterval( | ||
() => { | ||
const char = lyric.line[charIndex]; | ||
|
||
lyricsDiv.innerHTML += char; | ||
charIndex++; | ||
|
||
if (charIndex >= lyric.line.length) { | ||
if (!lyric.sameLine) { | ||
lyricsDiv.innerText += "\n"; | ||
} | ||
|
||
clearInterval(id); | ||
resolve(); | ||
} | ||
}, | ||
(lyric.duration - deviation) / lyric.line.length, | ||
); | ||
}); | ||
} | ||
} | ||
|
||
window.requestAnimationFrame(playSong); | ||
}); | ||
</script> |
File renamed without changes.
File renamed without changes.
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