-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.js
147 lines (134 loc) · 4.83 KB
/
example.js
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
145
146
147
import WSRTC from './wsrtc.js';
const container = document.getElementById('container');
const form = document.getElementById('form');
const input = form.querySelector('input[type=text]');
function parseQuery(queryString) {
var query = {};
var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
}
const qs = parseQuery(location.search);
input.value = qs.u || `wss://${location.host}`;
form.addEventListener('submit', e => {
e.preventDefault();
const u = input.value;
history.pushState({}, '', location.protocol + '//' + location.host + '?u=' + encodeURIComponent(u));
form.style.display = 'none';
const _createPlayerDom = player => {
const playerEl = document.createElement('div');
playerEl.classList.add('player');
const volumeEl = document.createElement('div');
volumeEl.classList.add('volume');
playerEl.appendChild(volumeEl);
container.appendChild(playerEl);
player.addEventListener('leave', e => {
container.removeChild(playerEl);
cancelAnimationFrame(frame);
});
const _updatePlayerDomPosition = () => {
playerEl.style.transform = `translate3d(${player.pose.position[0] * window.innerWidth}px, ${player.pose.position[1] * window.innerHeight}px, 0) scale(${1 - player.pose.position[2] * 0.2 + player.volume/10})`;
playerEl.style.backgroundColor = player.pose.position[2] ? '#333' : null;
volumeEl.style.height = `${player.volume * 100}%`;
volumeEl.style.filter = `hue-rotate(${player.pose.position[2] ? 180 : 0}deg)`;
};
_updatePlayerDomPosition();
// player.pose.addEventListener('update', _updatePlayerDomPosition);
let frame = null;
const _recurse = () => {
frame = requestAnimationFrame(() => {
_recurse();
_updatePlayerDomPosition();
/* if (player.pose.extra.length > 0) {
console.log('pose update', player.id, player.pose.position.join(','), player.pose.extra.slice());
} */
});
};
_recurse();
};
const _startWsrtc = async () => {
await WSRTC.waitForReady();
const wsrtc = new WSRTC(u);
wsrtc.addEventListener('open', e => {
wsrtc.localUser.setPose(
Float32Array.from([1, 2, 0]),
Float32Array.from([1, 0, 0, 0]),
Float32Array.from([3, 3, 3]),
);
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
const name = makeid(5);
wsrtc.localUser.setMetadata({
name,
});
wsrtc.enableMic();
const mousemove = e => {
const x = e.clientX / window.innerWidth;
const y = e.clientY / window.innerHeight;
wsrtc.localUser.setPose(
Float32Array.from([
x,
y,
wsrtc.localUser.pose.position[2],
]),
);
};
const mousedown = e => {
wsrtc.localUser.setPose(
Float32Array.from([
wsrtc.localUser.pose.position[0],
wsrtc.localUser.pose.position[1],
1,
]),
undefined,
undefined,
[
'lol',
2,
Float32Array.from([1, 2, 3, 4]),
],
);
};
const mouseup = e => {
wsrtc.localUser.setPose(
Float32Array.from([
wsrtc.localUser.pose.position[0],
wsrtc.localUser.pose.position[1],
0,
]),
);
};
document.addEventListener('mousemove', mousemove);
document.addEventListener('mousedown', mousedown);
document.addEventListener('mouseup', mouseup);
wsrtc.addEventListener('close', e => {
document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mousedown', mousedown);
document.removeEventListener('mouseup', mouseup);
});
});
wsrtc.addEventListener('join', e => {
const player = e.data;
player.audioNode.connect(WSRTC.getAudioContext().destination);
player.metadata.addEventListener('update', e => {
// console.log('metadata update', player.id, player.metadata.toJSON());
});
player.addEventListener('leave', e => {
// console.log('leave', player);
});
_createPlayerDom(player);
});
};
_startWsrtc();
}, {
once: true,
});