Skip to content

Commit ae0fb1b

Browse files
author
Chris Hayes
committed
🐛 Fix de-sync issue.
1 parent 7252f3a commit ae0fb1b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

main.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ function playClick() {
4444
console.log('Play clicked');
4545
if (ready) {
4646
document.body.classList.add('play');
47-
startPlaying();
47+
requestPlay();
4848
ready = false;
4949
}
5050
}
5151

52+
// CRT scanning visual animation
5253
function paintScreen() {
5354
// Fill with gradient
5455
ctx.clearRect(0, 0, c.width, c.height);
@@ -67,14 +68,20 @@ function paintScreen() {
6768
setTimeout(paintScreen, scanY == c.height - 1 ? delay : interval);
6869
}
6970

70-
// start when music is loaded
71-
function startPlaying() {
72-
console.log('Start playing..');
73-
initCursor();
74-
document.getElementById( "music" ).play();
75-
initLyrics();
76-
initCredits();
71+
// Request play
72+
function requestPlay() {
73+
const audioElem = document.querySelector('#music');
74+
const startAnimation = function() {
75+
initCursor();
76+
initLyrics();
77+
initCredits();
78+
};
79+
audioElem.onplay = startAnimation;
80+
// Request audio to start playing
81+
audioElem.play();
7782
}
83+
84+
7885
/* LYRICS */
7986
var lyricsIndex = 0;
8087
function initLyrics() {

0 commit comments

Comments
 (0)