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="container">
  <div id="canvas"></div>
  <div class="details">
    <p>code sketch <strong>#5</strong></p>
    <p>p5.js</p>
    <p>animated</p>
  </div>
</div>
              
            
!

CSS

              
                body {
  background: #f2f0e9;
  height: 100vh;
  margin: 0;
}

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 400px;
  margin: 0 auto;
}

.details {
  font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console,
    monospace;
  text-align: right;
}

.details p {
  margin-bottom: 0;
  margin-top: 0.3rem;
}

#canvas {
  width: 100%;
}

#canvas canvas {
  border: 2px solid #0e0a0d;
  width: 100%;
}

@media (max-width: 420px) {
  .container {
    width: 80vw;
  }
}

              
            
!

JS

              
                let size = 400;
let squareSize = size * 0.02;
let step = size * 0.01;
let squares = [];
let hue = 0;

class Square {
  constructor(x, y, size, hue) {
    this.x = x;
    this.y = y;
    this.size = size;
    this.centre = {
      x: this.x + squareSize * 0.5,
      y: this.y + squareSize * 0.5
    };
    this.hue = hue;
  }

  update() {
    // calculate angle from center of circle
    const angle = atan2(this.centre.y - Circle.y, this.centre.x - Circle.x);
    // extend the line to the edge of the circle
    const x = Circle.x + Circle.radius * cos(angle);
    const y = Circle.y + Circle.radius * sin(angle);
    // get the distance from the point on the line
    const distance = abs(dist(this.centre.x, this.centre.y, x, y));
    // if distance is smaller than radius * 0.7, make square larger
    // otherwise, square size is half
    let newSize = squareSize * 0.5;
    if (distance < Circle.radius * 0.7)
      newSize = map(
        distance,
        0,
        Circle.radius * 0.7,
        squareSize,
        squareSize * 0.5
      );

    this.size = newSize;
  }

  draw() {
    stroke(hue, 100, 40, 1);
    fill(hue, 100, 40, 1);
    square(this.x, this.y, this.size);
  }
}

function createSquares() {
  for (let x = step; x < size; x += squareSize + step) {
    for (let y = step; y < size; y += squareSize + step) {
      const square = new Square(x, y, squareSize, 0);
      squares.push(square);
    }
  }
}

const Circle = {
  x: size / 2,
  y: size / 2,
  radius: 0,
  acceleration: 5
};

function setup() {
  colorMode(HSL);
  const c = createCanvas(size, size);
  c.parent("canvas");
  resize();
}

function draw() {
  clear();
  Circle.radius += Circle.acceleration;
  Circle.acceleration += 0.02;
  hue += 2;
  if (hue > 360) hue = 0;
  if (Circle.radius > size * 1.5) {
    Circle.radius = 0;
    Circle.acceleration = 5;
  }
  for (let i = 0; i < squares.length; i++) {
    squares[i].update();
    squares[i].draw();
  }
}

function resize() {
  squares = [];
  if (windowWidth < 400) {
    size = windowWidth * 0.8;
  } else {
    size = 400;
  }
  squareSize = size * 0.02;
  step = size * 0.01;
  Circle.x = size / 2;
  Circle.y = size / 2;
  resizeCanvas(size, size);
  createSquares();
}

function windowResized() {
  resize();
}

requestAnimationFrame(() => {});

              
            
!
999px

Console