NoNameCracksInc

Javascript particle animations

Dec 22nd, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   function drewps()
  2.      {
  3.  
  4.      this.locate = function(c,t)
  5.      {
  6.       if(c.tagName == "CANVAS")
  7.       {
  8.         this.mainframe = c;
  9.         this.ctx = this.mainframe.getContext("2d");
  10.         this.pts = [];
  11.         t = this;
  12.  
  13.          c.onmouseenter = function(e){t.initX = e.offsetX;t.initY = e.offsetY;t.origins=[];for(var i=0; i<t.pts.length; i++){t.origins.push({"x":t.pts[i].x,"y":t.pts[i].y,"c":t.rn(1,50)});}}
  14.          c.onmousemove = function(e){if(t.floater && !t.moving)
  15.                                      {
  16.                                       var disY = t.initY - e.offsetY;
  17.                                       var disX = t.initX - e.offsetX;
  18.  
  19.                                       for(var i=0; i<t.pts.length; i++)
  20.                                       {
  21.                                         t.pts[i].x = t.origins[i].x + disX/t.origins[i].c;
  22.                                         t.pts[i].y = t.origins[i].y + disY/t.origins[i].c;
  23.                                       }
  24.  
  25.                                       t.set();  
  26.  
  27.                                      }
  28.                                      else{t.mx = e.offsetX;t.my = e.offsetY;}}
  29.  
  30.          c.onmouseout = function(e){delete t.mx;delete t.my;}
  31.       }
  32.      },
  33.  
  34.      this.getgradient = function(x,y,s,o)
  35.      {
  36.         var gradient = this.ctx.createRadialGradient(x,y,0,x,y,s);
  37.         gradient.addColorStop(0,"white");
  38.         gradient.addColorStop(0.4,"white");
  39.         gradient.addColorStop(0.4,o);
  40.         gradient.addColorStop(1,"black");
  41.  
  42.         return gradient;
  43.      },
  44.  
  45.      this.point = function(s,x,y,o,c)
  46.      {
  47.       c.ctx.beginPath();
  48.       c.ctx.arc(x,y,s,0,2*Math.PI);
  49.       c.ctx.fillStyle = (c.dblur) ? c.getgradient(x,y,s,o) : o;
  50.       c.ctx.fill();
  51.  
  52.       this.size = s;
  53.       this.color = o;
  54.       this.x = x;
  55.       this.y = y;
  56.       this.lr = c.r();
  57.       this.tb = c.r();
  58.  
  59.       return this;
  60.      },
  61.  
  62.      this.move = function(b,l,c,f,o)
  63.      {
  64.       o = this;
  65.       this.autoline = l;
  66.       this.linecolor = c;
  67.       this.moving = true;
  68.       this.rng = (b == "random");
  69.       this.callback = f;
  70.  
  71.       for(var i=0; i<o.pts.length; i++){o.pts[i].speed = (b == "random") ? o.rn(0,3) : b;}
  72.  
  73.       o.movemem = setInterval(function rd()
  74.       {
  75.         for(var i=0; i<o.pts.length; i++)
  76.         {
  77.           var co = o.pts[i].size/2;
  78.           if((o.pts[i].x < co) && !o.pts[i].lr){o.pts[i].lr = o.inv(o.pts[i].lr);}
  79.           if((o.pts[i].x > o.mainframe.width-co) && o.pts[i].lr){o.pts[i].lr = o.inv(o.pts[i].lr);}
  80.           if((o.pts[i].y > o.mainframe.height-co) && o.pts[i].tb){o.pts[i].tb = o.inv(o.pts[i].tb);}
  81.           if(o.pts[i].y < co && !o.pts[i].tb){o.pts[i].tb = o.inv(o.pts[i].tb);}
  82.  
  83.           o.pts[i].x += ((o.pts[i].lr) ? o.pts[i].speed : -o.pts[i].speed);
  84.           o.pts[i].y += ((o.pts[i].tb) ? o.pts[i].speed : -o.pts[i].speed);
  85.         }
  86.  
  87.         if(f && typeof f == "function"){f(o.pts);};
  88.  
  89.         o.set();
  90.         o.liner();
  91.       },1);
  92.      },
  93.  
  94.      this.add = function(c,s,o)
  95.      {
  96.       this.ctx.fillStyle = (this.backgroundcolor === undefined) ? "black" : this.backgroundcolor;
  97.       this.ctx.fillRect(0,0,this.mainframe.width,this.mainframe.height);
  98.  
  99.       for(var i=0; i<c; i++)
  100.       {
  101.         this.pts.push(new this.point(s,Math.random()*this.mainframe.width,Math.random()*this.mainframe.height,o,this));
  102.       }
  103.  
  104.       this.set();
  105.  
  106.       if(this.moving)
  107.       {
  108.         clearInterval(this.movemem);
  109.  
  110.         var lastpoint = this.pts[this.pts.length-1-c];
  111.         this.move(((this.rng) ? "random" : lastpoint.speed),this.autoline,this.linecolor,this.callback);
  112.       }
  113.  
  114.       return this.pts;
  115.      },
  116.  
  117.      this.set = function()
  118.      {
  119.       this.ctx.globalCompositeOperation = "source-over";
  120.      
  121.       if(this.dblur)  
  122.           this.ctx.fillStyle = this.h2rgba(((this.backgroundcolor === undefined) ? "#000000" : this.backgroundcolor),0.3);
  123.       else
  124.           this.ctx.fillStyle = ((this.backgroundcolor === undefined) ? "black" : this.backgroundcolor);
  125.  
  126.       this.ctx.fillRect(0,0,this.mainframe.width,this.mainframe.height);
  127.       if(this.dblur){this.ctx.globalCompositeOperation = "lighter";}
  128.  
  129.       for(var i=0; i<this.pts.length; i++)
  130.       {
  131.         this.point(this.pts[i].size, this.pts[i].x, this.pts[i].y, this.pts[i].color, this);
  132.       }
  133.      },
  134.  
  135.      this.liner = function()
  136.      {
  137.       if((typeof this.mx === "undefined" && this.autoline == "m-line") || this.autoline == "no-line"){return;}
  138.  
  139.       for(var i=0; i<this.pts.length; i++)
  140.       {
  141.         if(this.autoline == "m-line")
  142.         {
  143.           var dst = this.ld({"x":this.pts[i].x,"y":this.pts[i].y},{"x":this.mx,"y":this.my});
  144.           var coe = this.sense/dst*0.4;
  145.           if(coe >= 0.5){coe=0.5;}
  146.           if(coe < 0.1*0.1){continue;}
  147.           this.ctx.beginPath();
  148.           this.ctx.strokeStyle = this.linecolor;
  149.           this.ctx.lineWidth = coe;
  150.           this.ctx.moveTo(this.mx,this.my);
  151.           this.ctx.lineTo(this.pts[i].x,this.pts[i].y);
  152.           this.ctx.stroke();
  153.         }
  154.         else if(this.autoline == "a-line")
  155.         {
  156.           for(var q=0; q<this.pts.length; q++)
  157.           {
  158.             var dst = this.ld({"x":this.pts[i].x,"y":this.pts[i].y},{"x":this.pts[q].x,"y":this.pts[q].y});
  159.             var coe = this.sense/dst*0.4;
  160.             if(coe >= 0.5){coe=0.5;}
  161.             if(coe < 0.1*0.1){continue;}
  162.             this.ctx.beginPath();
  163.             this.ctx.strokeStyle = this.linecolor;
  164.             this.ctx.lineWidth = coe;
  165.             this.ctx.moveTo(this.pts[i].x,this.pts[i].y);
  166.             this.ctx.lineTo(this.pts[q].x,this.pts[q].y);
  167.             this.ctx.stroke();
  168.           }
  169.         }
  170.      }},
  171.  
  172.      this.sniff = function()
  173.      {
  174.       var elements = document.querySelectorAll("*");
  175.       var frames = [];
  176.       var useless = 0;
  177.  
  178.       var dummy = document.createElement("canvas");
  179.  
  180.       for(var i=0; i<elements.length; i++)
  181.       {
  182.         if(elements[i].tagName == "CANVAS")
  183.         {
  184.           dummy.width = elements[i].width;
  185.           dummy.height = elements[i].height;
  186.  
  187.           if(dummy.toDataURL() == elements[i].toDataURL())
  188.           {
  189.             frames.push(elements[i]);
  190.           }
  191.           else useless++;
  192.  
  193.         }
  194.       }
  195.  
  196.       console.log("I sniffed "+frames.length+" useable frame(s)!");
  197.       console.warn(useless + " frame(s) were probably already in use.");
  198.       return frames;
  199.      },
  200.  
  201.      // Support logic functions.
  202.  
  203.      this.r = function(){return Math.random() >= 0.5;},
  204.      this.inv = function(b){return !b;},
  205.      this.rn = function(mi, ma){var min=mi,max=ma;return Math.random()*(max - min)+min;},
  206.      this.ld = function(point1,point2){var xs=0;varys=0;xs=point2.x-point1.x;xs=xs*xs;ys=point2.y-point1.y;ys=ys*ys;return Math.sqrt(xs+ys);},
  207.      this.h2rgba = function(hex, alpha){var r=parseInt(hex.slice(1,3),16),g=parseInt(hex.slice(3,5),16),b=parseInt(hex.slice(5,7),16);return "rgba("+r+", "+g+", "+b+", "+alpha+")";}
  208.  
  209.      this.sense = 20,
  210.      this.moving = false,
  211.      this.floater = false,
  212.      this.dblur = false,
  213.      this.rng = false,
  214.      this.callback = null
  215.     }
Advertisement
Add Comment
Please, Sign In to add comment