123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              var lBlobs=[];
var numLBlobs=10;
var disturbers=[];
var numDisturbers=10;
var disturbLen=100;
var disturbA=0
var ox=0, oy=0;
var wp;
var ts=200;


function setup() {
  createCanvas(windowWidth, windowHeight);
  disturbLen=height/6;
  var offX=width/2-8*height/25;
  var offY=height/2-8*height/25;
  for(var j=0; j<16; j++){
    for(var i=0; i<16; i++){
      lBlobs.push(new LiquidBlob(offX+j*height/25, offY+i*height/25, height/25, 0.1, 0.95));
    }
  }
  colorMode(HSB);
}

function updateDisturbers(){
  for(var i=0; i<numDisturbers; i++){
    disturbers[i]={
      x:mouseX+cos(disturbA+TWO_PI/numDisturbers*i)*disturbLen/2,
      y:mouseY+sin(disturbA+TWO_PI/numDisturbers*i)*disturbLen/2
    };
  }
}

function draw() {
  background(20);
  lBlobs.forEach(function(lBlob){
    lBlob.show();
  });
  disturbA+=0.01;
  updateDisturbers();
  textAlign(CENTER);
  textSize(height/16);
  fill(30,80,100,0.4);
  noStroke();
  text("move mouse over the cells",width/2, height*0.85);
}

function mouseMoved(){
  ox=width/2+(mouseX-width/2)*2;
  oy=mouseY;
}

function LiquidBlob(x,y,r,s,d){
  var cells=[];
  var numCells=20;
  var verts=[];
  
 for(var i=0; i<numCells; i++){
    // cells[i]=new Cell(i*width/numCells,y+(y-r),s,d);
    cells[i]=new Cell(i, x+cos(TWO_PI*i/numCells)*r, y+sin(TWO_PI*i/numCells)*r, s, d);
    if(i>0){
      cells[i].prevCell=cells[i-1];
      cells[i].setPrevDist();
      cells[i-1].nextCell=cells[i];
      cells[i-1].setNextDist();
    }
  }
  cells[numCells-1].nextCell=cells[0];
  cells[numCells-1].setNextDist();
  cells[0].prevCell=cells[numCells-1];
  cells[0].setPrevDist();
  
  this.show=function(){
    verts=[];
    var velAgg=0;
    cells.forEach(function(cell){
      // cell.show();
      // cell.repel();
      cell.repelMany();
      cell.neighbourTension();
      cell.run();
      verts.push(cell.getVertex());
      velAgg+=cell.getVertex().v;
    });
    var velMean=velAgg/cells.length;
    stroke(100*velMean,100,100,0.5-0.25*velMean);
    strokeWeight(0.5+velMean*10);
    noFill();
    beginShape();
    verts.forEach(function(v){
      vertex(v.x, v.y);
    });
    endShape(CLOSE);
  };
}

function Cell(id,x,y, s, d){
  var stiffness=s;
  var decay=d;
  var maxDist=25;
  var repelStiffness=0.01;
  var origin=createVector(x,y);
  var pos=createVector(0,0);
  var vel=createVector(0,0);
  var velMax=maxDist*stiffness;
  var absPos=origin.copy();
  var repel=createVector(0,0);
  var repelMag=maxDist;
  var acc=createVector(0,0);
  var zero=createVector(0,0);
  
  
  this.prevCell=null;
  this.nextCell=null;
  var prevDist=0;
  var nextDist=0;
  var neighbourStiffness=s/10;
  var prevTension=createVector(0,0);
  var nextTension=createVector(0,0);
  
  this.setNextDist=function(){
    nextDist=p5.Vector.sub(absPos, this.nextCell.getPos()).mag();
  };
  
  this.setPrevDist=function(){
    prevDist=p5.Vector.sub(absPos, this.prevCell.getPos()).mag();
  };
  
  this.getPos=function(){
    return absPos;
  }
  
  this.show=function(){
    noStroke();
    push();
    translate(origin.x, origin.y);
    translate(pos.x, pos.y);
    fill(0,100,100,1);
    ellipse(0,0,5);
    pop();
  };
  
  this.repel=function(){
    repel=p5.Vector.sub(absPos,createVector(mouseX, mouseY));
    repelMag=repel.mag();
    if(repelMag<maxDist){
      repel.mult((maxDist-repelMag)*repelStiffness);
    } else {
      repelMag=maxDist;
      repel.mult(0);
    }
  };
  
  this.repelMany=function(){
    repel.mult(0);
    disturbers.forEach(function(d){
      var repelThis=p5.Vector.sub(absPos,createVector(d.x, d.y));
      repelMag=repelThis.mag();
      if(repelMag<maxDist){
        repelThis.mult((maxDist-repelMag)*repelStiffness);
      } else {
        repelMag=maxDist;
        repelThis.mult(0);
      }
      repel.add(repelThis);
    });
  }
  
  this.neighbourTension=function(){
    var prevTarget=p5.Vector.sub(absPos, this.prevCell.getPos());
    prevTarget.setMag(prevDist).add(this.prevCell.getPos());
    prevTension=p5.Vector.sub(prevTarget, absPos);
    var prevDistNow=prevTension.mag();
    // if(id===0) console.log(id+" "+prevDistNow);
    if(prevDistNow>0){
      prevTension.mult(neighbourStiffness);
    } else {
      prevDistNow=0;
      prevTension.mult(0);
    }
    
    var nextTarget=p5.Vector.sub(absPos, this.nextCell.getPos());
    nextTarget.setMag(nextDist).add(this.nextCell.getPos());
    nextTension=p5.Vector.sub(nextTarget, absPos);
    var nextDistNow=nextTension.mag();
    // if(id===0) console.log(id+" "+prevDistNow);
    if(nextDistNow>0){
      nextTension.mult(neighbourStiffness);
    } else {
      nextDistNow=0;
      nextTension.mult(0);
    }
  }
  
  function runPrevTension(){
    
  }
  
 
  this.run=function(){
    acc.add(p5.Vector.sub(zero,pos).mult(stiffness));
    acc.add(repel);
    acc.add(nextTension);
    acc.add(prevTension);
    vel.add(acc);
    vel.mult(decay);
    pos.add(vel);
    acc.mult(0);
    absPos=p5.Vector.add(origin, pos);
  };
  
  this.getVertex=function(){
    return {
      x:absPos.x,
      y:absPos.y,
      v:vel.mag()/velMax
    };
  };
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console