|
|
|
class Dots{constructor(width,height,spacing){this.spacing=spacing;this.dots=[];this.alphaStep=1/10;this.cols=Math.floor(width/spacing);this.rows=Math.floor(height/spacing);const canvas=document.createElement('canvas'),ctx=canvas.getContext('2d');canvas.width=width;canvas.height=height;this.canvas=canvas;this.ctx=ctx;this.draw()}draw(){const ctx=this.ctx,spacing=this.spacing;ctx.fillStyle='rgba(24, 129, 141, .1)';this.dots=Array.apply(null,Array(this.cols)).map((n,x)=>{return Array.apply(null,Array(this.rows)).map((p,y)=>{let dot={opacity:0.1,x:x*spacing,y:y*spacing};ctx.fillRect(dot.x,dot.y,1,1);return dot})})}ghost(){const ghostDots=document.createElement('canvas');ghostDots.width=this.canvas.width;ghostDots.height=this.canvas.height;const dotsCtx=ghostDots.getContext('2d');dotsCtx.fillStyle='rgb(24, 129, 141)';this.dots.forEach(col=>{col.forEach(dot=>{dotsCtx.fillRect(dot.x,dot.y,1,1)})});return ghostDots}}class Circuits{constructor(width,height,size,minLength,maxLength){this.size=size;this.width=width;this.height=height;this.cols= ~~(width/size);this.rows= ~~(height/size);this.scene=Array.apply(null,Array(this.cols)).map(()=>new Col(this.rows));this.collection=[];this.minLength=minLength;this.maxLength=maxLength;this.populate();this.draw()}draw(){const canvas=document.createElement('canvas'),ctx=canvas.getContext('2d'),size=this.size;canvas.width=this.width;canvas.height=this.height;ctx.strokeStyle='rgba(59, 177, 188, 1)';ctx.lineWidth=Math.round(size/10);this.collection.forEach(circuit=>{let point=[circuit.start[0],circuit.start[1]],path=circuit.path;ctx.beginPath();ctx.moveTo(point[0]*size+size/2+path[0][0]*size/4,point[1]*size+size/2+path[0][1]*size/4);path.forEach((dir,index)=>{point[0]+=dir[0];point[1]+=dir[1];if(index===path.length-1){ctx.lineTo(point[0]*size+size/2-dir[0]*size/4,point[1]*size+size/2-dir[1]*size/4)}else{ctx.lineTo(point[0]*size+size/2,point[1]*size+size/2)}});ctx.stroke()});ctx.lineWidth= ~~(this.size/5);ctx.strokeStyle='rgba(59, 177, 188, .6)';this.collection.forEach(circuit=>{ctx.beginPath();ctx.arc(circuit.start[0]*size+size/2,circuit.start[1]*size+size/2,size/4,0,2*Math.PI,false);ctx.stroke();ctx.beginPath();ctx.arc(circuit.end[0]*size+size/2,circuit.end[1]*size+size/2,size/4,0,2*Math.PI,false);ctx.stroke()});this.canvas=canvas}populate(){const size=this.size;let start=null,n=1000,maxLength=this.maxLength,minLength=this.minLength,length=0,dir=null;while((start=this.getStart())&&n--){length=minLength+ ~~(Math.random()*(maxLength-minLength));dir=this.getDir(start);this.setUsed(start[0],start[1]);if(dir[0]!==0||dir[1]!==0){let circuit=new Circuit(start,size),moving=true,path=[start[0],start[1]],coords=[start[0],start[1]];length-=1;while(moving&&length){circuit.path.push(dir);circuit.coords.push([path[0],path[1]]);path[0]+=dir[0];path[1]+=dir[1];this.setUsed(path[0],path[1]);dir=this.getDir(path,dir);if(dir[0]===0&&dir[1]===0){moving=false}length-=1}if(circuit.path.length>=minLength){circuit.end=path;circuit.coords.push([path[0],path[1]]);let speed=Math.random()*0.5+0.5;circuit.things.push(things.create(circuit,speed*1));if(circuit.path.length>maxLength/3){speed=Math.random()*0.5+0.5;circuit.things.push(things.create(circuit,-speed,circuit.path.length*size))}if(circuit.path.length>maxLength/1.5){speed=Math.random()*0.5+0.5*(Math.random()>=0.5?-1:1);circuit.things.push(things.create(circuit,speed,Math.random()*circuit.path.length*size))}circuit.length=circuit.path.length*size;this.collection.push(circuit)}}}}getStart(){let found=false,col=null,row=null,free=[],result=false;const scene=this.scene;scene.forEach((col,index)=>{if(col.free){free.push(index)}});if(free.length){col=this.pickOne(free);free.length=0;scene[col].rows.forEach((row,index)=>{if(row===0){free.push(index)}});row=this.pickOne(free);result=[col,row]}return result}pickOne(array){return array[~~(Math.random()*array.length)]}setUsed(x,y){this.scene[x].rows[y]=1;this.scene[x].free-=1}isAvailable(x,y){const scene=this.scene;let result=false;if(typeof scene[x]!=='undefined'){if(typeof scene[x].rows[y]!=='undefined'){if(scene[x].row
|