Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                <svg viewbox='-1000 -500 2000 1000' class='frame'></svg>
<div class='controls'>
  <input class='n-slider' type='range' min='3' value='3' max='26'>
  <input class='n-text' type='text' value='3'>
</div>
              
            
!

CSS

              
                $brown: #312C24; // dark medium-dark
$bright-yellow: #FADF1D; // light highlight-light
$light-yellow: #F9DE4E; // light medium-light
$bright-pink: #BB1654; // dark highlight-dark
$dirty-white: #F1F1F1; // light base-light

* { box-sizing: inherit; }

html {
  box-sizing: border-box;
  background: black;
}

.circle {
  fill: none;
  stroke: white;
  stroke-width: 5;
  
  &__radius {
    stroke: $dirty-white;
    stroke-dasharray: 15;
  }
}

.polygon {
  cursor: pointer;
  
  &__edge {
    stroke: $bright-yellow;
    stroke-width: 5;
    
    &-midpoint {
      fill: $dirty-white;
    }
  }
  
  &__circle {
    stroke-dasharray: 10;
    
    &:hover {
      stroke-dasharray: 0;
    }
  }
  
  &__vertex {
    fill: $bright-pink;
    
    &:hover {
      stroke: $light-yellow;
      stroke-width: 5;
    }
  }
  
  &__centre {
    fill: $light-yellow;
  }
}

.controls {
  position: fixed;
  top: .5em;
  padding: .5em;
  background: $brown;
  color: white;
  
  input {
    padding: .25em;
    vertical-align: middle;
    
    &[type='range'] {
      cursor: pointer;
    }
    
    &[type='text'] {
      max-width: 3em;
      border: none;
      border-radius: .25em;
      box-shadow: 0 0 .25em black;
      background: transparent;
      color: white;
      text-align: right;
      
      &:focus {
        box-shadow: 0 0 .25em $bright-yellow;
      }
    }
  }
}
              
            
!

JS

              
                var NS_URI = 'http://www.w3.org/2000/svg', 
    
    min = Math.min, 
    round = Math.round, 
    random = Math.random, 
    sqrt = Math.sqrt, pow = Math.pow, hypot = Math.hypot, 
    PI = Math.PI, cos = Math.cos, sin = Math.sin, 
    
    frame = document.querySelector('.frame'), 
    vb = frame.getAttribute('viewBox').split(' '), 
    w = ~~vb[2], h = ~~vb[3], 
    n_slider_el = document.querySelector('.n-slider'), 
    n_text_el = document.querySelector('.n-text'), 
    
    point_radius = 10;

/*
 * returns a random number between the min and max values
 * if min is omitted, it defaults to 0
 * if max is omitted, it defaults to 1
 */
var rand = function(max, min) {
  var b = max || 1, a = min || 0;

  return a + (b - a)*Math.random();
};

var setAttrs = function(el, o) {
  for(var a in o) {
    el.setAttribute(a, o[a]);
  }
};

var Point = function(x, y, r) {
  this.x = (x === 0 || x)?parseInt(x, 10):round(rand(frame.w));
  this.y = (y === 0 || y)?parseInt(y, 10):round(rand(frame.h));
  this.el = document.createElementNS(NS_URI, 'circle');
  
  this.el.classList.add('point');
  setAttrs(this.el, {
    'cx': this.x, 
    'cy': this.y, 
    'r': (r)?r:point_radius
  });
};

var Segment = function(start, end) {  
  this.start = (start instanceof Point)?start:(new Point());
  this.end = (end instanceof Point)?end:(new Point());
  this.el = document.createElementNS(NS_URI, 'line');
  
  this.el.classList.add('segment');
  setAttrs(this.el, {'x1': this.start.x, 'y1': this.start.y, 
                    'x2': this.end.x, 'y2': this.end.y});
  
  /* 
   * get the length of this segment
   * uses the start and end points
   */
  this.len = function() {
    var dx = this.end.x - this.start.x, 
        dy = this.end.y - this.start.y;

    return round(hypot?hypot(dx, dy):sqrt(pow(dx, 2) + pow(dy, 2)));
  };
  
  /*
   * the midpoint of the segment
   */
  this.midpoint = function() {
    return new Point(round((this.start.x + this.end.x)/2), 
                     round((this.start.y + this.end.y)/2), 
                     .7*point_radius);
  };
};

var Circle = function(type, radius, parent) {
  var radii_frag = document.createDocumentFragment(), b;
  
  this.centre = new Point(0, 0);
  this.radius = radius;
  this.radii = [];
  this.assembly_el = document.createElementNS(NS_URI, 'g');
  this.el = document.createElementNS(NS_URI, 'circle');
  
  this.el.classList.add('circle');
  setAttrs(this.el, {
    'cx': this.centre.x, 
    'cy':this.centre.y, 
    'r': this.radius
  });
  
  if(type === 'circumcircle') {
    for(var i = 0; i < parent.n; i++) {
      this.radii.push(new Segment(this.centre, parent.vertices[i]));
      this.radii[i].el.classList.add('circle__radius');
      radii_frag.appendChild(this.radii[i].el);
    }
  }
  if(type === 'incircle') {
    for(var i = 0; i < parent.n; i++) {
      b = parent.edges[i].midpoint();
      
      this.radii.push(new Segment(this.centre, b));
      
      this.radii[i].el.classList.add('circle__radius');
      b.el.classList.add('polygon__edge-midpoint');
      
      radii_frag.appendChild(this.radii[i].el);
      radii_frag.appendChild(b.el);
    }
  }
  
  this.assembly_el.appendChild(this.el);
  this.assembly_el.appendChild(radii_frag);
};

var Polygon = function(n, parent) {
  var parent = parent || frame, 
      radius_c = ((parent == frame)?1:random())*min(w, h)*.4, 
      central_angle = 2*PI/n, 
      start_angle = rand(central_angle), 
      radius_i = radius_c*cos(central_angle/2), 
      curr_angle, curr_x, curr_y, 
      frag_vertices = document.createElementNS(NS_URI, 'g'), 
      frag_edges = document.createElementNS(NS_URI, 'g'), 
      frag_circles = document.createElementNS(NS_URI, 'g');
  
  this.n = n;
  this.vertices = [];
  this.edges = [];
  this.el = document.createElementNS(NS_URI, 'g');
  
  this.el.classList.add('polygon');
  
  for(var i = 0; i < n; i++) {
    curr_angle = i*central_angle + start_angle;
    curr_x = round(radius_c*cos(curr_angle));
    curr_y = round(radius_c*sin(curr_angle));
    
    this.vertices.push(new Point(curr_x, curr_y));
    this.vertices[i].el.classList.add('polygon__vertex');
    this.vertices[i].el.setAttribute('data-i', i);
    frag_vertices.appendChild(this.vertices[i].el);
    
    if(i > 0) {
      this.edges.push(new Segment(this.vertices[i - 1], this.vertices[i]));
      this.edges[i - 1].el.classList.add('polygon__edge');
      frag_edges.appendChild(this.edges[i - 1].el);
      
      if(i === n - 1) {
        this.edges.push(new Segment(this.vertices[i], this.vertices[0]));
        this.edges[i].el.classList.add('polygon__edge');
        frag_edges.appendChild(this.edges[i].el);
      }
    }
  }
  
  this.circles = {
    'c': new Circle('circumcircle', radius_c, this), 
    'i': new Circle('incircle', radius_i, this)
  };
  
  this.o = this.circles.c.centre;
  
  frag_circles.appendChild(this.circles.c.assembly_el);
  this.circles.c.el.classList.add('polygon__circle');
  frag_circles.appendChild(this.circles.i.assembly_el);
  this.circles.i.el.classList.add('polygon__circle');
  this.o.el.classList.add('polygon__centre');
  
  this.el.appendChild(frag_edges);
  this.el.appendChild(frag_circles);
  this.el.appendChild(frag_vertices);
  this.el.appendChild(this.o.el);
  parent.appendChild(this.el);
  
};

var draw = function(n) {
  var poly = frame.querySelector('.polygon');
  
  if(poly) {
    frame.removeChild(poly);
  }
  
  poly = new Polygon(n, frame);
}

/* ========================= START IT ALL ========================= */
draw(n_slider_el.value);

/*
 * handle hitting ⇅ in text box 
 */
addEventListener('keyup', function(e) {}, false);

/*
 * handle moving slider thumb
 */
n_slider_el.addEventListener('change', function(e) {
  n_text_el.value = n_slider_el.value;
  draw(n_slider_el.value);
}, false);

/*
 * handle changing n text value
 */
n_text_el.addEventListener('blur', function(e) {
  n_slider_el.value = n_text_el.value;
  draw(n_slider_el.value);
}, false);
              
            
!
999px

Console