xxxxxxxxxx
68
let myVideo;
let friends = {};
let liveMediaConnection;
let colors = [
{r: 100, g: 200, b: 250},
{r: 200, g: 100, b: 0},
{r: 255, g: 0, b: 0}
]
let myBGColor = colors[0];
function setup() {
createCanvas(1280, 480);
liveMediaConnection = new p5LiveMedia(this, null, null, "my-cool-room");
liveMediaConnection.on("stream", gotStream);
liveMediaConnection.on("data", gotData);
myVideo = createCapture(VIDEO, gotLocalMediaStream);
myVideo.muted = true;
myVideo.hide();
}
function gotData(data){
console.log(data);
// myBGColor = JSON.parse(data);
myBGColor = colors[data];
}
function gotLocalMediaStream (stream) {
console.log("got local stream!");
liveMediaConnection.addStream(stream, "CAPTURE");
};
// We got a new stream!
function gotStream(stream, id) {
console.log('got remote stream!');
friends[id] = {
x: random() * (width - 100),
y: random() * (height - 100),
video: stream};
stream.hide();
}
function mousePressed(){
console.log('mouse pressed!');
let myIndex = floor(random(0,3));
myBGColor = colors[myIndex];
liveMediaConnection.send(myIndex);
}
function draw() {
background(myBGColor.r,myBGColor.g,myBGColor.b);
stroke(255);
strokeWeight(2);
textSize(24);
if (myVideo != null) {
image(myVideo, 0, 0, 100,100);
text("My Video", 30, 30);
}
for (let id in friends){
let p = friends[id];
image(p.video,p.x,p.y,100,100);
}
}