diff --git a/index.html b/index.html index a506830..53af248 100644 --- a/index.html +++ b/index.html @@ -30,7 +30,7 @@
- +
@@ -70,14 +70,7 @@
- - - - - - - diff --git a/js/index.js b/js/index.js index 75c5285..385235c 100644 --- a/js/index.js +++ b/js/index.js @@ -1,544 +1,620 @@ -const micro_height=100; -const micro_width=100; -const canvas = document.getElementById('canvas'); -const ctx = canvas.getContext('2d'); -const SPEED= 50; -const AMPLITUDE=50; -const SIZE=1; -const NP=10; -const rigi=5; -const krigi=0.9; -const CLAMPED=1; -var DAMP=0.05; +const micro_height = 100; +const micro_width = 100; +const canvas = document.getElementById("canvas"); +const ctx = canvas.getContext("2d"); +const SPEED = 50; +const AMPLITUDE = 50; +const SIZE = 1; +const NP = 10; +const rigi = 5; +const krigi = 0.9; +const CLAMPED = 1; +var DAMP = 0.05; var firstMic = 1; var myId = 0; var nbMicro = 0; -var l=Math.floor(canvas.height/NP); //length of a row -var midl=Math.pow(l,2)/2-l/2; -var r=1; -var redstyle=0; -var bluestyle= 0; -var tempx,tempy,tempvx,tempvy,i,b,j,k,f,v,vunx,vuny,vdeuxx,vdeuxy,diffun,diffdeux,average,self,start, mousedown,press,radioval,firstmic=0; -var tab=[]; -var tabuffer=[]; -var previoustab=[]; -var tabtaken=[]; -var tabsin=[]; +var l = Math.floor(canvas.height / NP); //length of a row +var midl = Math.pow(l, 2) / 2 - l / 2; +var r = 1; +var redstyle = 0; +var bluestyle = 0; +const FRAME_RATE = 60; +const FRAME_INTERVAL = 1000 / FRAME_RATE; +var tempx, + tempy, + tempvx, + tempvy, + i, + b, + j, + k, + f, + v, + vunx, + vuny, + vdeuxx, + vdeuxy, + diffun, + diffdeux, + average, + self, + start, + mousedown, + press, + radioval, + firstmic = 0; +var tab = []; +var tabuffer = []; +var previoustab = []; +var tabtaken = []; +var tabsin = []; -frequency=document.querySelector("#freq"); -amplitude=document.querySelector("#gain"); -DB=document.querySelector("#db"); -damping=document.querySelector("#damp"); +frequency = document.querySelector("#freq"); +amplitude = document.querySelector("#gain"); +DB = document.querySelector("#db"); +damping = document.querySelector("#damp"); //////////////INITIALISATION//////// function initialisation() { - console.log("initialisation"); - for (i=0;il&&i&& i%l && (i%l)-l+1 && i l && i && i % l && (i % l) - l + 1 && i < l * (l - 1)) { + act = + tab[i + l].active + + tab[i - l].active + + tab[i - 1].active + + tab[i + 1].active; + if (!act) { + act = 1; } - else{disable(i); - temp=0; - switch (i) { - case 0:{ - temp=down(i)+right(i); - if (!temp){temp=SIZE;} - act=tab[i+l].active+tab[i+1].active; - }break; - case l-1:{ - temp=up(i)+right(i) - if (!temp){temp=SIZE;} - act=tab[i+l].active+tab[i-1].active; - }break; - case tab.length-l:{ - temp=down(i)+left(i); - if (!temp){temp=SIZE;} - act=tab[i-l].active+tab[i+1].active; - }break; - case tab.length-1:{ - temp=up(i)+left(i); - if (!temp){temp=SIZE;} - + // temp=(up(i)+down(i)+left(i)+right(i))*(rigi-1)/(act*rigi)+(tab[i].z)*1/rigi + d = (self * 4 - (up(i) + down(i) + left(i) + right(i))) / 4; + spd = tab[i].v - krigi * d - tab[i].v * DAMP; + temp = spd + tab[i].z; + tabuffer[i].z = temp; + tab[i].v = spd; + } else { + disable(i); + temp = 0; + switch (i) { + case 0: + { + temp = down(i) + right(i); + if (!temp) { + temp = SIZE; + } + act = tab[i + l].active + tab[i + 1].active; } + break; + case l - 1: + { + temp = up(i) + right(i); + if (!temp) { + temp = SIZE; + } + act = tab[i + l].active + tab[i - 1].active; } - if (temp){ - if(CLAMPED){ - temp=SIZE; + break; + case tab.length - l: + { + temp = down(i) + left(i); + if (!temp) { + temp = SIZE; + } + act = tab[i - l].active + tab[i + 1].active; } - else{ - temp=temp*(rigi-1)/(act*rigi)+(tab[i].z)*1/rigi; + break; + case tab.length - 1: { + temp = up(i) + left(i); + if (!temp) { + temp = SIZE; } } - else { - if (((i%l)-l+1)===0&&i){ - temp=up(i)+right(i)+left(i); - act=tab[i+l].active+tab[i-l].active - +tab[i-1].active; - } - if(i%l===0&&i!==l&&i){ - temp=right(i)+down(i)+left(i); - act=tab[i+l].active+tab[i-l].active - +tab[i+1].active; - } - if(il*l-l-1){ - temp=up(i)+down(i)+left(i); - act=tab[i-l].active - +tab[i-1].active+tab[i+1].active; - } - if(i0){ - temp=up(i)+down(i)+right(i); - act=tab[i+l].active+tab[i-1].active+tab[i+1].active; - } - if(!act){act=1} - temp=(temp*(rigi-1)/(act*rigi)+(tab[i].z)*1/rigi)/2; - temp+=previoustab[i].z*1/2; - previoustab[i].z=temp; + } + if (temp) { + if (CLAMPED) { + temp = SIZE; + } else { + temp = (temp * (rigi - 1)) / (act * rigi) + (tab[i].z * 1) / rigi; } - tabuffer[i].z=temp; - } + } else { + if ((i % l) - l + 1 === 0 && i) { + temp = up(i) + right(i) + left(i); + act = tab[i + l].active + tab[i - l].active + tab[i - 1].active; } - for(i=0;i l * l - l - 1) { + temp = up(i) + down(i) + left(i); + act = tab[i - l].active + tab[i - 1].active + tab[i + 1].active; + } + if (i < l && i > 0) { + temp = up(i) + down(i) + right(i); + act = tab[i + l].active + tab[i - 1].active + tab[i + 1].active; + } + if (!act) { + act = 1; + } + temp = ((temp * (rigi - 1)) / (act * rigi) + (tab[i].z * 1) / rigi) / 2; + temp += (previoustab[i].z * 1) / 2; + previoustab[i].z = temp; + } + tabuffer[i].z = temp; } + } + for (i = 0; i < tab.length; i++) { + tab[i].z = tabuffer[i].z; + } } -function down(i){ - return tab[i+1].z*tab[i+1].active;} -function up(i){ - return tab[i-1].z*tab[i-1].active;} -function left(i){ - return tab[i-l].z*tab[i-l].active;} -function right(i){ - return tab[i+l].z*tab[i+l].active;} -function disable(a){ - tab[a].active=0; +function down(i) { + return tab[i + 1].z * tab[i + 1].active; } -function unable(a){ - tab[a].active=1; +function up(i) { + return tab[i - 1].z * tab[i - 1].active; } -function toggle(a){ - tab[a].active ? tab[a].active=0:tab[a].active=1; +function left(i) { + return tab[i - l].z * tab[i - l].active; } -function makeWave(e){ - if (press){ - var x = event.pageX - elemLeft, - y = event.pageY - elemTop; - console.log("i move in make wave"); - if (ielement(x,y)!=b){ - wave(x,y)} - b=ielement(x,y); - } +function right(i) { + return tab[i + l].z * tab[i + l].active; } -function wave(x,y){ - tab[ielement(x,y)].v+=-SPEED; +function disable(a) { + tab[a].active = 0; } -function clear(){ +function unable(a) { + tab[a].active = 1; +} +function toggle(a) { + tab[a].active ? (tab[a].active = 0) : (tab[a].active = 1); +} +function makeWave(e) { + if (press) { + var x = event.pageX - elemLeft, + y = event.pageY - elemTop; + console.log("i move in make wave"); + if (ielement(x, y) != b) { + wave(x, y); + } + b = ielement(x, y); + } +} +function wave(x, y) { + tab[ielement(x, y)].v += -SPEED; +} +function clear() { removeMicro(); - tabsin=[]; + tabsin = []; } - - ////////////CLICK//////////////////// var elemLeft = canvas.offsetLeft, - elemTop = canvas.offsetTop, - context = canvas.getContext('2d'), - elements = []; + elemTop = canvas.offsetTop, + context = canvas.getContext("2d"), + elements = []; -onmousemove = function(e){ - switch (radioval){ - case "Mic":{ - };break; - case "Wall":{wallMaker(e) - };break; - case "Wave":{makeWave(e); - console.log(radioval); - };break; - - // case "Nothing":{};break; - } - -} -//onmousemove = wallMaker(e); -damping.oninput = function() { - DAMP= this.value; -}; -canvas.addEventListener('mouseup', function(event) { - press=0; - firstmic=0; -}, false); -canvas.addEventListener('mousedown', function(event) { - console.log("click"); - press=1; - var temp=0; - var x = event.pageX - elemLeft, - y = event.pageY - elemTop; - //switch () - for(i=0;i0){ - return temp; +function wallMaker(event) { + if (press) { + var x = event.pageX - elemLeft, + y = event.pageY - elemTop; + if (ielement(x, y) != b) { + makeWall(x, y); } - else {return midl}; + b = ielement(x, y); + } +} +function makeWall(x, y) { + toggle(ielement(x, y)); + window.requestAnimationFrame(render); + console.log("i made a wall", ielement(x, y)); +} +function ielement(x, y) { + var temp = Math.round(x / NP) * l + Math.round(y / NP); + if (temp < tab.length && temp > 0) { + return temp; + } else { + return midl; + } } ////////////RENDER/////////////////// -function render(){ - var xsize,ysize,xpos,ypos=10; - var up,right,diag=0; - var r,g,b,avr=0; - - ctx.clearRect(0, 0, canvas.width, canvas.height); - for(i=0;i= FRAME_INTERVAL) { + engine(); + lastFrameTime = currentTime; + } + requestAnimationFrame(loop); + } + requestAnimationFrame(loop); +} +*/