Activate your free membership today | Log-in

Friday, June 11th, 2010

Liquid Particles

Category: Canvas, Fun

<>p>Sit back and enjoy:

The bulk of the code is just:

JAVASCRIPT:
  1.  
  2. function run()
  3. {
  4.         ctx.globalCompositeOperation = "source-over";
  5.         ctx.fillStyle = "rgba(8,8,12,.65)";
  6.         ctx.fillRect( 0 , 0 , canvasW , canvasH );
  7.         ctx.globalCompositeOperation = "lighter";
  8.        
  9.         mouseVX    = mouseX - prevMouseX;
  10.         mouseVY    = mouseY - prevMouseY;
  11.         prevMouseX = mouseX;
  12.         prevMouseY = mouseY;
  13.        
  14.         var toDist   = canvasW / 1.15;
  15.         var stirDist = canvasW / 8;
  16.         var blowDist = canvasW / 2;
  17.        
  18.         var Mrnd   = Math.random;
  19.         var Mabs   = Math.abs;
  20.         var Msqrt  = Math.sqrt;
  21.         var Mcos   = Math.cos;
  22.         var Msin   = Math.sin;
  23.         var Matan2 = Math.atan2;
  24.         var Mmax   = Math.max;
  25.         var Mmin   = Math.min;
  26.        
  27.         var i = numMovers;
  28.         while( i-- )
  29.         {
  30.                 var m  = movers[i];
  31.                 var x  = m.x;
  32.                 var y  = m.y;
  33.                 var vX = m.vX;
  34.                 var vY = m.vY;
  35.                
  36.                 var dX = x - mouseX;
  37.                 var dY = y - mouseY;
  38.                 var d = Msqrt( dX * dX + dY * dY );
  39.                 var a = Matan2( dY , dX );
  40.                 var cosA = Mcos( a );
  41.                 var sinA = Msin( a );
  42.                
  43.                 if( isMouseDown )
  44.                 {
  45.                         if( d <blowDist )
  46.                         {
  47.                                 var blowAcc = ( 1 - ( d / blowDist ) ) * 14;
  48.                                 vX += cosA * blowAcc + .5 - Mrnd();
  49.                                 vY += sinA * blowAcc + .5 - Mrnd();
  50.                         }
  51.                 }
  52.                
  53.                 if( d <toDist )
  54.                 {
  55.                         var toAcc = ( 1 - ( d / toDist ) ) * canvasW * .0014;
  56.                         vX -= cosA * toAcc;
  57.                         vY -= sinA * toAcc;
  58.                 }
  59.                
  60.                 if( d <stirDist )
  61.                 {
  62.                         var mAcc = ( 1 - ( d / stirDist ) ) * canvasW * .00022;
  63.                         vX += mouseVX * mAcc;
  64.                         vY += mouseVY * mAcc;         
  65.                 }
  66.                
  67.                
  68.                 vX *= friction;
  69.                 vY *= friction;
  70.                
  71.                 var avgVX = Mabs( vX );
  72.                 var avgVY = Mabs( vY );
  73.                 var avgV = ( avgVX + avgVY ) * .5;
  74.                
  75.                 if( avgVX <.1 ) vX *= Mrnd() * 3;
  76.                 if( avgVY <.1 ) vY *= Mrnd() * 3;
  77.                
  78.                 var sc = avgV * .45;
  79.                 sc = Mmax( Mmin( sc , 3.5 ) , .4 );
  80.                
  81.                
  82.                 var nextX = x + vX;
  83.                 var nextY = y + vY;
  84.                
  85.                 if( nextX> canvasW )
  86.                 {
  87.                         nextX = canvasW;
  88.                         vX *= -1;
  89.                 }
  90.                 else if( nextX <0 )
  91.                 {
  92.                         nextX = 0;
  93.                         vX *= -1;
  94.                 }
  95.                
  96.                 if( nextY> canvasH )
  97.                 {
  98.                         nextY = canvasH;
  99.                         vY *= -1;
  100.                 }
  101.                 else if( nextY <0 )
  102.                 {
  103.                         nextY = 0;
  104.                         vY *= -1;
  105.                 }
  106.                
  107.                 m.vX = vX;
  108.                 m.vY = vY;
  109.                 m.x  = nextX;
  110.                 m.y  = nextY;
  111.                
  112.                 ctx.fillStyle = m.color;
  113.                 ctx.beginPath();
  114.                 ctx.arc( nextX , nextY , sc , 0 , radCirc , true );
  115.                 ctx.closePath();
  116.                 ctx.fill();          
  117.         }
  118. }
  119.  

Related Content:

  • molecule
    A molecule is the smallest particle in a chemical element or compound that has the chemical properties of that element or...
  • sound wave
    A sound wave is the pattern of disturbance caused by the movement of energy traveling through a medium (such as air, water, or any other liquid or...
  • acoustic wave
    A sound wave is the pattern of disturbance caused by the movement of energy traveling through a medium (such as air, water, or any other liquid or...
  • nanocrystal
    A nanocrystal is a crystalline particle with at least one dimension measuring less than 1000 nanometers (nm), where 1 nm is defined as 1...
  • IPF
    particleIllusion SE...

Posted by Dion Almaer at 2:58 am
6 Comments

OOOOO
Rate the above post

6 Comments »

Comments feed TrackBack URI

Why not just link to the page? A sodding long code listing and a cut-off screenshot? Such rubbish!

Comment by sixtyseconds — June 11, 2010

And why not make an article for every Chrome Experiment ?
Ok it is good code, but, I don’t think it deserves such an article here.

Comment by fabienmenager — June 11, 2010

ah cmon people that’s really a nice one. but i have to agree the site layout has started to look years older every day i get to see it. as a minimal incremental improvement, how about widening the main content region so it looks less like a toilet paper roll optimized for hercules graphics adapter resolutions?

i find it totally OK to show off snippets of code but that too suffers from the 100px or so narrow display imho.

Comment by loveencounterflow — June 11, 2010

I’d personally just like to be able to scroll the damned page without it slowing to a crawl every time… Dunno what it is, whether it’s a Firefox-only thing, but I see it on multiple machines. Exceedingly annoying and I really can’t understand what could possibly be happening on the page to cause it.

Comment by fzammetti — June 12, 2010

Cool. Works so smooth in Opera, good Canvas show case. And nice interaction with mouse.

Comment by movax — June 13, 2010

tn;dr

(Too Narrow, Didn’t Read)

Comment by MG — June 14, 2010

Leave a comment

You must be logged in to post a comment.