work.suroh.tk/assets/js/interaction.js

81 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-12-02 12:22:45 +00:00
import {Synth} from '/assets/js/synth.js'
// audio for events
// AUDIO SETUP
const audioEngine = new (window.AudioContext || window.webkitAudioContext)()
audioEngine.suspend()
const masterGain = audioEngine.createGain()
const audioEl = document.querySelector('#audioToggle')
masterGain.gain.value = 0.9
masterGain.connect(audioEngine.destination)
const audioToggle = () => {
audioEl.classList.toggle('active');
if (audioEngine.state == 'suspended') {
console.log('resumed')
audioEngine.resume()
} else {
audioEngine.suspend()
console.log('suspended')
}
}
audioEl.addEventListener('click', audioToggle)
// NAVIGATION
let primaryNav = document.querySelectorAll('nav>span')
let subNav = document.querySelector('nav#sub')
let menuItem = document.querySelector('#menuItem')
let menuVoices = []
for (let n of primaryNav) {
n.nextSibling.parentNode.removeChild(n.nextSibling)
menuVoices[n.dataset.link] = new Synth(audioEngine)
menuVoices[n.dataset.link].gain.connect(masterGain)
n.addEventListener('click', (e) => {
for (let _n of primaryNav) {
if (n === _n && n.dataset.link != '~') {
n.classList.toggle('active')
} else if (n.dataset.link == '~') {
window.location.href = '/'
} else {
_n.classList.remove('active')
}
}
// subnav is nav element
if (!subNav.classList.contains('active')) {
subNav.classList.add('active')
}
for (let s of subNav.children) {
if (s.id == n.dataset.link) {
s.classList.add('active')
} else {
s.classList.remove('active')
}
}
})
n.addEventListener('mouseenter', () => {
if (n.dataset.link == '~') {
menuItem.textContent = '\u003C\u007E'
} else {
menuItem.textContent = n.dataset.link
}
menuItem.className = 'active'
menuVoices[n.dataset.link].noteOn()
})
n.addEventListener('mouseout', () => {
menuItem.className = 'inactive'
menuVoices[n.dataset.link].noteOff()
})
}