res/script.js (view raw)
1const iframe = document.getElementById("content-iframe");
2const music = document.getElementById("music");
3const source = document.getElementById("music-source");
4const music_base = document.getElementById("music-player-base");
5const music_song = document.getElementById("music-player-song");
6const music_song_text = document.getElementById("song-text");
7const clickAudio1 = new Audio(url="res/sfx/os/click2.ogg");
8const clickAudio2 = new Audio(url="res/sfx/os/click1.ogg");
9const loadSpeed = 2;
10
11const tracks = {
12 "awesome": {
13 file_name: "AwesomeRapBeat.opus",
14 track_name: "Awesome Rap Beat",
15 track_author: "Jay Tholen",
16 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
17 },
18 "dream": {
19 file_name: "WeFellIntoaDream.opus",
20 track_name: "We Fell Into a Dream",
21 track_author: "Jay Tholen",
22 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
23 },
24 "earmint": {
25 file_name: "Earmint.opus",
26 track_name: "Earmint",
27 track_author: "Jay Tholen",
28 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
29 },
30 "forest": {
31 file_name: "ForestFeast.opus",
32 track_name: "Forest Feast",
33 track_author: "Jay Tholen",
34 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
35 },
36 "gtrock": {
37 file_name: "GoodtimeRock.opus",
38 track_name: "Goodtime Rock",
39 track_author: "Jay Tholen",
40 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
41 },
42 "katie": {
43 file_name: "KatiesKuddlyKatKorner.opus",
44 track_name: "Katie's Kuddly Kat Korner",
45 track_author: "Jay Tholen",
46 track_from: "Hypnospace Outlaw Original Soundtrack, Vol. 2"
47 },
48}
49
50const page_tracks = {
51 "about": "awesome",
52 "anime": "dream",
53 "dragonquest": "forest",
54 "earthbound": "gtrock",
55 "gf": "katie",
56 "indies": "earmint",
57 "zelda": "forest",
58}
59
60let first = false;
61let play = false;
62let controlsEnabled = false;
63let currentPage = "about";
64let currentTrack = "";
65let showTimeout, hideTimeout;
66
67function firstPlay(){
68 if(first) return;
69 document.getElementById("music-player").style.bottom = "0";
70 music.volume = 0.1;
71 controlsEnabled = true;
72 playPause(false, page_tracks[currentPage]);
73 first = true;
74}
75
76function loadIFrame(callback){
77 iframe.contentWindow.start_loading(speed=loadSpeed, not_allowed, callback);
78}
79
80function firstClickPlay(element) {
81 const overlay = document.getElementById("overlay");
82 if(overlay) {
83 overlay.remove();
84 hide_all();
85 start_loading(speed=loadSpeed, not_allowed, () => { loadIFrame(firstPlay) });
86 //slow_load(speed=loadSpeed, not_allowed, () => { loadIFrame(firstPlay) });
87 }
88
89 click1 = clickAudio1 || parent.clickAudio1;
90 click2 = clickAudio2 || parent.clickAudio2;
91
92 playAudio(click1)
93 element.onmousedown = () => playAudio(click1);
94 element.onmouseup = () => playAudio(click2);
95}
96
97function frameLoadedCallback() {
98 controlsEnabled = true;
99 playPause(!play, page_tracks[currentPage]);
100}
101
102function goToPage(page) {
103 if (!controlsEnabled || currentPage == page) return
104
105 music.pause()
106 controlsEnabled = false;
107 iframe.src = "pages/" + page + ".html";
108 currentPage = page;
109
110}
111
112function formatTrack(track) {
113 return `${track.track_author} - ${track.track_name} (${track.track_from})`;
114}
115
116function showTitle(){
117 if (!currentTrack) return;
118 hideTitle();
119 clearTimeout(showTimeout);
120 clearTimeout(hideTimeout);
121
122 showTimeout = setTimeout(() => {
123 music_song_text.textContent = formatTrack(tracks[currentTrack]);
124 music_song.style.top = "0";
125 }, 400);
126
127 hideTimeout = setTimeout(() => hideTitle(), 3_400);
128}
129
130function hideTitle(){
131 music_song.style.top = "100%";
132}
133
134function playPause(toggle=play, track=currentTrack) {
135 let changed = false;
136 if (track != currentTrack) {
137 changed = true;
138 currentTrack = track;
139 source.src = "res/mus/" + tracks[currentTrack].file_name;
140 music.load();
141 }
142
143 if (!controlsEnabled)
144 return;
145 if (toggle){
146 music_base.classList.remove("player-play");
147 music.pause();
148 play = false
149 hideTitle();
150 }
151 else {
152 music_base.classList.add("player-play");
153 music.play().catch(() => {});
154 if(changed)
155 showTitle();
156 play = true
157 }
158}