cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <p>Hover the vertices (intersection points in the middle of the edges are <b>not</b> vertices).</p>

<p><b>What are these?</b> Regular polygons (all angles and all vertices equal) gallery. Both regular convex polygons and regular star polygons.</p>

<p><b>What do the <code>p</code> and <code>q</code> mean?</b> <code>p</code> is the number of vertices. They are all distributed evenly on a circle. For <code>q =1</code>, you connect each point to the next one on the circle (clockwise or counterclockwise, it doesn't matter). For <code>q = 2</code>, you skip the first one and connect to the 2nd. For <code>q = 3</code>, you skip the first two and connect to the 3rd. And so on... as long as <code>q < p/2</code>.</p>
<p><code>{p, q}</code> is the <a href='http://en.wikipedia.org/wiki/Schl%C3%A4fli_symbol' target='_blank'>Schläfli symbol</a>.</p>
            
          
!
            
              
body {
  background: #222;
  color: #eee;
  font: 1em/1 trebuchet ms, verdana, sans-serif;
  text-align: center;
}

p {
  margin: 1em auto;
  width: 90vw;
  text-align: left;
}

b { color: lemonchiffon; }

a {
  color: yellow;
  &:hover { color: red; }
}

.shape-wrapper {
  display: inline-block;
  position: relative;
  padding-top: 3em;
  width: 19vw; height: 19vw;
  box-shadow: 0 0 2px rgba(#fff, .25);
  
  @media (max-width: 1000px) {
    width: 23vw; height: 23vw;
  }
  
  @media (max-width: 800px) {
    width: 45vw; height: 45vw;
  }
  
  @media (max-width: 400px) {
    width: 90vw; height: 90vw;
  }
  
  &:before, &:after {
    position: absolute;
    top: .25em;
    padding: .5em;
    border-radius: .25em;
    background: tan;
    color: black;
    transition: .35s;
  }
  
  &:before {
    left: .25em;
    content: 'point:' attr(data-curr);
  }
  
  &[data-curr='']:before { display: none; }
  
  &:after {
    right: .25em;
    opacity: .32;
    content: attr(data-type);
  }
  
  &:hover:after { opacity: .99; }
}

path {
  fill: rgba(#eee, .03);
  stroke: red;
  stroke-width: 16;
  filter: drop-shadow(0 0 25px tan);
}

.p {
  fill: red;
  stroke: tan;
  stroke-width: 16;
  opacity: .32;
  cursor: pointer;
  
  &:hover {
    opacity: .99;
  }
}
            
          
!
            
              var NS_URI = 'http://www.w3.org/2000/svg', 
    MAX_P = 17,
    frag = document.createDocumentFragment();

Node.prototype.setAttrs = function(attr_obj) {
  for(var prop in attr_obj) {
    this.setAttribute(prop, attr_obj[prop]);
  }
};

Node.prototype.getAncestorWithClass = function(cls) {
  var parent = this.parentNode, parent_cls;
    
  if(!parent) { return null; }
  
  parent_cls = parent.getAttribute('class');
  if(!parent_cls || parent_cls.indexOf(cls) === -1) {
    return parent.getAncestorWithClass(cls);
  }
  else { return parent; }
};

var generatePoly = function(parent, p, q, r) {
  var path = document.createElementNS(NS_URI, 'path'), 
      points = document.createElementNS(NS_URI, 'g'), 
      curr_g, 
      curr_point, 
      base_angle = 2*Math.PI/p, 
      angle = (Math.random() -q)*base_angle, 
      x, y, d_attr = '';
  
  for(var i = 0; i < p; i++) {
    curr_g = document.createElementNS(NS_URI, 'g');
    
    angle += q*base_angle;
    
    x = ~~(r*Math.cos(angle));
    y = ~~(r*Math.sin(angle));
    
    d_attr += 
      ((i === 0)?'M':'L') + x + ' ' + y + ' ';
    
    if(i*q%p === 0 && i > 0) {
      angle += base_angle;
      x = ~~(r*Math.cos(angle));
      y = ~~(r*Math.sin(angle));
      
      d_attr += 'M' + x + ' ' + y + ' ';
    }
    
    curr_point = document.createElementNS(NS_URI, 'circle');
    curr_point.setAttrs({
      'class': 'p p--' + i, 
      'cx': x, 'cy': y, 
      'r': 32
    });
    curr_g.appendChild(curr_point);
    
    points.appendChild(curr_g);
  }
  
  d_attr += 'z'
  
  path.setAttribute('d', d_attr);
  path.setAttribute('fill-rule', 'evenodd');
  parent.appendChild(path);
  parent.appendChild(points)
};

var addTests = function(MAX_P) {
  var wrapper, svg, path, 
      w = 2000, h = 2000, 
      k = .4, 
      r = k*Math.min(w, h);
  
  for(var p = 3; p <= MAX_P; p++) {
    for(var q = 1; q < p/2; q++) {
      wrapper = document.createElement('div');
      wrapper.classList.add('shape-wrapper');
      wrapper.dataset.type = 
        'p: ' + p + ', q: ' + q;
      wrapper.dataset.curr = '';
      
      svg = document.createElementNS(NS_URI, 'svg');
      svg.setAttribute('viewBox', -w/2 + ' ' + -h/2 + ' ' + w + ' ' + h);
      
      generatePoly(svg, p, q, r);
      
      wrapper.appendChild(svg);
      frag.appendChild(wrapper)
    }
  }
  
  document.body.appendChild(frag);
};

addTests(MAX_P);

addEventListener('mouseover', function(e) {
  var t = e.target, 
      cls = t.getAttribute('class'), 
      idx, a;
  
  if(cls && cls.indexOf('p--') > -1) {
    idx = ~~cls.split('p--')[1].split(' ')[0];
    a = t.getAncestorWithClass('shape-wrapper');
    a.dataset.curr = '' + (idx + 1);
  }
}, false);

addEventListener('mouseout', function(e) {
  var t = e.target, 
      cls = t.getAttribute('class'), 
      idx, a;
  
  if(cls && cls.indexOf('p--') > -1) {
    idx = ~~cls.split('p--')[1].split(' ')[0];
    a = t.getAncestorWithClass('shape-wrapper');
    a.dataset.curr = '';
  }
}, false);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console