xxxxxxxxxx
var socket = io.connect(":30000?sketch=1373198");
var thestuff = {};
var myid;
var w;
var numnotes = 24;
var myscale = [0,2,4,5,7,9,11];
var basenote = 40;
var thescale = [];
var ftime = 100;
var whichnote = 0;
var noteid = 0;
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;
for(let i = 0;i<numnotes;i++)
{
thescale[i] = basenote+myscale[i%myscale.length]+floor(i/myscale.length)*12;
}
console.log(thescale);
}
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);
if(thestuff[i].noteid==whichnote)
{
thestuff[i].o.amp(0.1*(thestuff[i].f/ftime));
}
else {
thestuff[i].o.amp(0);
}
thestuff[i].f--;
if(thestuff[i].f<0) thestuff[i].f=0;
}
if(frameCount%15==0) whichnote = (whichnote+1)%thestuff.length;
}
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.noteid = noteid;
noteid++;
}
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(thescale[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(1373229));
Learn more See an example