Advertisement
Guest User

bkmotiv

a guest
Apr 10th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var NUM_PARTICLES = ( ( ROWS = 100 ) * ( COLS = 300 ) ),
  3.     THICKNESS = Math.pow( 80, 2 ),
  4.     SPACING = 3,
  5.     MARGIN = 100,
  6.     COLOR = 220,
  7.     DRAG = 0.95,
  8.     EASE = 0.25,
  9.    
  10.     /*
  11.    
  12.     used for sine approximation, but Math.sin in Chrome is still fast enough :)http://jsperf.com/math-sin-vs-sine-approximation
  13.  
  14.     B = 4 / Math.PI,
  15.     C = -4 / Math.pow( Math.PI, 2 ),
  16.     P = 0.225,
  17.  
  18.     */
  19.  
  20.     container,
  21.     particle,
  22.     canvas,
  23.     mouse,
  24.     stats,
  25.     list,
  26.     ctx,
  27.     tog,
  28.     man,
  29.     dx, dy,
  30.     mx, my,
  31.     d, t, f,
  32.     a, b,
  33.     i, n,
  34.     w, h,
  35.     p, s,
  36.     r, c
  37.     ;
  38.  
  39. particle = {
  40.   vx: 0,
  41.   vy: 0,
  42.   x: 0,
  43.   y: 0
  44. };
  45.  
  46. function init() {
  47.  
  48.   container = document.getElementById( 'container' );
  49.   canvas = document.createElement( 'canvas' );
  50.  
  51.   ctx = canvas.getContext( '2d' );
  52.   man = false;
  53.   tog = true;
  54.  
  55.   list = [];
  56.  
  57.   w = canvas.width = COLS * SPACING + MARGIN * 2;
  58.   h = canvas.height = ROWS * SPACING + MARGIN * 2;
  59.  
  60.   container.style.marginLeft = Math.round( w * -0.5 ) + 'px';
  61.   container.style.marginTop = Math.round( h * -0.5 ) + 'px';
  62.  
  63.   for ( i = 0; i < NUM_PARTICLES; i++ ) {
  64.    
  65.     p = Object.create( particle );
  66.     p.x = p.ox = MARGIN + SPACING * ( i % COLS );
  67.     p.y = p.oy = MARGIN + SPACING * Math.floor( i / COLS );
  68.    
  69.     list[i] = p;
  70.   }
  71.  
  72.   container.addEventListener( 'mousemove', function(e) {
  73.  
  74.     bounds = container.getBoundingClientRect();
  75.     mx = e.clientX - bounds.left;
  76.     my = e.clientY - bounds.top;
  77.     man = true;
  78.    
  79.   });
  80.  
  81.   if ( typeof Stats === 'function' ) {
  82.     document.body.appendChild( ( stats = new Stats() ).domElement );
  83.   }
  84.  
  85.   container.appendChild( canvas );
  86. }
  87.  
  88. function step() {
  89.  
  90.   if ( stats ) stats.begin();
  91.  
  92.   if ( tog = !tog ) {
  93.  
  94.     if ( !man ) {
  95.  
  96.       t = +new Date() * 0.001;
  97.       mx = w * 0.5 + ( Math.cos( t * 2.1 ) * Math.cos( t * 0.9 ) * w * 0.45 );
  98.       my = h * 0.5 + ( Math.sin( t * 3.2 ) * Math.tan( Math.sin( t * 0.8 ) ) * h * 0.45 );
  99.     }
  100.      
  101.     for ( i = 0; i < NUM_PARTICLES; i++ ) {
  102.      
  103.       p = list[i];
  104.      
  105.       d = ( dx = mx - p.x ) * dx + ( dy = my - p.y ) * dy;
  106.       f = -THICKNESS / d;
  107.  
  108.       if ( d < THICKNESS ) {
  109.         t = Math.atan2( dy, dx );
  110.         p.vx += f * Math.cos(t);
  111.         p.vy += f * Math.sin(t);
  112.       }
  113.  
  114.       p.x += ( p.vx *= DRAG ) + (p.ox - p.x) * EASE;
  115.       p.y += ( p.vy *= DRAG ) + (p.oy - p.y) * EASE;
  116.  
  117.     }
  118.  
  119.   } else {
  120.  
  121.     b = ( a = ctx.createImageData( w, h ) ).data;
  122.  
  123.     for ( i = 0; i < NUM_PARTICLES; i++ ) {
  124.  
  125.       p = list[i];
  126.       b[n = ( ~~p.x + ( ~~p.y * w ) ) * 4] = b[n+1] = b[n+2] = COLOR, b[n+3] = 255;
  127.     }
  128.  
  129.     ctx.putImageData( a, 0, 0 );
  130.   }
  131.  
  132.   if ( stats ) stats.end();
  133.  
  134.   requestAnimationFrame( step );
  135. }
  136.  
  137. init();
  138. step();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement