-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (211 loc) · 4.15 KB
/
Copy pathscript.js
File metadata and controls
219 lines (211 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const data = [
{
text: "Here's to the crazy ones,",
type: "word",
startTime: 3.79,
},
{
text: "the misfits,",
type: "word",
startTime: 6.53,
},
{
text: "the rebels,",
type: "word",
startTime: 8.27,
},
{
text: "the troublemakers,",
type: "word",
startTime: 10.13,
},
{
text: "the round pegs",
type: "word",
startTime: 12.17,
},
{
text: "in the square holes...",
type: "word",
startTime: 13.31,
},
{
text: "the ones ",
type: "word",
startTime: 16.05,
},
{
text: "who see things",
type: "word",
startTime: 16.71,
},
{
text: "differently —",
type: "word",
startTime: 17.46,
},
{
text: "they're not fond of rules...",
type: "word",
startTime: 19.3,
},
{
text: "and they",
type: "word",
startTime: 20.41,
},
{
text: "have no respect",
type: "word",
startTime: 20.97,
},
{
text: "for the status quo.",
type: "word",
startTime: 21.91,
},
{
text: "You can quote them,",
type: "word",
startTime: 24.59,
},
{
text: "disagree with them,",
type: "word",
startTime: 26.0,
},
{
text: "glorify",
type: "word",
startTime: 27.55,
},
{
text: "or vilify them.",
type: "word",
startTime: 28.37,
},
{
text: "About the only thing",
type: "word",
startTime: 30.53,
},
{
text: "you can't do",
type: "word",
startTime: 31.6,
},
{
text: "is ignore them",
type: "word",
startTime: 32.58,
},
{
text: "because they change things...",
type: "word",
startTime: 34.14,
},
{
text: "They push",
type: "word",
startTime: 36.36,
},
{
text: "the human race forward,",
type: "word",
startTime: 37.1,
},
{
text: "while some may see",
type: "word",
startTime: 40.49,
},
{
text: "them as the crazy ones,",
type: "word",
startTime: 41.5,
},
{
text: "we",
type: "word",
startTime: 44.27,
},
{
text: "see",
type: "word",
startTime: 44.91,
},
{
text: "genius,",
type: "word",
startTime: 45.21,
},
{
text: "because",
type: "word",
startTime: 47.45,
},
{
text: "the people who are crazy enough",
type: "word",
startTime: 47.9,
},
{
text: "to think they",
type: "word",
startTime: 49.23,
},
{
text: "can change the world,",
type: "word",
startTime: 49.74,
},
{
text: "are the ones who do.",
type: "word",
startTime: 52.49,
},
]
const playButton = document.getElementById("playButton")
const audioPlayer = document.querySelector(".player")
const lyricsContainer = document.querySelector(".lyricsContainer")
// Add click event listener to the play button
playButton.addEventListener("click", () => {
// Hide the play button when click on
playButton.classList.add("hide")
// Play the audio
audioPlayer.play()
})
// Create a map to associate each line with its corresponding start time
const syncData = data.map((line) => ({
start: line.startTime,
text: line.text,
}))
// Generate the <span> tag for each line and append it to the lyrics div
syncData.forEach((item) => {
const spanTag = document.createElement("span")
spanTag.innerText = item.text
lyricsContainer.appendChild(spanTag)
})
audioPlayer.addEventListener("timeupdate", () => {
// Get the current time of the audio player
const currentTime = audioPlayer.currentTime
// Toggle the "active" class for each span tag based on the current time
syncData.forEach((item, index) => {
const spanTag = lyricsContainer.children[index]
if (currentTime >= item.start) {
spanTag.classList.add("active")
}
})
})
// Add event listener to:
// - remove the active class when the audio finishes playing
// - reset the button when the audio finishes playing
audioPlayer.addEventListener("ended", () => {
syncData.forEach((_, index) => {
const spanTag = lyricsContainer.children[index]
spanTag.classList.remove("active")
})
playButton.classList.remove("hide")
})
// Prevent right click on mouse
document.addEventListener("contextmenu", (e) => e.preventDefault(), false)