vail/static/vail.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-04-09 23:09:33 -06:00
var ac = new AudioContext()
var gain = ac.createGain()
gain.connect(ac.destination)
gain.gain.value = 0.1
2020-04-10 08:27:35 -06:00
var short = 80
var long = 200
2020-04-09 23:09:33 -06:00
var audioFreq = 660
var audioFreqMe = audioFreq * 6 / 5 // I think this works out to a minor third
var myosc
function message(event) {
let duration = Number(event.data) || 0
2020-04-10 08:27:35 -06:00
duration = Math.min(duration, long)
2020-04-09 23:09:33 -06:00
let osc = ac.createOscillator()
osc.connect(gain)
osc.frequency.value = audioFreq
osc.start(ac.currentTime)
osc.stop(ac.currentTime + (duration * 0.001))
}
function key(event) {
2020-04-10 08:27:35 -06:00
let duration = 0
if ((event.button === 0) || (event.key == ",") || (event.key == "w")) {
duration = short
}
if ((event.button === 2) || (event.key == ".") || (event.key == "v")) {
duration = long
}
// You don't get to hold the key down yet, sorry
if ((event.repeat) || (duration === 0)) {
return
2020-04-09 23:09:33 -06:00
}
2020-04-10 08:27:35 -06:00
window.socket.send(duration)
myosc = ac.createOscillator()
myosc.connect(gain)
myosc.frequency.value = audioFreqMe
myosc.start(ac.currentTime)
myosc.stop(ac.currentTime + duration * 0.001)
2020-04-09 23:09:33 -06:00
}
2020-04-10 08:59:15 -06:00
function canWeJustNot(event) {
event.preventDefault()
return false
}
2020-04-09 23:09:33 -06:00
function init() {
2020-04-10 08:27:35 -06:00
let wsUrl = new URL(window.location)
wsUrl.protocol = "ws:"
wsUrl.pathname += "chat"
window.socket = new WebSocket(wsUrl)
2020-04-09 23:09:33 -06:00
window.socket.addEventListener("message", message)
2020-04-10 08:59:15 -06:00
// disable RMB context menu
document.addEventListener("contextmenu", e => canWeJustNot(e))
2020-04-09 23:09:33 -06:00
document.addEventListener("mousedown", e => key(e))
document.addEventListener("keydown", e => key(e))
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init)
} else {
init()
}