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

              
                <main>
  <section>
    <svg id="stage">
      <g id="group">
        <path id="path1" class="path" />
        <path id="path2" class="path" />
        <circle id="orb" class="orb" r="10" />
      </g>
    </svg>
  </section>
  <section id="hud"></section>
</main>




              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  overflow: hidden;
  margin: 10px;
}

svg {
  position: absolute;
}

label, input {
  cursor: pointer;
}

main {
  visibility: hidden;
}

#stage {
  height: 100%;
  width: 100%;
  margin-left: 210px;
}

#hud {
  width: 200px;
}

.orb {
  stroke: #333;
  stroke-width: 2;
  fill: cornflowerblue;
}

.path {
  stroke: mediumvioletred;
  stroke: cornflowerblue;
  stroke: silver;
  stroke: #777;
  stroke-width: 5;
  fill: none;
}

.control {
  padding: 5px;
  margin: 5px;
  height: 20px;
}

.dashbox {
  width: 150px;
  height: 20px;
  overflow: hidden;
}

.dash {
  stroke: #333;
  stroke-width: 3;
}

.control-radio {
  vertical-align: middle;
  margin-right: 8px;
}


              
            
!

JS

              
                
var $$ = (query, context = document) => context.querySelector(query);

var group = $$("#group");
var path1 = $$("#path1");
var path2 = $$("#path2");
var svg   = $$("#stage");
var hud   = $$("#hud");
var orb   = $$("#orb");

var dashes = [
  [10, 5, 5, 5],
  [5, 5],
  [10, 20, 10],
  [20, 10],
  [5, 1],
  [1, 5],
  [0.9],
  [15, 10, 5],
  [5, 5, 2, 5]
];

var tl = new TimelineMax();

var cx = 150;
var cy = 100;
var rx = 250;
var ry = 150;

var circle1 = createCircle(path1, cx, cy, ry);
var circle2 = createCircle(path2, cx, cy, rx, ry);

var insert = hud.insertAdjacentHTML.bind(hud, "beforeend");
var radios = document.getElementsByClassName("control-radio");

dashes.forEach(createControl);

TweenLite.defaultEase = Power0.easeNone;
TweenLite.set(group, { x: cx, y: cy });
TweenLite.set("main", { autoAlpha: 1 });

changeDash();

function createControl(dash, index) {

  var checked = !index ? "checked" : "";  
  var control = `
    <div class="control">
      <label>
        <input class="control-radio" type="radio" name="dash" value="${index}" ${checked} />
        <svg class="dashbox">
          <path class="dash" d="M0,10 L200,10" stroke-dasharray="${dash}" />
        </svg>
      </label>
    </div>`;
  
  insert(control);
  
  //radios[index].addEventListener("change", changeDash);  
  radios[index].addEventListener("click", changeDash);  
}

function changeDash(event) {
    
  tl.clear();
  
  var index = event && event.target.value || 0;
  var dash  = dashes[index];
  var time  = 2;
  
  circle1.dash = dash;
  circle2.dash = dash;
  
  tl.to(circle1, time, { offset: 0 }, "startC1")
    .to(circle2, time, { offset: 0 }, "startC2")
    .to(circle1, time, { offset: -circle1.length }, "clearC1")
    .to(circle2, time, { offset: -circle2.length }, "clearC2")
    .to(orb, time, { bezier: circle1 }, "startC1")
    .to(orb, time, { bezier: circle2 }, "startC2")
    .to(orb, time, { bezier: circle1 }, "clearC1")
    .to(orb, time, { bezier: circle2 }, "clearC2");
}

function createCircle(node, cx, cy, rx, ry = rx) {

  var bezier = getBezier(cx, cy, rx, ry);    

  var circle = {
    node,
    bezier,    
    type: "cubic",
    values: bezier.values,       
    set path(d) { this.node.setAttribute("d", d); },
    get length() { return this.node.getTotalLength(); },
    get offset() { return this.node.getAttribute("stroke-dashoffset"); },
    set offset(val) { this.node.setAttribute("stroke-dashoffset", val); },
    get dash() { return this.node.getAttribute("stroke-dasharray"); },
    set dash(val) { 
      var dash = getDash(val, this.length);
      this.node.setAttribute("stroke-dasharray", dash); 
      this.offset = this.length;
    }
  };
  
  circle.path = bezier.path;    
  circle.dash = dashes[0];

  return circle;
}

function getDash(pattern, length) {

  var dash = [];
  var full = false;       
  var len  = pattern.length;
  var sum  = 0; 

  while (!full) {        
    for (var i = 0; i < len; i++) {
      var val = pattern[i];
      if (sum + val >= length) {
        full = true; break;                
      }
      sum += val;
      dash.push(val)
    }
  }    

  var last = length - sum;
  dash.length % 2 ? dash.push(0, last, length) : dash.push(last, length);
  return dash;
}

function getBezier(cx, cy, rx, ry) {

  var k = 1.81; 
  var path = "";

  var segments = [
    [(cx), (cy - ry)],
    [(cx + rx / k), (cy - ry), (cx + rx), (cy - ry / k), (cx + rx), (cy)],
    [(cx + rx), (cy + ry / k), (cx + rx / k), (cy + ry), (cx), (cy + ry)],
    [(cx - rx / k), (cy + ry), (cx - rx), (cy + ry / k), (cx - rx), (cy)],
    [(cx - rx), (cy - ry / k), (cx - rx / k), (cy - ry), (cx), (cy - ry)]
  ];    

  var values = segments.reduce((result, segment, index) => {

    var i = 0, len = segment.length;
    while (i < len) {
      result.push({
        x: segment[i++],
        y: segment[i++]
      });
    }
    path += (index ? "C" : "M") + segment;
    return result;
  }, []);

  path += "z";

  return { path, values };
}







              
            
!
999px

Console