index-MyYpA6PZ.js 5.1 KB

1
  1. (function(){const i=document.createElement("link").relList;if(i&&i.supports&&i.supports("modulepreload"))return;for(const s of document.querySelectorAll('link[rel="modulepreload"]'))r(s);new MutationObserver(s=>{for(const o of s)if(o.type==="childList")for(const n of o.addedNodes)n.tagName==="LINK"&&n.rel==="modulepreload"&&r(n)}).observe(document,{childList:!0,subtree:!0});function t(s){const o={};return s.integrity&&(o.integrity=s.integrity),s.referrerPolicy&&(o.referrerPolicy=s.referrerPolicy),s.crossOrigin==="use-credentials"?o.credentials="include":s.crossOrigin==="anonymous"?o.credentials="omit":o.credentials="same-origin",o}function r(s){if(s.ep)return;s.ep=!0;const o=t(s);fetch(s.href,o)}})();const a=10,u=7.5,g=4,M=17,m=300,d={night:"#10414F",day:"#D3E4DD"};class C{constructor(i,t,r){this.position={x:i,y:t},this.size={width:a,height:a},this.color=r}update(i){}draw(i){i.fillStyle=d[this.color],i.beginPath(),i.rect(this.position.x*this.size.width,this.position.y*this.size.height,this.size.width,this.size.height),i.fill()}}class b{constructor(i,t,r,s,o){this.position={x:t.x,y:t.y},this.radius=u,this.levelBounds=i,this.velocity={x:r.x,y:r.y},this.color=s,this.oppositeColor=o,this.drawCollisions=!1}update(i){this.position.x,this.position.y,this.position.x+=this.velocity.x,this.position.y+=this.velocity.y,this.position.x<this.radius&&(this.position.x=this.radius,this.velocity.x*=-1),this.position.y<this.radius&&(this.position.y=this.radius,this.velocity.y*=-1),this.position.x>this.levelBounds.width-this.radius&&(this.position.x=this.levelBounds.width-this.radius,this.velocity.x*=-1),this.position.y>this.levelBounds.height-this.radius&&(this.position.y=this.levelBounds.height-this.radius,this.velocity.y*=-1);const t={x:Math.floor(this.position.x/a),y:Math.floor(this.position.y/a)},r=i.filter(n=>n instanceof C).filter(n=>n.color==this.color).sort(n=>2*Math.random()).filter(n=>n.position.x>t.x-2&&n.position.x<t.x+2&&n.position.y>t.y-2&&n.position.y<t.y+2);for(let n=0;n<r.length;n++){let h=r[n];if(this.checkIfWithin(this.position,h)){h.color=this.oppositeColor;var s=0,o=0;this.velocity.x>0?s=h.position.x*h.size.width-(this.position.x+this.radius):this.velocity.x<0&&(s=h.position.x*h.size.width+h.size.width-(this.position.x-this.radius)),this.velocity.y>0?o=h.position.y*h.size.height-(this.position.y+this.radius):this.velocity.y<0&&(o=h.position.y*h.size.height+h.size.height-(this.position.y-this.radius)),Math.abs(s)<Math.abs(o)?(this.position.x+=s,this.velocity.x*=-1):(this.position.y+=o,this.velocity.y*=-1);break}}}checkIfWithin(i,t){return this.checkWithinX(i,t)&&this.checkWithinY(i,t)}checkWithinX(i,t){return i.x+this.radius>t.position.x*t.size.width&&i.x-this.radius<t.position.x*t.size.width+t.size.width}checkWithinY(i,t){return i.y+this.radius>t.position.y*t.size.height&&i.y-this.radius<t.position.y*t.size.height+t.size.height}draw(i){i.fillStyle=d[this.color],i.beginPath(),i.arc(this.position.x,this.position.y,this.radius,0,2*Math.PI),i.fill(),this.drawCollisions&&(i.strokeStyle="lime",i.beginPath(),i.rect(this.position.x-this.radius,this.position.y-this.radius,this.radius*2,this.radius*2),i.stroke())}}const x=document.querySelector("canvas.night-and-day"),z=x.getContext("2d"),l={width:m,height:m},B=m/4,I=m/2;var p,f;const c=[];var E=!1,D=0,A=0,y=M,L={x:0,y:0},S={x:0,y:0};document.addEventListener("DOMContentLoaded",()=>{T(),F()});window.addEventListener("resize",T);window.addEventListener("keydown",e=>{switch(e.code){case"F9":E=!E,E?v():w();break;case"Equal":y=Math.max(M,y-30),v(),w();break;case"Minus":y=y+30,v(),w();break;default:console.log(e.code);break}});function N(e,i){return e>=l.width/a/2?i<l.height/a/2?"night":"day":i<l.height/a/2?"day":"night"}function F(){_(),fetch("https://botsin.space/@randomColorContrasts.rss").then(e=>e.text(),e=>{console.error("Could not retrieve RSS feed for color contrasts.")}).then(e=>new DOMParser().parseFromString(e,"text/xml")).then(e=>{let i=[...e.getElementsByTagName("item")];for(let t=0;t<i.length;t++){let r=i[t],s=[...r.getElementsByTagName("category")];if(s.length==2){d.day=`#${s[0].innerHTML}`,d.night=`#${s[1].innerHTML}`;break}let o=r.getElementsByTagName("description")[0].textContent,n=new RegExp(/#.*?([0-9A-F]+)/gm),h=[...o.matchAll(n)];if(h[0]&&h[0][1]&&h[1]&&h[1][1]){d.day=`#${h[0][1]}`,d.night=`#${h[1][1]}`;break}}})}function _(){p=new b(l,{x:B+u,y:B+u},{x:-g,y:g},"night","day"),f=new b(l,{x:I+B-u,y:I-u},{x:g,y:-g},"day","night"),c.length=0;for(let e=0;e<l.width/a;e++)for(let i=0;i<l.height/a;i++){const t=new C(i,e,N(i,e));c.push(t)}c.push(p),c.push(f),w()}function O(){k(L,p.position)&&k(S,f.position)&&(v(),F()),L.x=p.position.x,L.y=p.position.y,S.x=f.position.x,S.y=f.position.y}function k(e,i,t=a*2){return i.x>e.x-t&&i.x<e.x+t&&i.y>e.y-t&&i.y<e.y+t}function v(){clearInterval(D),clearInterval(A)}function w(){D=setInterval(P,y),A=setInterval(O,3e4),R()}function P(){c.forEach(e=>e.update(c))}function W(e){c.forEach(i=>i.draw(e))}function R(){z.clearRect(0,0,l.width,l.height),W(z),window.requestAnimationFrame(R)}function T(e){const i=x.getBoundingClientRect();x.width=i.width,x.height=i.height,z.width=i.width,z.height=i.height}