Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="content">
  <div id="canvasWrapper">
  </div>
</div>
              
            
!

CSS

              
                /* 
 * A p5.js coding sketch created as footage  
 * for: https://vimeo.com/644878742
 */

@import url('https://fonts.googleapis.com/css2?family=Recursive:CRSV,MONO@0,0..1&display=swap');

*, *:before, *:after{
  box-sizing: border-box;
}

:root {
  --purple-dark: #9E00FD;
  --purple-medium: #9F85FF;
  --purple-light: #D4D0FF;
  --green: #00FF80;
  --yellow: #FFFFC2;
  --black: #000000;
  --grey: #EFEFEF;
}

html, body {
  margin: 0;
  padding: 0;
  background-color: var(--grey);
}

body {
  background-color: var(--grey);
  font-family: "Recursive", sans-serif;
  font-size: 4vmin;
}

.content {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

canvas {
  min-width: 68vmin;
  min-height: 56vmin;
  margin: 12vmin;
  /* border: 1px dotted grey; */
}

#canvasWrapper {
  flex: 0 1 auto;
  background: #FFFFFF;
}
              
            
!

JS

              
                /* 
 * A coding sketch created as footage  
 * for: https://vimeo.com/644878742
 *
 * p5.js documentation:
 * https://p5js.org/reference 
 */

let blockWidth;
let blockHeight;
let margin;

let squareColors = [];
let blocks = [];

let font;
let fontsize;

function preload() {
  font = loadFont('https://res.cloudinary.com/dbyvpsrju/raw/upload/v1643209419/fonts/RecursiveSansLnrSt-Bold_um5m1o.otf');
}

function createBlocks(cnvsSize) {
  // set measurements based on canvas size
  blockWidth = cnvsSize/4;
  // blockHeight = blockWidth/2;
  margin = blockWidth/4;
  blockHeight = blockWidth/12 * 7;
  fontsize = blockWidth/2.4;

  for (let i = 0; i < 16; i++) {
    // create new object for each block
    let b = new Block(i);
    // and store in array
    blocks.push(b);
  }
}

function setup() {
  let cnvsSize;
  // basically, set canvas width to 68vmin
  if (windowHeight < windowWidth) {
    cnvsSize = windowHeight/100 * 68;
  } else {
    cnvsSize = windowWidth/100 * 68;
  }
  let cnvs = createCanvas(cnvsSize, cnvsSize * 0.88);
  cnvs.parent('canvasWrapper');
  // change fps from the standard 60 to 30
  frameRate(30);
  // get rid of stroke once and for all
  noStroke();

  const colourList = ['#9E00FD', '#9F85FF', '#00FF80'];
  /* selecting a colour for each block –
   * this is done once in setup so that colours
   * don't get reshuffled when window is resized
   */ 
  for (let i = 0; i < 16; i++) {
    squareColors[i] = random(colourList);
  }
  // call function that will setup the blocks
  createBlocks(cnvsSize);
}

// draw is looping 30 times per sec
function draw() {
  // rects are drawn from the center so we need some offset
  translate(margin * 2, margin);
  // draw background each time so animation is possible
  background(255);

  for (var i = 0; i < 16; i++) {
    blocks[i].show(i);
  }

  // Text
  push();
  translate( -margin * 2 + blockWidth/18, blockHeight * 4.5);
  fill(0);
  textSize(fontsize);
  textFont(font);
  text('Institute of', 0, 0);
  text('Generative Design', 0, fontsize * 1.08); //1.15
  pop();
}

class Block {
  constructor(i) {
    this.w = 0;
    this.h = 0;

    // x positions for the blocks
    this.xPos = [
      0, blockWidth, blockWidth * 2, blockWidth * 3,
      0, blockWidth, blockWidth * 2, blockWidth * 3,
      0, blockWidth, blockWidth * 2, blockWidth * 3,
      0, blockWidth, blockWidth * 2, blockWidth * 3
    ];

    // y positions for the blocks
    this.yPos = [
      0, 0, 0, 0,
      blockHeight, blockHeight, blockHeight, blockHeight,
      blockHeight * 2, blockHeight * 2, blockHeight * 2, blockHeight * 2,
      blockHeight * 3, blockHeight * 3, blockHeight * 3, blockHeight * 3
    ];

    this.x = this.xPos[i];
    this.y = this.yPos[i];
  }

  show(j) {
    /* check mouse positions to trigger
     * animation because blocks are drawn 
     * from center -> use -/+ half of w and
     * h to calculate trigger areas plus
     * margins used with translate earlier
     */
    if ( mouseX >= this.x - blockWidth/2 + margin * 2 && mouseX <= this.x + blockWidth/2 + margin * 2 &&
         mouseY >= this.y - blockHeight/2 + margin * 2 && mouseY <= this.y + blockHeight/2 + margin * 2 ) {
      // if mouse pos is within a block, set it's h and w to max
      this.w = blockWidth - blockWidth/8;
      this.h = blockHeight - blockWidth/8;
    }

    fill(squareColors[j]);
    rectMode(CENTER); // needed to animate from center
    rect(this.x, this.y, this.w, this.h);

    // decrease h + w when mouse leaves block area
    this.w -= this.w * 0.05;
    this.h -= this.h * 0.05;
  }
}

// Recalculates all the sizes when window is resized
function windowResized() {
  let cnvs;
  let cnvsSize;
  if (windowHeight < windowWidth) {
    cnvsSize = windowHeight/100 * 68;
  } else {
    cnvsSize = windowWidth/100 * 68;
  }
  cnvs = resizeCanvas(cnvsSize, cnvsSize * 0.88);
  // Blocks objects also need to be reset each time
  blocks = [];
  createBlocks(cnvsSize);
  redraw();
};
              
            
!
999px

Console