Advertisement
jamescolin

Spiders on your SystemeIO page

Aug 29th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. window.addEventListener('load', function () {
  3.  
  4. let mynewdiv = document.createElement("canvas");
  5. mynewdiv.setAttribute("style", "position:fixed;top:0;left:0;width:100%;height:100%;z-index:100;pointer-events:none;");
  6. mynewdiv.setAttribute("id", "canvas");
  7. document.querySelector("#app").appendChild(mynewdiv);
  8.  
  9. let w, h;
  10. const ctx = canvas.getContext("2d");
  11. const { sin, cos, PI, hypot, min, max } = Math;
  12.  
  13. function spawn() {
  14.    
  15.     const pts = many(333, () => {
  16.         return {
  17.             x: rnd(innerWidth),
  18.             y: rnd(innerHeight),
  19.             len: 0,
  20.             r: 0
  21.         };
  22.     });
  23.    
  24.     const pts2 = many(9, (i) => {
  25.         return {
  26.             x: cos((i / 9) * PI * 2),
  27.             y: sin((i / 9) * PI * 2)
  28.         };
  29.     });
  30.    
  31.     let seed = rnd(100)
  32.     let tx = rnd(innerWidth);
  33.     let ty = rnd(innerHeight);
  34.     let x = rnd(innerWidth)
  35.     let y = rnd(innerHeight)
  36.     let kx = rnd(0.5, 0.5)
  37.     let ky = rnd(0.5, 0.5)
  38.     let walkRadius = pt(rnd(50,50), rnd(50,50))
  39.     let r = innerWidth / rnd(100, 150);
  40.    
  41.     function paintPt(pt){
  42.         pts2.forEach((pt2) => {
  43.             if (!pt.len )
  44.                 return
  45.             drawLine(
  46.                 lerp(x + pt2.x * r, pt.x, pt.len * pt.len),
  47.                 lerp(y + pt2.y * r, pt.y, pt.len * pt.len),
  48.                 x + pt2.x * r,
  49.                 y + pt2.y * r
  50.             );
  51.         });
  52.         drawCircle(pt.x, pt.y, pt.r);
  53.     }
  54.  
  55.     return {
  56.         follow(x,y) {
  57.             tx = x;
  58.             ty = y;
  59.         },
  60.        
  61.         tick(t) {
  62.        
  63.     const selfMoveX = cos(t*kx+seed)*walkRadius.x        
  64.     const selfMoveY = sin(t*ky+seed)*walkRadius.y      
  65.     let fx = tx + selfMoveX;        
  66.     let fy = ty + selfMoveY;
  67.            
  68.     x += min(innerWidth/100, (fx - x)/10)
  69.     y += min(innerWidth/100, (fy - y)/10)
  70.            
  71.     let i = 0
  72.     pts.forEach((pt) => {
  73.         const dx = pt.x - x,
  74.             dy = pt.y - y;
  75.         const len = hypot(dx, dy);
  76.         let r = min(2, innerWidth / len / 5);
  77.         pt.t = 0;
  78.         const increasing = len < innerWidth / 10
  79.             && (i++) < 8;
  80.         let dir = increasing ? 0.1 : -0.1;
  81.         if (increasing) {
  82.             r *= 1.5;
  83.         }
  84.         pt.r = r;
  85.         pt.len = max(0, min(pt.len + dir, 1));
  86.         paintPt(pt)
  87.     });
  88.         }
  89.     }
  90. }
  91.  
  92. const spiders = many(2, spawn)
  93.  
  94. addEventListener("pointermove", (e) => {
  95.     spiders.forEach(spider => {
  96.         spider.follow(e.clientX, e.clientY)
  97.     })
  98. });
  99.  
  100. requestAnimationFrame(function anim(t) {
  101.     if (w !== innerWidth) w = canvas.width = innerWidth;
  102.     if (h !== innerHeight) h = canvas.height = innerHeight;
  103.     ctx.clearRect(0, 0, w, h);
  104.     ctx.fillStyle = ctx.strokeStyle = "rgba(255, 255, 255, 0.25)";
  105.     t/=1000
  106.     spiders.forEach(spider => spider.tick(t))
  107.     requestAnimationFrame(anim);
  108. });
  109.  
  110. function recalc(X, Y) {
  111.     tx = X;
  112.     ty = Y;
  113. }
  114.  
  115. function rnd(x = 1, dx = 0) {
  116.     return Math.random() * x + dx;
  117. }
  118.  
  119. function drawCircle(x, y, r, color) {
  120.     ctx.beginPath();
  121.     ctx.ellipse(x, y, r, r, 0, 0, PI * 2);
  122.     ctx.fill();
  123. }
  124.  
  125. function drawLine(x0, y0, x1, y1) {
  126.     ctx.beginPath();
  127.     ctx.moveTo(x0, y0);
  128.  
  129.     many(100, (i) => {
  130.         i = (i + 1) / 100;
  131.         let x = lerp(x0, x1, i);
  132.         let y = lerp(y0, y1, i);
  133.         let k = noise(x/5+x0, y/5+y0) * 2;
  134.         ctx.lineTo(x + k, y + k);
  135.     });
  136.  
  137.     ctx.stroke();
  138. }
  139.  
  140. function many(n, f) {
  141.     return [...Array(n)].map((_, i) => f(i));
  142. }
  143.  
  144. function lerp(a, b, t) {
  145.     return a + (b - a) * t;
  146. }
  147.  
  148. function noise(x, y, t = 101) {
  149.     let w0 = sin(0.3 * x + 1.4 * t + 2.0 +
  150.                  2.5 * sin(0.4 * y + -1.3 * t + 1.0));
  151.     let w1 = sin(0.2 * y + 1.5 * t + 2.8 +
  152.                  2.3 * sin(0.5 * x + -1.2 * t + 0.5));
  153.     return w0 + w1;
  154. }
  155.  
  156. function pt(x,y){
  157.     return {x,y}
  158. }
  159. });
  160. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement