-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
144 lines (125 loc) · 4.44 KB
/
Copy pathmain.js
File metadata and controls
144 lines (125 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const gyroOutput = document.getElementById("gyro-output")
const socket = new WebSocket("wss://hackathon-backend-l22i.onrender.com/")
const spaceship = document.getElementById("spaceship")
const gyro = {}
let deltaXNorm = 0
let deltaYNorm = 0
let isRecording = false
const cookies = {}
const cookiesRaw = document.cookie.split("; ")
cookiesRaw.forEach(cookie => {
[key, value] = cookie.split("=")
cookies[key] = value
})
console.log(cookies)
function setSpaceshipColor(uuid) {
const hue = Number("0x" + uuid.substring(0, 2)) * 360 / 255
spaceship.style.color = `hsl(${hue}, 80%, 50%)`
}
socket.onopen = () => {
console.log("connection established")
if (cookies.uuid) {
socket.send(JSON.stringify({ client: cookies.uuid }))
setSpaceshipColor(cookies.uuid)
} else {
socket.send(JSON.stringify({ client: "new" }))
}
}
socket.onmessage = msg => {
const msgJSON = JSON.parse(msg.data)
if (msgJSON.uuid) {
document.cookie = "uuid=" + msgJSON.uuid
cookies.uuid = msgJSON.uuid
setSpaceshipColor(msgJSON.uuid)
}
}
setInterval(() => {
if (mouseIsDown) {
socket.send(JSON.stringify({ uuid: cookies.uuid, gyro: { alpha: deltaXNorm, beta: deltaYNorm } }))
console.log("sending " + deltaXNorm + " " + deltaYNorm)
}
}, 100);
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
console.log("getUserMedia supported.")
navigator.mediaDevices.getUserMedia({
audio: true
}).then(stream => {
const mediaRecorder = new MediaRecorder(stream)
recordBtn.addEventListener("touchstart", () => {
mediaRecorder.start()
console.log(mediaRecorder.state)
console.log("start media recording")
recordBtn.style.boxShadow = "0 0 5rem red inset"
isRecording = true
})
let chunks = []
mediaRecorder.ondataavailable = e => {
chunks.push(e.data)
}
document.body.addEventListener('touchend', () => {
if (isRecording) {
mediaRecorder.stop()
console.log(mediaRecorder.state)
console.log("recorder stopped")
recordBtn.style.boxShadow = "0 0 5rem blue inset"
}
})
mediaRecorder.onstop = e => {
console.log("recorder stopped")
const blob = new Blob(chunks, { type: "audio/wav" })
const fileReader = new FileReader()
fileReader.onload = function (event) {
console.log("onload")
const base64Data = event.target.result.split(',')[1]
socket.send(JSON.stringify({ uuid: cookies.uuid, audio: base64Data }))
}
fileReader.readAsDataURL(blob)
chunks = []
}
}).catch(err => {
console.error(`the following getUserMedia error occured: ${err}`)
})
} else {
console.log("getUserMedia not supported on your browser")
}
const dpad = document.getElementById('dpad')
const dpadKnob = document.getElementById('dpad-knob')
const recordBtn = document.getElementById('record-btn')
const container = document.getElementById('container')
const controlsContainer = document.getElementById('controls-container')
let mouseIsDown = false
let dragStartPosition = { x: 0, y: 0 }
dpadKnob.addEventListener("touchstart", event => {
mouseIsDown = true
dragStartPosition.x = event.touches[0].screenX
dragStartPosition.y = event.touches[0].screenY
})
document.addEventListener("touchend", () => {
mouseIsDown = false
dpadKnob.style.left = "0"
dpadKnob.style.top = "0"
dpadKnob.style.transition = "all 0.2s ease-in-out"
setTimeout(() => {
dpadKnob.style.transition = "none"
}, 200);
})
document.addEventListener("touchmove", event => {
if (mouseIsDown) {
const deltaX = event.touches[0].screenX - dragStartPosition.x
const deltaY = event.touches[0].screenY - dragStartPosition.y
dpadKnob.style.left = deltaX + "px"
dpadKnob.style.top = deltaY + "px"
deltaXNorm = deltaX / dpad.offsetWidth
deltaYNorm = deltaY / dpad.offsetHeight
}
})
const startBtn = document.getElementById("start-btn")
startBtn.addEventListener("touchstart", () => {
document.body.requestFullscreen()
setTimeout(() => {
if (document.fullscreenElement) {
startBtn.style.display = "none"
controlsContainer.style.display = "flex"
}
}, 500);
})