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 gaps=[];
var numGaps=100;
var showInfo=true;
var centre;
var step;
var maxW=step;

var nX=0;
var nXInc=0.005;
var nY=0;
var nYInc=0.05;

function setup() {
  createCanvas(windowWidth, windowHeight);
  step=40;
  colorMode(HSB);
  centre=createVector(width/2, height/2);
  
  for(var j=0; j<floor(height/step); j++){
    for(var i=0; i<floor(width/step); i++){
      var g=new Gap(centre, 0,0,step, step);
      g.update(-width/2+i*step+step/2, -height/2+j*step+step/2);
      g.findOuter();
      g.findOuter2();
      gaps.push(g);
    }
  }

}

function draw() {
  background(0);
  gaps.forEach(function(g){
    g.showBeam2();
    g.run();
  });
  nX+=nXInc;
  nY+=nYInc;
  push();
  fill(0,0,100,0.2);
  textAlign(CENTER);
  textSize(height/16);
  text("Move the mouse", width/2, height*0.7);
  pop();
}

function mouseDragged(){
  gaps[0].update(mouseX-centre.x, mouseY-centre.y);
  // gaps[0].findOuter();
  gaps[0].findOuter2();
}

function nomouseDragged(){
  centre.x=mouseX;
  centre.y=mouseY;
  
}

function Gap(c,x,y,ow,oh) {
  var pivot=null;
  var verts=[];
  var outVerts=[];
  var startV=null;
  var startA=TWO_PI;
  var endV=null;
  var endA=0;
  var nearV=null;
  var nearD=width*2;
  var farV=null;
  var farD=0;
  var farStartV=null;
  var farEndV=null;
  var farFarV=null;
  var w=ow;
  var h=oh;
  var lightArea=constrain(w*h/(maxW*maxW),0,1);
  var isInside=false;
  var isHalfInside=false;
  var controlSize=0.1;
  var throbA=random(TWO_PI);
  var throbInc=PI/100;
  var dynaSize=0;
  var hCol=random(360);
  
  var nearVerts=[];
  var farVerts=[];
  var drift=createVector(1, 1).mult(0.2);
  
  this.run=function(){
    var dynaSize=1-constrain(dist(x,y,mouseX-c.x,mouseY-c.y)/(width/8),0,1);
    pivot.add(drift);
    edges();
    w=dynaSize*ow;
    h=dynaSize*oh;
    lightArea=constrain(w*h/(maxW*maxW),0,1);
    this.update(pivot.x, pivot.y);
    this.findOuter2();
  }

  function edges(){
    if(pivot.x>width/2) pivot.x=-width/2;
    if(pivot.x<-width/2) pivot.x=width/2;
    if(pivot.y>height/2) pivot.y=-height/2;
    if(pivot.y<-height/2) pivot.y=height/2;
  }
  
  this.update=function(nx,ny){
    pivot=createVector(nx, ny);
    x=nx;
    y=ny;
    verts=[];
    outVerts=[];
    var v, ov;
    var inCount=0;
    var vMult=width/pivot.mag();
    v=createVector(x-w/2, y-h/2);
    verts.push(v);
    ov=v.copy().mult(vMult);
    inCount+=(v.x>ov.x && v.y>ov.y)?1:0;
    outVerts.push(ov);
    v=createVector(x+w/2, y-h/2);
    verts.push(v);
    ov=v.copy().mult(vMult)
    inCount+=(v.x<ov.x && v.y>ov.y)?1:0;
    outVerts.push(ov);v=createVector(x+w/2, y+h/2);
    verts.push(v);
    ov=v.copy().mult(vMult)
    inCount+=(v.x<ov.x && v.y<ov.y)?1:0;
    outVerts.push(ov);v=createVector(x-w/2, y+h/2);
    verts.push(v);
    ov=v.copy().mult(vMult)
    inCount+=(v.x>ov.x && v.y<ov.y)?1:0;
    outVerts.push(ov);// console.log(verts);
    isInside=inCount>=4;
    isHalfInside=(inCount>=2 && !isInside);
  }
  
  this.showBox=function(){
    var intensity=1-pivot.mag()/(width*0.71);
    rectMode(CENTER);
    push();
    translate(c.x, c.y);
    noFill();
    stroke(50,100,100,1);
    rect(x,y,w,h);
    pop();
  }
  
  this.show=function(){
    var intensity=1-pivot.mag()/(width*0.71);
    rectMode(CENTER);
    push();
    translate(c.x, c.y);
    fill(intensity*200+50,200);
    noStroke();
    beginShape();
    verts.forEach(function(v){
      vertex(v.x, v.y);
    });
    endShape(CLOSE);
    stroke(255,150,0,255);
    noFill();
    beginShape();
    outVerts.forEach(function(v){
      vertex(v.x, v.y);
    });
    endShape(CLOSE);
    pop();
  }
  
  this.findOuter2=function(){
      nearVerts=[];
      var far=0;
      var farthest=-1;
      verts.forEach(function(v,i){
        nearVerts[i]={
          v: v.copy(),
          d: v.mag(),
          h: v.heading()+TWO_PI
        }
        if(v.mag()>far){
          far=v.mag();
          farthest=i;
        }
      });
      if(isHalfInside){
        //get two points of near side by brute force
        if(pivot.x-w/2<0 && pivot.x+w/2>0){
          if(pivot.y<0){
            nearVerts.splice(0,2);
          } else {
            nearVerts=nearVerts.splice(0,2);
          }
        }
        if(pivot.y-h/2<0 && pivot.y+h/2>0){
          if(pivot.x<0){
            nearVerts=nearVerts.splice(1,2);
          } else {
            nearVerts.splice(1,2);
          }
        }
        nearVerts.sort(function(a,b){
          return a.h-b.h;
        });
      } else {
        nearVerts.splice(farthest,1);
        nearVerts.sort(function(a,b){
          return a.h-b.h;
        });
      }

      farVerts=[];
      var near=width*2;
      var nearest=-1;
      outVerts.forEach(function(v,i){
        farVerts[i]={
          v: v.copy(),
          d: v.mag(),
          h: v.heading()
        }
        if(v.mag()<near){
          near=v.mag();
          nearest=i;
        }
      });
      if(isHalfInside){
        //order by heading, reverse
        farVerts.sort(function(a,b){
          return b.h-a.h;
        });
      } else {
        farVerts.splice(nearest,1);
        farVerts.sort(function(a,b){
          return b.h-a.h;
        });
      }
  };
  
  this.showBeam=function(){
    var intensity=1-nearV.mag()/(width*0.71);
    push();
    translate(c.x, c.y);
    if(startV && endV && nearV && farStartV && farEndV && farFarV){
      fill(50+100*lightArea+intensity*100,50);
      noStroke();
      beginShape();
      vertex(startV.x, startV.y);
      vertex(nearV.x, nearV.y);
      vertex(endV.x, endV.y);
      vertex(farEndV.x, farEndV.y);
      vertex(farFarV.x, farFarV.y);
      vertex(farStartV.x, farStartV.y);
      endShape(CLOSE);
    } else {
      // console.log("not");
    }
    pop();
  }
  
  this.showBeam2=function(){
    var intensity=1-pivot.mag()/(width*0.71);
    push();
    translate(c.x, c.y);
    fill(hCol,0,lightArea*50+intensity*50,intensity);
    noStroke();
    rect(x-w/2,y-h/2,w,h);
    beginShape();
    if(isInside){
      outVerts.forEach(function(ov){
        vertex(ov.x, ov.y);
      });
    } else {
      nearVerts.forEach(function(nv){
        vertex(nv.v.x, nv.v.y);
      });
      farVerts.forEach(function(fv,i){
        vertex(fv.v.x, fv.v.y);
      });
    }
    noStroke();
    fill(hCol,0,intensity*100,intensity/2);//lightArea*35+65
    endShape(CLOSE);
    pop();
  }
  
  this.findOuter=function(){
    startA=TWO_PI;
    endA=0;
    startV=null;
    endV=null;
    nearD=width*2;
    nearV=null;
    farD=0;
    farV=null;
    farStartV=null;
    farEndV=null;
    farFarV=null;
    verts.forEach(function(v){
      if(v.heading()<startA){
        startA=v.heading();
        startV=v;
        farStartV=v.copy().setMag(width*0.71);
      }
      if(v.heading()>endA){
        endA=v.heading();
        endV=v;
        farEndV=v.copy().setMag(width*0.71);
      }
      if(v.mag()<nearD){
        nearD=v.mag();
        nearV=v;
      }
      if(v.mag()>farD){
        farD=v.mag();
        farV=v;
        farFarV=v.copy().setMag(width*0.71);
      }
    });
  }
}
            
          
!
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