Skip to content

Commit

Permalink
refactor: improve audio and progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Jan 5, 2025
1 parent 1351b7e commit 3a9a139
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
1 change: 0 additions & 1 deletion js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { comment } from './comment/comment.js';

document.addEventListener('DOMContentLoaded', () => {
guest.init();
window.AOS.init();

window.util = util;
window.guest = guest;
Expand Down
19 changes: 14 additions & 5 deletions js/guest/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,51 @@ export const audio = (() => {
const statePlay = '<i class="fa-solid fa-circle-pause spin-button"></i>';
const statePause = '<i class="fa-solid fa-circle-play"></i>';

/**
* @returns {Promise<void>}
*/
const play = async () => {
music.disabled = true;
try {
await audio.play();
isPlay = true;
music.disabled = false;
music.innerHTML = statePlay;
} catch (err) {
isPlay = false;
alert(err);
}
music.disabled = false;
};

/**
* @returns {void}
*/
const pause = () => {
isPlay = false;
audio.pause();
music.innerHTML = statePause;
};

/**
* @returns {void}
*/
const init = () => {
music = document.getElementById('button-music');

audio = new Audio(music.getAttribute('data-url'));
audio.volume = 1;
audio.loop = true;
audio.muted = false;
audio.currentTime = 0;
audio.autoplay = false;
audio.muted = false;
audio.loop = true;
audio.volume = 1;
audio.controls = false;

play();
music.addEventListener('offline', pause);
music.addEventListener('click', () => isPlay ? pause() : play());
};

return {
init,
play,
};
})();
2 changes: 1 addition & 1 deletion js/guest/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export const guest = (() => {
opacity('welcome', 0.025);

audio.init();
audio.play();
theme.spyTop();

util.timeOut(animation, 1500);
Expand Down Expand Up @@ -200,6 +199,7 @@ export const guest = (() => {
theme.init();
session.init();
offline.init();
window.AOS.init();

normalize();
countDownDate();
Expand Down
36 changes: 31 additions & 5 deletions js/guest/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,60 @@ export const progress = (() => {
let valid = true;
let push = true;

/**
* @returns {void}
*/
const onComplete = () => {
guest.name();
};

/**
* @returns {void}
*/
const add = () => {
if (push) {
total += 1;
}
};

/**
* @returns {string}
*/
const showInformation = () => {
return `(${loaded}/${total}) [${parseInt((loaded / total) * 100).toFixed(0)}%]`;
};

/**
* @param {string} type
* @returns {void}
*/
const complete = (type) => {
if (!valid) {
return;
}

loaded += 1;
info.innerText = `Loading ${type} complete ${showInformation()}`;
bar.style.width = Math.min((loaded / total) * 100, 100).toString() + '%';
info.innerText = `Loading ${type} complete (${loaded}/${total}) [${parseInt((loaded / total) * 100).toFixed(0)}%]`;

if (loaded === total) {
onComplete();
}
};

/**
* @param {string} type
* @returns {void}
*/
const invalid = (type) => {
info.innerText = `Error loading ${type} (${loaded}/${total}) [${parseInt((loaded / total) * 100).toFixed(0)}%]`;
bar.style.backgroundColor = 'red';
valid = false;
bar.style.backgroundColor = 'red';
info.innerText = `Error loading ${type} ${showInformation()}`;
};

/**
* @returns {Promise<void>}
*/
const run = async () => {
document.querySelectorAll('img').forEach((asset) => {
asset.onerror = () => {
Expand All @@ -64,13 +88,15 @@ export const progress = (() => {
});
};

/**
* @returns {void}
*/
const init = () => {
document.querySelectorAll('img').forEach(add);

info = document.getElementById('progress-info');
bar = document.getElementById('progress-bar');
info.style.display = 'block';

document.querySelectorAll('img').forEach(add);
push = false;
run();
};
Expand Down

0 comments on commit 3a9a139

Please sign in to comment.