55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
|
|
// Sound effects
|
||
|
|
import { DOM } from './dom.js';
|
||
|
|
|
||
|
|
// Play sound effect
|
||
|
|
export function playSound(type) {
|
||
|
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||
|
|
|
||
|
|
if (type === 'click') {
|
||
|
|
const oscillator = audioContext.createOscillator();
|
||
|
|
const gainNode = audioContext.createGain();
|
||
|
|
|
||
|
|
oscillator.connect(gainNode);
|
||
|
|
gainNode.connect(audioContext.destination);
|
||
|
|
|
||
|
|
oscillator.frequency.setValueAtTime(800, audioContext.currentTime);
|
||
|
|
oscillator.frequency.exponentialRampToValueAtTime(400, audioContext.currentTime + 0.1);
|
||
|
|
|
||
|
|
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime);
|
||
|
|
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.1);
|
||
|
|
|
||
|
|
oscillator.start(audioContext.currentTime);
|
||
|
|
oscillator.stop(audioContext.currentTime + 0.1);
|
||
|
|
} else if (type === 'hover') {
|
||
|
|
const oscillator = audioContext.createOscillator();
|
||
|
|
const gainNode = audioContext.createGain();
|
||
|
|
|
||
|
|
oscillator.connect(gainNode);
|
||
|
|
gainNode.connect(audioContext.destination);
|
||
|
|
|
||
|
|
oscillator.frequency.setValueAtTime(600, audioContext.currentTime);
|
||
|
|
oscillator.frequency.exponentialRampToValueAtTime(800, audioContext.currentTime + 0.05);
|
||
|
|
|
||
|
|
gainNode.gain.setValueAtTime(0.05, audioContext.currentTime);
|
||
|
|
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.05);
|
||
|
|
|
||
|
|
oscillator.start(audioContext.currentTime);
|
||
|
|
oscillator.stop(audioContext.currentTime + 0.05);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add sound effects to buttons
|
||
|
|
export function addSoundEffects() {
|
||
|
|
const buttons = DOM.getAll('.nav-button');
|
||
|
|
|
||
|
|
buttons.forEach((button) => {
|
||
|
|
button.addEventListener('click', function () {
|
||
|
|
playSound('click');
|
||
|
|
});
|
||
|
|
|
||
|
|
button.addEventListener('mouseenter', function () {
|
||
|
|
playSound('hover');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|