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

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

              
                <canvas class='c'></canvas>
<canvas class='c'></canvas>
<canvas class='c'></canvas>

<section class='controls'>
  <section class='controls__basic'>
    <label for='r1'>fixed circle radius (R):</label>
    <input id='r1' type='range' 
           min='.2' max='.62' value='.5' 
           step='.01'/>

    <label for='r2'>rolling circle radius (r):</label>
    <input id='r2' type='range' 
           min='.25' max='.62' value='.18' 
           step='.01'/>

    <label for='d'>tracer distance (d):</label>
    <input id='d' type='range' 
           min='.5' max='2.62' value='1.87' 
           step='.05'/>

    <label for='v'>velocity (v):</label>
    <input id='v' type='range' 
           min='.2' max='1' value='.5' 
           step='.01'/>
  </section>

  <section class='controls__type'>
    <input type='radio' name='type' id='hypo' checked/>
    <label for='hypo'>hypotrochoid</label>
    <input type='radio' name='type' id='epi'/>
    <label for='epi'>epitrochoid</label>
  </section>

  <section class='controls__scheme'>
    <label for='hue'>hue:</label>
    <input id='hue' type='range' 
           min='0' max='360' value='90' 
           step='1'/>
  </section>

  <section class='controls__btns'>
    <button id='rand'>random</button>
    <button id='apply'>apply</button>
    <button id='cancel'>cancel</button>
  </section>

  <button id='toggle'>toggle controls</button>
</section>
              
            
!

CSS

              
                @import "compass/css3";

$grid-major-c: rgba(gainsboro, .15);
$grid-minor-c: rgba(silver, .1);
$grid-major-th: .25em;
$grid-minor-th: .125em;
$grid-major-s: 10em;
$grid-minor-s: $grid-major-s/5;
$axis-c: rgba(white, .4);
$axis-th: 1.25*$grid-major-th;
$bg: darken(#143d63, 10%);
$hue-grad-list: ();

@mixin track() {
  height: 1.5em;
  background: transparent;
}

@mixin h-track() {
  background: transparent;
}

@mixin thumb() {
  width: .5em; height: 1.5em;
  box-shadow: 0 0 1px 1px $bg;
  background: whitesmoke;
  cursor: ew-resize;
}

@for $i from 0 to 36 {
  $hue-grad-list: append(
    $hue-grad-list, 
    hsl($i*10, 100%, 50%), 
    comma
  );
}

html, body, .c { width: 100%; height: 100%; }

body {
  overflow: hidden;
  margin: 0;
  background: 
    radial-gradient(
      rgba($bg, .001), darken(rgba($bg, .99), 20)),
    
    linear-gradient(90deg, 
      $axis-c $axis-th, 
      transparent $axis-th) 
    calc(50vw - #{$axis-th/2}) 0, 

    linear-gradient(
      $axis-c $axis-th, 
      transparent $axis-th) 
    0 calc(50vh - #{$axis-th/2}), 
    
    linear-gradient(90deg, 
      $grid-major-c $grid-major-th, 
      transparent $grid-major-th) 
    calc(50vw - #{$grid-major-th/2}) 0, 

    linear-gradient(
      $grid-major-c $grid-major-th, 
      transparent $grid-major-th) 
    0 calc(50vh - #{$grid-major-th/2}), 

    linear-gradient(90deg, 
      $grid-minor-c $grid-minor-th, 
      transparent $grid-minor-th) 
    calc(50vw - #{$grid-minor-th/2}) 0, 

    linear-gradient(
      $grid-minor-c $grid-minor-th, 
      transparent $grid-minor-th) 
    0 calc(50vh - #{$grid-minor-th/2}) 
  
  darken(#143d63, 10%);
  background-size: 100% 100%, 
    100% 100%, 100% 100%, 
    $grid-major-s $grid-major-s, 
    $grid-major-s $grid-major-s, 
    $grid-minor-s $grid-minor-s, 
    $grid-minor-s $grid-minor-s;
  font: 1em verdana, sans-serif;
}

.c {
  position: absolute;
  z-index: -1;
}

.controls {
  position: absolute;
  bottom: 100%;
  padding: 0 .5em;
  background: #111;
  color: #eee;
  transition: .35s ease-out;
  
  &--open {
    transform: translateY(100%);
  }
  
  section {
    margin: .75em 0;
    padding: .5em;
    border-radius: .25em;
    box-shadow: 0 0 2px rgba(white, .25);
  }
  
  button, input, [type='radio'] + label {
    cursor: pointer;
  }
  
  button {
    border: none;
    font: 1em/2 verdana, sans-serif;
    transition: .3s;
    
    &:hover {
      color: greenyellow;
      background: #333;
    }
  }
  
  input[type='range'] {
    -webkit-appearance: none;
    display: block;
    margin: .5em 0 1em;
    padding: 0;
    width: 100%; height: 1.5em;
    background: grey;

    &[id='hue'] {
      background: 
        linear-gradient(90deg, $hue-grad-list);
      
      &::-webkit-slider-runnable-track {
        @include h-track();
      }
      
      &::-moz-range-track { @include h-track(); }
      
      &::-ms-track { @include h-track(); }
    }
    
    &::-webkit-slider-runnable-track {
      -webkit-appearance: none;
      @include track();
    }
    
    &::-webkit-slider-thumb {
      -webkit-appearance: none;
      @include thumb();
    }
    
    &::-moz-range-track { @include track(); }
    
    &::-moz-range-thumb {
      border-radius: 0;
      @include thumb();
    }
    
    &::-ms-track {
      @include track();
      padding: 0;
      color: transparent;
    }
    
    &::-ms-thumb {
      @include thumb();
    }
    
    &::-ms-fill-lower, &::-ms-fill-upper {
      background: transparent;
    }
    
    &::-ms-tooltip { display: none; }
    
    &:focus { outline: none; }
  }
  
  input[type='radio'] {
    display: none;
    
    & + label {
      display: inline-block;
      color: lightsalmon;
      
      &:last-child { margin-right: .5em; }

      &:before {
        display: inline-block;
        margin: 0 .25em;
        transform: translateY(.125em);
        font: 1.5em/1 verdana, sans-serif;
        content: '☒';
      }
    }
    
    &:checked + label {
      color: greenyellow;
      
      &:before {
        content: '☑';
      }
    }
  }
  
  input:checked + label { color: greenyellow; }
  
  & &__btns {
    text-align: center;
    box-shadow: none;
  }
  
  [id='toggle'] {
    position: absolute;
    top: 100%; left: 0;
    width: 100%;
  }
}
              
            
!

JS

              
                Object.getOwnPropertyNames(Math).map(function(p) {
  window[p] = Math[p];
});

var rand = function(min, max, isInt) {
  var max = ((max - 1) || 0) + 1, 
      min = min || 0, 
      gen = min + (max - min)*random();
    
  return (isInt)?(~~gen):gen;
};

var randsign = function() {
  return (random() < .5)?-1:1;
};

var c = document.querySelectorAll('.c'), 
    w, h, min_dim, ctx =[], 
    ctrls = document.querySelector('.controls'), 
    ctrl_c = 'hsl(84, 99%, 69%)', 
    r1_ctrl = document.getElementById('r1'), 
    r1, r1_f, r1_c = 'hsl(328, 100%, 75%)', 
    r2_ctrl = document.getElementById('r2'), 
    r2, r2_f, r2_c = 'hsl(60, 100%, 75%)', 
    sign, r2_rot, r2_r, 
    trace_ctrl = document.getElementById('d'), 
    d, d_f, 
    hue_ctrl = document.getElementById('hue'), 
    hue, 
    trace_c, 
    hypo_ctrl = document.getElementById('hypo'), 
    epi_ctrl = document.getElementById('epi'), 
    v_ctrl = document.getElementById('v'), 
    v_f, 
    rand_ctrl = document.getElementById('rand'), 
    ok_ctrl = document.getElementById('apply'), 
    no_ctrl = document.getElementById('cancel'), 
    toggle = document.getElementById('toggle'),
    prev_pos = null, 
    theta = 0, u = PI/180;
    t = 0, rID = null, running = false;

var setUpCanvas = function() {
  var s = getComputedStyle(c[0]);
  
  w = ~~s.width.split('px')[0];
  h = ~~s.height.split('px')[0];
  min_dim = min(w, h);
  
  for(var i = 0; i < 3; i++) {
    c[i].width = w;
    c[i].height = h;
    ctx[i] = c[i].getContext('2d');
    ctx[i].lineWidth = 3;
    ctx[i].translate(w/2, h/2);
  }
  
  initValues();
};

var initValues = function() {
  r1_f = 1*r1_ctrl.value;
  r2_f = 1*r2_ctrl.value;
  d_f = 1*trace_ctrl.value;
  v_f = 1*v_ctrl.value;
  
  sign = (hypo_ctrl.checked)?-1:1;
  hue = ~~hue_ctrl.value;
  trace_c = 'hsl(' + hue + ', 100%, 56%)'
  
  r1 = r1_f*min_dim/2;
  r2 = r2_f*r1;
  d = d_f*r2;
  
  r2_rot = r1 + sign*r2;
  r2_r = r2_rot/r2;
  
  if(rID) {
    cancelAnimationFrame(rID);
    t = 0;
    theta = 0;
    prev_pos = null;
    
    for(var i = 0; i < 3; i++) {
      ctx[i].clearRect(-w/2, -h/2, w, h);
    }
  }
  
  styleSlider(r1_ctrl);
  styleSlider(r2_ctrl);
  styleSlider(trace_ctrl);
  styleSlider(v_ctrl);
  
  drawFixed();
  ctx[2].strokeStyle = trace_c;
  ani();
};

var resetValues = function() {
  r1_ctrl.value = r1_f;
  r2_ctrl.value = r2_f;
  trace_ctrl.value = d_f;
  v_ctrl.value = v_f;
  
  hypo_ctrl.checked = (sign < 0)?true:false;
  epi_ctrl.checked = (sign > 0)?true:false;
  
  hue_ctrl.value = hue;
  
  styleSlider(r1_ctrl);
  styleSlider(r2_ctrl);
  styleSlider(trace_ctrl);
  styleSlider(v_ctrl);
};

var randValues = function() {
  var tmps = randsign();
  
  r1_ctrl.value = rand(1*r1_ctrl.max, 1*r1_ctrl.min).toFixed(2);
  r2_ctrl.value = rand(1*r2_ctrl.max, 1*r2_ctrl.min).toFixed(2);
  trace_ctrl.value = rand(1*trace_ctrl.max, 1*trace_ctrl.min).toFixed(2);
  v_ctrl.value = rand(1*v_ctrl.max, 1*v_ctrl.min).toFixed(2);
  
  hypo_ctrl.checked = (tmps < 0)?true:false;
  epi_ctrl.checked = (tmps > 0)?true:false;
  
  hue_ctrl.value = ~~rand(1*hue_ctrl.max, 1*hue_ctrl.min);
  
  styleSlider(r1_ctrl);
  styleSlider(r2_ctrl);
  styleSlider(trace_ctrl);
  styleSlider(v_ctrl);
};

var drawFixed = function() {
  ctx[0].strokeStyle = r1_c;
  ctx[0].beginPath();
  ctx[0].moveTo(r1, 0);
  ctx[0].arc(0, 0, r1, 0, 2*PI);
  ctx[0].closePath();
  ctx[0].stroke();
};

var drawRoller = function(pc, pt) {
  var pc = { 'x': round(pc.x), 'y': round(pc.y) };
  
  ctx[1].clearRect(-w/2, -h/2, w, h);
  ctx[1].strokeStyle = r2_c;
  ctx[1].beginPath();
  ctx[1].moveTo(pc.x + r2, pc.y);
  ctx[1].arc(pc.x, pc.y, r2, 0, 2*PI);
  ctx[1].moveTo(pc.x + 2, pc.y);
  ctx[1].arc(pc.x, pc.y, 2, 0, 2*PI);
  ctx[1].moveTo(pc.x, pc.y);
  ctx[1].lineTo(pt.x, pt.y);
  ctx[1].closePath();
  ctx[1].stroke();
  
  ctx[1].strokeStyle = trace_c;
  ctx[1].beginPath();
  ctx[1].moveTo(pt.x, pt.y);
  ctx[1].arc(pt.x, pt.y, 3, 0, 2*PI);
  ctx[1].closePath();
  ctx[1].stroke();
};

var drawTracer = function(pt) {  
  if(prev_pos) {
    ctx[2].beginPath();
    ctx[2].moveTo(prev_pos.x, prev_pos.y);
    ctx[2].lineTo(pt.x, pt.y);
    ctx[2].closePath();
    ctx[2].stroke();
  }
  
  prev_pos = {'x': pt.x, 'y': pt.y};
};

var ani = function() {
  var pc, pt, j, k;
  
  if(running) {
    theta = v_f*t/60*2*PI/((r2_rot + d)/128);

    pc = {
      'x': r2_rot*cos(theta), 
      'y': r2_rot*sin(theta)
    };

    pt = {
      'x': round(pc.x - sign*d*cos(theta*r2_r)), 
      'y': round(pc.y - d*sin(theta*r2_r))
    };

    drawRoller(pc, pt);
    drawTracer(pt);
  }
  
  t++;
  
  j = floor(theta/PI);
  k = theta - j*PI;
  
  if(running && abs(theta) > PI && 
     abs(k) < 5*u && t > 1 && 
     abs(r2_rot - sign*d - pt.x) < 8 && 
     abs(pt.y) < 8) {
    running = false;
    
    pc = { 'x': r2_rot, 'y': 0 };
    pt = { 'x': r2_rot - sign*d, 'y': 0 };
    
    drawRoller(pc, pt);
    drawTracer(pt);
    
    t = 0;
  }
  else { if(running) { running = true; } }
  
  if(!running && t > 30) {
    ctx[2].clearRect(-w/2, -h/2, w, h);
    running = true;
    t = 0;
  }
  
  rID = requestAnimationFrame(ani);
};

var styleSlider = function(me, pre) {
  var span = me.max - me.min, 
      rel_val = (me.value - me.min)/span, 
      perc = round(rel_val*100), 
      txt_val = 'linear-gradient(90deg, ' + 
        ctrl_c + ' ' + perc + '%, grey ' + perc + '%)';
  
  me.style.background = txt_val;
};

setTimeout(function() {
  setUpCanvas();
  
  addEventListener('resize', setUpCanvas, false);
  
  r1_ctrl.addEventListener('input', function() {
    styleSlider(this);
  }, false);
  
  r1_ctrl.addEventListener('change', function() {
    styleSlider(this);
  }, false);
  
  r2_ctrl.addEventListener('input', function() {
    styleSlider(this);
  }, false);
  
  r2_ctrl.addEventListener('change', function() {
    styleSlider(this);
  }, false);
  
  trace_ctrl.addEventListener('input', function() {
    styleSlider(this);
  }, false);
  
  trace_ctrl.addEventListener('change', function() {
    styleSlider(this);
  }, false);
  
  v_ctrl.addEventListener('input', function() {
    styleSlider(this);
  }, false);
  
  v_ctrl.addEventListener('change', function() {
    styleSlider(this);
  }, false);
  
  rand_ctrl.addEventListener('click', randValues, false);
  ok_ctrl.addEventListener('click', function() {
    initValues();
    ctrls.classList.remove('controls--open');
  }, false);
  no_ctrl.addEventListener('click', resetValues, false);
  
  toggle.addEventListener('click', function() {
    ctrls.classList.toggle('controls--open');
  }, false);
}, 15);
              
            
!
999px

Console