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 beds=[];
var timeOfDay=0.25;
var tick=2*1/(24*60);
var bedShadowX=new TimeTrack(0.25,0.5,0.75,0);
var bedShadowY=new TimeTrack(0.25,0.25,0.5,0.25);
var bedShadowA=new TimeTrack(0.25,0.25,0.5,0.25);
var bedWriggle=new TimeTrack(0.25,0.25,0.5,0.25);
var sky;

function setup() {
  createCanvas(windowWidth, windowHeight);
  beds.push(new Bed(width/2,height/2,height*0.25,height*0.4));
  sky=new Sky();
}

function draw() {
  background(200);
  sky.show(timeOfDay);
  beds.forEach(function(bed){
    bed.show(timeOfDay);
  });
  timeOfDay=(timeOfDay+tick)%1;
}

function Bed(x,y,w,h){
  var pillow=new Pillow(x,y-h*0.4,w*0.8,h*0.18);
  var duvet=new Duvet(x,y+h*0.1,w*1.1,h*0.9);
  
  this.show=function(tod){
    push();
    translate(x,y);
    rectMode(CENTER);
    var bsA=bedShadowA.getVal(tod);
    noStroke();
        fill(40,180*bsA);
    var bsX=bedShadowX.getVal(tod);
    var bsY=bedShadowY.getVal(tod);
    rect(-bsX*w/2+w/4, w/8+w/4-bsY*w/4, w, h, w*0.1);//light.bedShadowX*w/2-w/4
    fill(128);
    rect(0,0,w,h,w*0.1);
    pop();
    pillow.show(tod);
    duvet.show(tod);
  };
}

function Pillow(x,y,w,h){
  var shape=new Shape(x,y,w,h,10,0.5);
  
  this.show=function(tod){
    shape.show(tod);
  };
}

function Duvet(x,y,w,h){
  var shape=new Shape(x,y,w,h,10,2);
  
  this.show=function(tod){
    shape.show(tod);
  };
}

function Sky(){
  this.show=function(tod){
    var skyHue=80+140*abs((0.5-tod)/0.5);
    push();
    fill(skyHue/2,skyHue/2,skyHue);
    noStroke();
    rect(0,0,width,height);
    translate(width/2, height/2);
    push();
    rotate(tod*TWO_PI);
    translate(0,height*0.35);
    var dayNight=abs(0.5-tod)/0.5
    scale(1-dayNight);
    fill(255);
    ellipse(0,0,height*0.1);
    rotate(-tod*TWO_PI-PI/2);
    fill(skyHue/2,skyHue/2,skyHue);
    ellipse(0,height*0.02,height*0.1);
    pop();
    push();
    fill(255);
    rotate(tod*TWO_PI);
    translate(0,-height*0.35);
    scale(dayNight);
    ellipse(0,0,height*0.12);
    fill(255,100);
    ellipse(0,0,height*0.12*(0.5+dayNight)*(0.5+dayNight));
    pop();
    pop();
  };
  
}

function TimeTrack(ad,ar,dd,dr){
  
  this.getVal=function(time){
    var val=0;
    if(time>dd+dr){
      val=0;
    } else if(time>dd){
      val=1-(time-dd)/dr;
    } else if(time>ad+ar){
      val=1;
    } else if(time>ad){
      val=(time-ad)/ar;
    }
    return val;
  };
}

function Shape(x,y,w,h,n,magn){
  var ns=random(10);
  var vertices=[];
  var wriggle=0;
  var wriggleCount=60;
  var wriggleDisp=w/2;
  var wriggleInc=1;
  var wriggling=false;
  var noiseCount=0;
  var shade=random(-20,20);

  this.wriggleNow=function(){
    wriggling=true;
  }
  
  for(var i=0; i<n; i++){
    vertices.push({x:-w/2+i*w/n, y: -h/2, o:"h", s:0});
  }
  for(var i=0; i<n; i++){
    vertices.push({x:w/2, y:-h/2+i*h/n, o:"v", s:1});
  }
  for(var i=0; i<n; i++){
    vertices.push({x:w/2-i*w/n, y: h/2, o:"h", s:0});
  }
  for(var i=0; i<n; i++){
    vertices.push({x:-w/2, y:h/2-i*h/n, o:"v", s:1});
  }
  console.log(vertices);
  
  this.show=function(tod){
    var wrDisp=magn*wriggleDisp*bedWriggle.getVal(tod);//wriggleDisp*wriggle/wriggleCount;
    push();
    translate(x,y);
    beginShape();
    fill(200+shade);
    noStroke();
    vertices.forEach(function(vert,i){
      if(vert.o==="v"){
        vertex(vert.x+noise(ns+vert.y/50,vert.s,noiseCount/50)*wrDisp-wrDisp/2, vert.y);
      } else {
        vertex(vert.x, vert.y+noise(ns+vert.x/50,vert.s,noiseCount/50)*wrDisp-wrDisp/2);
      }
    });
    endShape(CLOSE);
    pop();
    noiseCount++;
  };
}
            
          
!
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