-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidealgas.min.js
1 lines (1 loc) · 2.17 KB
/
idealgas.min.js
1
const n=20;const t=25;const s=4;const i="rgb(80, 80, 80)";const h=2*Math.PI;class o{constructor(t,s){this.x=t;this.y=s}sub(t){return new o(this.x-t.x,this.y-t.y)}mul(t){return new o(this.x*t,this.y*t)}t(){return new o(-this.x,-this.y)}i(t){return this.x*t.x+this.y*t.y}get h(){return Math.sqrt(this.x**2+this.y**2)}}class e{constructor(t,s,i,h,n){this.position=new o(t,s);this.r=i;this.o=new o(h,n);this.l=new Set}u(){this.position.x+=this.o.x;this.position.y+=this.o.y}m(t,s){if(this.position.x<this.r){this.position.x=this.r;this.o.x=-this.o.x}else if(this.position.x>t-this.r){this.position.x=t-this.r;this.o.x=-this.o.x}if(this.position.y<this.r){this.position.y=this.r;this.o.y=-this.o.y}else if(this.position.y>s-this.r){this.position.y=s-this.r;this.o.y=-this.o.y}}g(t){return(this.position.x-t.position.x)**2+(this.position.y-t.position.y)**2<(this.r+t.r)**2}M(t){if(this.g(t)){if(this.l.has(t)){return}const s=this.o.sub(t.o);const i=this.position.sub(t.position);const h=s.i(i);const n=this.o.sub(i.mul(h/i.h**2));const o=s.t();const e=i.t();const c=o.i(e);const r=t.o.sub(e.mul(c/e.h**2));this.o=n;t.o=r;this.l.add(t);t.l.add(this)}else if(this.l.has(t)){this.l["delete"](t);t.l["delete"](this)}}}class c{constructor(){this.c=document.getElementById("c");this.p=this.c.getContext("2d");this.B()}B(){this.C=[];clearTimeout(this.S);this.S=setTimeout(()=>{this.c.width=window.innerWidth;this.c.height=window.innerHeight;this.k()},100)}T(){return Math.random()*2*s-s}k(){const i=Math.min(this.c.width,this.c.height)/1440*t;for(let s=i;s<=this.c.height-i;s+=2*i){for(let t=i;t<=this.c.width-i;t+=2*i){if(Math.random()*100>n){continue}const h=new e(t,s,i,this.T(),this.T());this.C.push(h)}}}F(){this.p.beginPath();this.p.fillStyle=i;for(const t of this.C){this.p.moveTo(t.position.x+t.r,t.position.y);this.p.arc(t.position.x,t.position.y,t.r,0,h)}this.p.stroke();this.p.fill()}P(){this.p.clearRect(0,0,this.c.width,this.c.height);for(let t=0;t<this.C.length;t++){const s=this.C[t];s.u();s.m(this.c.width,this.c.height);for(const i of this.C.slice(t+1)){s.M(i)}}this.F()}}window.addEventListener("load",async function(){let t=new c;window.addEventListener("resize",t.B.bind(t),false);window.setInterval(t.P.bind(t),10)});