-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (78 loc) · 2.71 KB
/
script.js
File metadata and controls
85 lines (78 loc) · 2.71 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
//Audio
const track = document.getElementById('track');
const trackList = [
'sounds/trackGayageum.mp3', //0
'sounds/trackGeomungo.mp3', //1
'sounds/trackAjaeng.mp3', //2
'sounds/trackHaegeum.mp3', //3
'sounds/trackHyangBipa.mp3', //4
'sounds/trackJanggo.mp3', //5
'sounds/trackJing.mp3', //6
'sounds/trackBuk.mp3', //7
'sounds/trackKkwaenggwari.mp3', //8
'sounds/trackSogo.mp3', //9
'sounds/trackDaegeum.mp3', //10
'sounds/trackTaepyeongso.mp3', //11
'sounds/trackDanso.mp3', //12
'sounds/trackHyangPiri.mp3' //13
];
// Instrument mapping
const instruments = [
{ name: 'gayageum', index: 0, icon: 'iconGayageum' },
{ name: 'geomungo', index: 1, icon: 'iconGeomungo' },
{ name: 'ajaeng', index: 2, icon: 'iconAjaeng' },
{ name: 'haegeum', index: 3, icon: 'iconHaegeum' },
{ name: 'hyangbipa', index: 4, icon: 'iconBipa' },
{ name: 'janggo', index: 5, icon: 'iconJanggo' },
{ name: 'jing', index: 6, icon: 'iconJing' },
{ name: 'buk', index: 7, icon: 'iconBuk' },
{ name: 'kkwaenggwari', index: 8, icon: 'iconKkwaenggwari' },
{ name: 'sogo', index: 9, icon: 'iconSogo' },
{ name: 'daegeum', index: 10, icon: 'iconDaegeum' },
{ name: 'taepyeongso', index: 11, icon: 'iconTaepyongso' },
{ name: 'danso', index: 12, icon: 'iconDanso' },
{ name: 'hyangpiri', index: 13, icon: 'iconHyangPiri' }
];
const playPause = (i) => {
if (track.paused) {
track.src = trackList[i];
track.play();
}
else if (trackList.indexOf(track.src) === i) {
track.pause();
}
else {
track.src = trackList[i];
track.play();
}
};
const swapIcon = (iconID) => {
if (!track.paused) {
document.getElementById(iconID).setAttribute('href','#icon-pause');
instruments.forEach(instrument => {
if (instrument.icon !== iconID) {
document.getElementById(instrument.icon).setAttribute('href', '#icon-play');
}
});
}
else {
document.getElementById(iconID).setAttribute('href','#icon-play');
}
};
// Swap all icons to "Play" when track is ended
track.addEventListener('ended', e => {
instruments.forEach(instrument => {
document.getElementById(instrument.icon).setAttribute('href', '#icon-play');
});
});
// Event delegation for instrument clicks
document.getElementById('grid').addEventListener('click', (e) => {
const gridItem = e.target.closest('.grid-item');
if (!gridItem) return;
const instrumentName = gridItem.dataset.instrument;
const instrument = instruments.find(inst => inst.name === instrumentName);
if (instrument) {
playPause(instrument.index);
swapIcon(instrument.icon);
}
});