xxxxxxxxxx
var socket = io.connect(":30000?sketch=1373225");
var thestuff = {};
var myid;
var w;
var numnotes = 24;
var ftime = 100;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
textSize(32);
textAlign(CENTER,CENTER);
socket.on("connect", () => {
myid = socket.id;
});
socket.on("disconnect", () => {
socket.emit('killme', myid);
});
socket.on('iamclicking', someoneIsClicking); //when "iamclicking" messages is received from server, set "someoneIsDrawing" function to be called
socket.on('hello!', someoneJoined); //when "hello!" messages is received from server, set "someoneJoined" function to be called
socket.on('killme', killme); //delete from stack
socket.emit('hello!'); //let's send a message to anyone in the room
// this doesn't work:
window.addEventListener('beforeunload', function (e) {
//e.preventDefault();
//e.returnValue = '';
socket.emit('killme', myid);
});
w = width/numnotes;
}
function draw() {
background(255);
for(let i in thestuff)
{
fill(0, 0, 255, thestuff[i].f);
stroke(0);
rect(thestuff[i].n*w, 0, w, height);
thestuff[i].o.amp(0.1*(thestuff[i].f/ftime));
thestuff[i].f--;
if(thestuff[i].f<0) thestuff[i].f=0;
}
}
function mouseReleased() {
someoneIsClicking(myid, mouseX,mouseY);
socket.emit('iamclicking',myid, mouseX, mouseY);
}
function someoneIsClicking(id,x,y) { //this function will be called to draw a red ellipse for guest user
console.log(id);
let s;
if(!thestuff[id]) {
thestuff[id] = {};
s = thestuff[id];
s.o = new p5.Oscillator('sine');
s.o.start();
}
s = thestuff[id];
s.n = floor(x/w);
s.f = ftime;
s.o.amp(0.1*(s.f/ftime)); // 0.1 is so it isn't too loud
s.o.freq(midiToFreq(48+s.n));
}
function someoneJoined(){ //this function will be called to notify users when a new user joins
print('New user joined!');
}
function killme(id) {
delete thestuff[id];
}
function settext()
{
tval = this.value();
}
var socket = io.connect($OP.getEchoServerURL(1373225));
Learn more See an example