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 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

              
                <div class="hud">
  Click or press arrow keys for directional change<br>
<!--   <button id="tween-xy">XY</button>
  <button id="tween-x">X</button>
  <button id="tween-y">Y</button>  -->
</div>
  
<svg id="svg">
  <path id="path" />  
  <g id="group"></g>  
</svg>


              
            
!

CSS

              
                hmtl, body {
  height: 100%;
}

body {  
  background:#11252f;
  overflow: hidden;
  color: #ddd;
  user-select: none;
}

#svg {
  position: absolute;
  width: 1000%;
  height: 1000%;
  top: 0;
  left: 0;
}

.point {
  stroke: #fafafa;
  fill: #ff002f;
}

#path {
  fill: none;
  stroke: white;
  stroke: #fafafa;
}

.hud {
  position: absolute;
  left: 15px;
  top: 15px;
  z-index: 10;
}

button {
  color: black;
  padding: 6px 4px;
  margin: 8px;
  margin-left: 0;
  width: 50px;
}

              
            
!

JS

              
                // KEYBOARD
// XY => space/z
// X  => left/right arrows
// Y  => up/down arrows

console.clear();

var log = console.log.bind(console);
var $   = document.querySelector.bind(document);

var xmlns  = "http://www.w3.org/2000/svg";
var create = document.createElementNS.bind(document, xmlns);

// var ease = Sine.easeInOut;

var duration  = 1;
var numPoints = 6;

var startX = 25;
var startY = 300;
var width  = 1000;
var height = 200;

var stage = $("#svg");
var path  = $("#path");
var group = $("#group");

//
// CUSTOM EASE
// ===========================================================================
class CustomEase extends Ease {  

  constructor(omega = 10, zeta = 0.75) {
    super();
    this.omega = omega;
    this.zeta  = zeta;
  }  
  
  getRatio(progress) {
        
    var omega = this.omega;
    var zeta  = this.zeta;
    var beta  = Math.sqrt(1.0 - zeta * zeta);
    
    progress = 1 - Math.cos(progress * Math.PI / 2);   
    progress = 1 / beta * 
      Math.exp(-zeta * omega * progress) * 
      Math.sin( beta * omega * progress + Math.atan(beta / zeta));
    
    return 1 - progress;
  }  
}

var ease = new CustomEase(10, 0.9);
// ease = new CustomEase(30, 0.5);

//
// INIT
// ===========================================================================
function init() {

  TweenLite.set([path, group], { x: startX, y: startY });
  
  var spline = new Spline();
      
  // $("#tween-xy").addEventListener("click", () => spline.addTween(true,  true));
  // $("#tween-x").addEventListener("click",  () => spline.addTween(true,  false));
  // $("#tween-y").addEventListener("click",  () => spline.addTween(false, true));
    
  document.addEventListener("click", () => spline.addTween(true,  true));
  
  document.addEventListener("keyup", event => {
    
    switch (event.keyCode) {        
      case 90: case 32: return spline.addTween(true,  true);  // XY: z, space
      case 37: case 39: return spline.addTween(true,  false); // X:  ←,→
      case 38: case 40: return spline.addTween(false, true);  // Y:  ↑,↓
    }
  });   
  
  TweenLite.ticker.addEventListener("tick", spline.render.bind(spline));
}

//
// SPLINE
// ===========================================================================
class Spline {
  
  constructor() {
    
    this.points = new LinkedList();
    
    var last  = numPoints - 1;    
    var range = 360 / numPoints;
    var space = width / last;
        
    for (var i = 0; i < numPoints; i++) {
            
      var color = `hsl(${i * range},100%,50%)`;
      var point = new Point(i * space, 0, space, height, color, i);
      
      if (i === 0) point.first = true;
      if (i === last) point.last  = true;
      
      this.points.add(point);
    } 
  }
  
  addTween(x, y) {
    
    var size  = this.points.size;
    var point = this.points.first;
    
    while (size--) {
      point.addTween(x, y);
      point = point.next;
    }
    
    return this;
  }
    
  solve(data) {
            
    var size = data.length;
    var last = size - 4;    
    
    var d = `M${data[0]},${data[1]}`;
        
    for (var i = 0; i < size - 2; i +=2) {
            
      var x0 = i ? data[i - 2] : data[0];
      var y0 = i ? data[i - 1] : data[1];
      
      var x1 = data[i + 0];
      var y1 = data[i + 1];
      
      var x2 = data[i + 2];
      var y2 = data[i + 3];
      
      var x3 = i !== last ? data[i + 4] : x2;
      var y3 = i !== last ? data[i + 5] : y2;
      
      var cp1x = (-x0 + 6 * x1 + x2) / 6;
      var cp1y = (-y0 + 6 * y1 + y2) / 6;
      
      var cp2x = (x1 + 6 * x2 - x3) / 6;
      var cp2y = (y1 + 6 * y2 - y3) / 6;
            
      d += `C ${cp1x},${cp1y} ${cp2x},${cp2y} ${x2},${y2}`;    
    } 
    
    path.setAttribute("d", d);
  }
  
  render() {
        
    var size  = this.points.size;
    var point = this.points.first;    
    var data  = [];
           
    while (size--) {
      point.render(data);
      point = point.next;
    }
      
    if (data.length < 3) return;
    
    this.solve(data);
  }
}

//
// RELATIVE TWEEN
// ===========================================================================
class RelativeTween {
  
  constructor(scope, x1, y1, x2, y2, node) {
        
    var onComplete = () => scope.removeTween(this, node);
    
    var signX = x1 < x2 ? -1 : 1;
    var signY = y1 < y2 ? -1 : 1;
    
    this._x = Math.abs(x1 - x2) * signX;
    this._y = Math.abs(y1 - y2) * signY;
    
    this.tween = TweenLite.to(this, duration, { x: 0, y: 0, ease, onComplete });      
    this.node  = node;
    
    if (scope.tweens.last) scope.tweens.last.hide();    
  }
  
  get x() { return 0 - this._x; }
  get y() { return 0 - this._y; }
  
  set x(x) { this._x = x;}  
  set y(y) { this._y = y;}  
  
  hide() {    
    var time = (1 - this.tween.progress()) * duration;
    TweenLite.to(this.node, time, { scale: 0, autoAlpha: 0 });
  }
}

//
// POINT
// ===========================================================================
class Point {
  
  constructor(x, y, w, h, color, index) {
    
    this.x  = x;
    this.y  = y;  
    
    this.x1 = x;
    this.x2 = x + w;
    this.w2 = w * 0.5;
    this.h2 = h * 0.5;
    
    this.node  = createNode(x, y, color);
    this.color = color;
    this.index = index;
       
    this.first = false;
    this.last  = false;    
       
    this.sign  = !(index & 1) ? -1 : 1;
    this.gap   = 100;
    
    this.tweens = new LinkedList();
  }
    
  addTween(tweenX, tweenY) {
    
    var x = tweenX ? _.random(this.w2) * this.sign : this.x;
    var y = tweenY ? _.random(this.h2) * this.sign : this.y;
    
    if (tweenX) {
      
      var x1 = this.first ? this.x1 : this.prev.x + this.gap;
      var x2 = this.last  ? this.x2 : this.next.x - this.gap;
      
      x = _.clamp(x + this.x1, x1, x2);
    }
    
    this.sign *= -1;
    
    var point = createNode(x, y, this.color, 0.6);
    var tween = new RelativeTween(this, this.x, this.y, x, y, point);
    
    this.tweens.add(tween);
    
    this.x = x;
    this.y = y;
       
    return this;    
  }
    
  removeTween(tween, point) {
    
    group.removeChild(point);
    this.tweens.remove(tween);
    return this;
  }
  
  render(data) {
    
    var size = this.tweens.size;
    
    if (!size) {
      data.push(this.x, this.y);
      return data;
    }    
    
    var tween = this.tweens.last;   
    
    var x = this.x;
    var y = this.y;
    
    while (size--) {
      x += tween.x;
      y += tween.y;
      tween = tween.prev;
    }
        
    TweenLite.set(this.node, { x, y });
        
    data.push(x, y);
    return data;
  }
}

//
// LINKED LIST
// ===========================================================================
class LinkedList {

  constructor() {

    // Alias
    this.add = this.append;
    this.clear();
  }

  get isEmpty() { return !this.size && !this.first && !this.last; }

  static fromArray(array) {

    var list = new LinkedList();
    var size = array.length;

    while (size--) list.prepend(array[size]);
    return list;
  }
  
  clear() {

    this.size  = 0;
    this.first = null;
    this.last  = null;
    this.next  = null;
    this.prev  = null;

    return this;
  }

  get(i) {

    if (this.isEmpty) return null;

    var node = this.first;
    var size = i % this.size;

    while (size--) node = node.next;
    return node;
  }

  random() {

    var n = Math.random() * this.size >> 0;
    return this.get(n);
  }

  toArray() {

    var array = [];
    var node  = this.first;
    var size  = this.size;

    while (size--) {
      array.push(node);
      node = node.next;
    }

    return array;
  }

  forEach(callback, scope) {

    var node = this.first;
    var size = this.size;

    for (var i = 0; i < size; i++) {
      callback.call(scope, node, i);
      node = node.next;
    }
  }

  append(node) {
   
    if (this.first === null) {

      node.prev = node;
      node.next = node;

      this.first = node;
      this.last  = node;
      this.next  = node;

    } else {

      node.prev = this.last;
      node.next = this.first;

      this.last.next = node;
      this.last      = node;
    }
    
    this.size++;
    return node;
  }

  prepend(node) {

    if (this.first === null) {

      return this.append(node);

    } else {

      node.prev = this.last;
      node.next = this.first;

      this.first.prev = node;
      this.last.next  = node;
      this.first      = node;
    }

    this.size++;
    return node;
  }

  remove(node) {

    if (this.size > 1) {

      node.prev.next = node.next;
      node.next.prev = node.prev;

      if (node === this.first) this.first = node.next;
      if (node === this.last)  this.last = node.prev;

    } else {

      this.first = null;
      this.last  = null;
    }

    node.prev = null;
    node.next = null;

    this.size--;    
    return node;
  }

  insertBefore(node, newNode) {

    newNode.prev = node.prev;
    newNode.next = node;

    node.prev.next = newNode;
    node.prev      = newNode;

    if (newNode.next === this.first) this.first = newNode;

    this.size++;
    return newNode;
  }

  insertAfter(node, newNode) {

    newNode.prev = node;
    newNode.next = node.next;

    node.next.prev = newNode;
    node.next      = newNode;

    if (newNode.prev === this.last) this.last = newNode;

    this.size++;
    return newNode;
  }
}

//
// CREATE CIRCLE
// ===========================================================================
function createNode(x, y, fill, alpha = 1) {
   
  var node = create("circle"); 
  var attr = { class: "point", r: 3.5 };
  
  group.appendChild(node);
  
  TweenLite.set(node, { alpha, attr, fill, x, y});
  return node;
}

//////////
init();












              
            
!
999px

Console