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.

            
              <![if gte IE 9 | !IE]>
<div class="options">
  
  <div class="option">
    <label for="stroke">Stroke Width</label>
    <input id="stroke" type="range" name="stroke" value="0.33" min="0.01" max="0.4" step="0.01">
    <p class="input-info">
      <span class="left">-</span>
      <span class="right">+</span>
    </p>
  </div>
  
  <div class="option">
    <label for="points">Polygon Points</label>
    <input id="points" type="range" name="points" value="6" min="3" max="12" step="1">
    <p class="input-info">
      <span class="left">-</span>
      <span class="right">+</span>
    </p>
  </div>
  
  <div class="option">
    <label for="base">Color</label>
    <input id="base" type="color" name="base" value="#b3ffc8">
  </div>
  
  <div class="option">
    <label for="rotSpeed">Rotation</label>
    <input id="rotSpeed" type="range" name="rotSpeed" value="0.004" min="0" max="0.02" step="0.002">
    <p class="input-info">
      <span class="left">-</span>
      <span class="right">+</span>
    </p>
  </div>
  
</div>
<![endif]>
            
          
!
            
              canvas {
  position: fixed;
  top: 0;
  left: 0;
}

.options {
    font-family: sans-serif;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: white;
    z-index: 1;
}

.option {
  float: left;
  margin: 8px 20px;
}

label, input {
  display: block;
}

label {
  font-size: 12px;
  font-weight: 900;
}

.input-info {
  margin: 0;
  padding: 0;
}

.left {
  float: left;
}

.right {
  float: right;
}
            
          
!
            
              var polygons = [];
var from;
var to;
var startSize = 1;
var strokeSize = 0.33;
var polygonPoints = 6;
var rotationOffset = 4;
var rotSpeed = 0.004;

function setup() {
  var iHeight = constrain(window.innerHeight, 500, 900);
  var iWidth = constrain(window.innerWidth, 500, 1200);
  
  createCanvas(iWidth, iHeight);
  
  drawingContext.shadowBlur = 50;
  drawingContext.shadowColor = 'black';
  
  from = color(55, 55, 55, 0);
  chooseColor();
  
  var colourPickEl = select('#base');
  var strokeEl = select('#stroke');
  var pointsEl = select('#points');
  var rotSpeedEl = select('#rotSpeed');
  
  colourPickEl.changed(chooseColor);
  strokeEl.changed(chooseStroke);
  pointsEl.changed(choosePoly);
  rotSpeedEl.changed(chooseRotSpeed);
  
  setupPolygons();
}

function chooseRotSpeed() {
  rotSpeed = select('#rotSpeed').value();
}

function choosePoly() {
  polygonPoints = select('#points').value();
}

function chooseStroke() {
  strokeSize = select('#stroke').value();
}

function chooseColor() {
  var colour = select('#base').value();
  colour = hexToRgb(colour);
 
  to = color(colour.r, colour.g, colour.b);
}

function setupPolygons() {
  var polySize = startSize;
  var polyRot = 1;
  var polyId = 0;
  while (polySize < width) {
    polygons.push(new Polygon(polySize, polyRot, polyId));
    var mult = map(polySize, startSize, width, 1.13, 1.5);
    polySize *= mult;
    polyRot += rotationOffset;
    polyId += 1;
  }
}

function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

function draw() {
  background(51);
  
  polygons.forEach(function(p) {
    p.render();
    p.update();
  });
}

function Polygon(size, rotation, id) {
  this.id = id;
  this.size = size;
  this.rotation = rotation;
  this.color = lerpColor(from, to, 0);
}

Polygon.prototype.update = function() {
  if (this.size > (width * 1.5)) {
    this.size = startSize;
    this.rotation = getPrevRotation(this.id) - rotationOffset;
  }
  
  var mult = map(this.size, startSize, width, 1.005, 1.018);
  this.size *= mult;
  var col = map(this.size, startSize, width/2, 0, 1);
  this.color = lerpColor(from, to, col);
  this.rotation += rotSpeed;
}

Polygon.prototype.render = function() {
  noFill();
  strokeWeight(this.size * strokeSize);
  stroke(this.color);
  push();
  translate(width * 0.5, height * 0.47);
  rotate(this.rotation);
  polygon(0, 0, this.size, polygonPoints);
  pop();
}

function getPrevRotation(id) {
  var lastIndex = polygons.length - 1;
  if (id === lastIndex) {
    return polygons[0].rotation;
  } else {
    return polygons[id + 1].rotation;
  }
}

function polygon(x, y, radius, npoints) {
  var angle = TWO_PI / npoints;
  beginShape();
  for (var a = 0; a < TWO_PI; a += angle) {
    var sx = x + cos(a) * radius;
    var sy = y + sin(a) * radius;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}
            
          
!
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