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>#4</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 squares = [];
let squareSize = size / 20;
let pathSize = size / 2;
let path = [];
let start = size / 2 - pathSize / 2;
const positionsPerStep = 30;
const squarePlacements = [0, 0.22, 0.41, 0.59, 0.78, 1];

function createPositions() {
  addPositionsForSide("top", start, start);
  addPositionsForSide("right", start + pathSize, start);
  addPositionsForSide("bottom", start, start + pathSize);
  addPositionsForSide("left", start, start);
}

function addPositionsForSide(side, x, y) {
  for (let p = 0; p < squarePlacements.length - 1; p++) {
    const offset =
      side == "top" || side == "right"
        ? pathSize * squarePlacements[p]
        : pathSize - pathSize * squarePlacements[p];
    const start = side == "top" || side == "bottom" ? x : y;
    const positionStart = start + offset;
    const step =
      (pathSize * squarePlacements[p + 1] - pathSize * squarePlacements[p]) /
      positionsPerStep;
    for (let i = 0; i < positionsPerStep; i++) {
      const adjustment =
        side == "top" || side == "right" ? step * i : -step * i;
      path.push({
        x: side === "top" || side === "bottom" ? positionStart + adjustment : x,
        y: side === "left" || side === "right" ? positionStart + adjustment : y
      });
    }
  }
}

class Square {
  constructor(position, size, color) {
    this.position = position;
    this.size = size;
    this.color = color;
  }

  move() {
    this.position++;
    if (this.position >= path.length) {
      this.position = 0;
    }
  }

  draw() {
    stroke(this.color);
    strokeWeight(this.size * 0.14);
    square(
      path[this.position].x - this.size / 2,
      path[this.position].y - this.size / 2,
      this.size
    );
  }
}

function createSquares() {
  for (let i = 0; i < path.length; i += positionsPerStep) {
    squares.push(
      new Square(
        i,
        squareSize,
        color(Math.floor((i / path.length) * 360), 100, 34)
      )
    );
  }
}

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

function draw() {
  clear();
  strokeWeight(1);
  // square(start, start, pathSize);
  for (let i = 0; i < squares.length; i++) {
    const square = squares[i];
    square.draw();
    square.move();
  }
}

function resize() {
  if (windowWidth < 420) {
    size = windowWidth * 0.8;
  } else {
    size = 400;
  }

  pathSize = size / 2;
  squareSize = size / 20;
  start = size / 2 - pathSize / 2;
  resizeCanvas(size, size);
  squares = [];
  path = [];
  createPositions();
  createSquares();
}

function windowResized() {
  resize();
}

requestAnimationFrame(() => {});

              
            
!
999px

Console