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

              
                <!-- <div id="hud">
  <label>
    <input type="checkbox" id="spline" /> SplineCurve
  </label>
  <label>
    <input type="checkbox" id="uniform" checked /> Uniform Catmull-rom
    <input type="range" id="tension" min=0 max=1 step=0.01 value=0.5 /> <span id="tension_value"></span>
  </label>
  <label>
    <input type="checkbox" id="centripetal" checked /> Centripetal Catmull-rom
  </label>
  <label>
    <input type="checkbox" id="chordal" checked /> Chordal Catmull-rom
  </label>
  <p>(double click to add or remove points)</p>
  <p id="info">0,0</p>
</div> -->

<svg>
  
  <rect id="rect-1" class="bg" width="400" height="400" />
  <rect id="rect-2" class="bg" width="400" height="400" />

  <g id="group-1">
<!--     <rect class="bg" width="400" height="400" /> -->
    <path id="path-1a" />
    <path id="path-1b" />
    <path id="path-1c" />
    <circle class="handle handle-start" cx="0" cy="400" />
    <circle class="handle handle-end" cx="400" cy="0" />
    <circle id="handle-1a" class="handle" />
    <circle id="handle-1b" class="handle" />
  </g>
  <g id="group-2">
<!--     <rect class="bg" width="400" height="400" /> -->
    <path id="path-2" />
    <line id="line-2a" x1="0" y1="400" />
    <line id="line-2b" x1="400" y1="0" />
    <circle class="handle handle-start" cx="0" cy="400" />
    <circle class="handle handle-end" cx="400" cy="0" />
    <circle id="handle-2a" class="handle" />
    <circle id="handle-2b" class="handle" />
  </g>
</svg>



              
            
!

CSS

              
                // html, body {
//   height: 100%;
// }

// * {
//   box-sizing: border-box;
// }

// h1, p {
//   margin: 8px 0;
// }

// label {
//   display: block;
// }

// #hud {
//   position: absolute;
//   font-size: 14px;
//   padding: 10px;
//   top: 0;
//   left: 0;
//   z-index: 10;
  
//   border: 1px solid #ccc;
// }

body {
  overflow: hidden;
}

svg {
  // position: absolute;
  // top: 0;
  // left: 0;
  width: 100vw;
  height: 100vh;
}

path {
  fill: none;
  stroke: #555;
  stroke-width: 2;
}

#path-1a,
#path-1b,
#path-1c {
  stroke-opacity: 0.8;
}

#path-1a {
  stroke: #EA4335;
  stroke: orange;
  stroke: red;
  // stroke: tomato;
}

#path-1b {
  stroke: orange;
  stroke: #FBBC05;
  stroke: purple;
  stroke: blue;
  
  // stroke: violet;
  // stroke: #4285F4;
}

#path-1c {
  stroke: blue;
  stroke: #34A853;
  stroke: green;
  // stroke: orange;
}

// .surface {
//   width: 400;
//   height: 400;
//   position: absolute;
//   left: 200px;
  
//   background: #ccc;
// }

.bg {
  fill: #eaeaea;
  stroke: #777;
  stroke-width: 1;
}

.handle {
  stroke: #222;
  stroke-width: 1;
}

.handle-start,
.handle-end {
  fill: #fff;
}

#handle-1a,
#handle-2a {
  fill: cornflowerblue;
}

#handle-1b,
#handle-2b {
  fill: mediumvioletred;
}

line {
  stroke: #222;
  stroke-width: 1;
}



              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

var vw = window.innerWidth;
var vh = window.innerHeight;

var path1a = document.querySelector("#path-1a");
var path1b = document.querySelector("#path-1b");
var path1c = document.querySelector("#path-1c");
var path2  = document.querySelector("#path-2");

var tension = 1;

var settings = QuickSettings.create(15, 15, "Catmull-Roms")
  .addBoolean(" Chordal (green)", true, value => {
    TweenLite.set(path1c, { autoAlpha: value ? 1 : 0 });
  })
  .addBoolean(" Centripetal (blue)", true, value => {
    TweenLite.set(path1b, { autoAlpha: value ? 1 : 0 });
  })
  .addBoolean(" Uniform (red)", true, value => {
    TweenLite.set(path1a, { autoAlpha: value ? 1 : 0 });
  })
  .addRange("Uniform Tension", 0, 3, tension, 0.1, value => {
    tension = value;
    update();
  });

var p1 = [
  { x: 0,   y: 400 },
  { x: 100, y: 150 },
  { x: 350, y: 250 },
  { x: 400, y: 0 }
];

var p2 = [
  { x: 0,   y: 400 },
  { x: 100, y: 150 },
  { x: 350, y: 250 },
  { x: 400, y: 0 }
];

TweenLite.set("#group-1, #rect-1", { x: vw * 1/3 - 250, y: vh / 2 - 200 });
TweenLite.set("#group-2, #rect-2", { x: vw * 2/3 - 150, y: vh / 2 - 200 });
TweenLite.set(".handle", { attr: { r: 5}});

initArea(p1, 1);
initArea(p2, 2);
update();

function initArea(points, group) {
  
  var line1 = document.querySelector(`#line-${group}a`);
  var line2 = document.querySelector(`#line-${group}b`);  
  var handle1 = document.querySelector(`#handle-${group}a`);
  var handle2 = document.querySelector(`#handle-${group}b`);
  
  TweenLite.set(handle1, { x: points[1].x, y: points[1].y });
  TweenLite.set(handle2, { x: points[2].x, y: points[2].y }); 
  
  var drag1 = new Draggable(handle1, { onDrag: () => dragAction(drag1, points[1], line1) });
  var drag2 = new Draggable(handle2, { onDrag: () => dragAction(drag2, points[2], line2) });  
  
  if (group == 2) {
    moveLine(line1, points[1].x, points[1].y);
    moveLine(line2, points[2].x, points[2].y);
  }
    
  function dragAction(drag, point, line) {
    if (line) moveLine(line, drag.x, drag.y);    
    point.x = drag.x;
    point.y = drag.y;    
    update();
  }  
  
  function moveLine(line, x, y) {
    TweenLite.set(line, { attr: { x2: x, y2: y }});
  }
}

function update() {
  
  path1a.setAttribute("d", cardinal(p1, false, tension));
  path1b.setAttribute("d", catmullRom(p1, false, 0.5));
  path1c.setAttribute("d", catmullRom(p1, false, 1));
  
  var data = `M${p2[0].x},${p2[0].y} C ${p2[1].x},${p2[1].y},${p2[2].x},${p2[2].y},${p2[3].x},${p2[3].y}`;  
  path2.setAttribute("d", data);
}

function cardinal(data, closed, tension) {
  
  if (data.length < 1) return `M0,0`;
  if (tension == null) tension = 1;
  
  var size = data.length - (closed ? 0 : 1);
  var path = `M${data[0].x},${data[0].y} C`;
  
  for (var i = 0; i < size; i++) {
    
    var p0, p1, p2, p3;
    
    if (closed) {
      p0 = data[(i - 1 + size) % size];
      p1 = data[i];
      p2 = data[(i + 1) % size];
      p3 = data[(i + 2) % size];
      
    } else {
      p0 = i == 0 ? data[0] : data[i - 1];
      p1 = data[i];
      p2 = data[i + 1];
      p3 = i == size - 1 ? p2 : data[i + 2];
    }
        
    var x1 = p1.x + (p2.x - p0.x) / 6 * tension;
    var y1 = p1.y + (p2.y - p0.y) / 6 * tension;

    var x2 = p2.x - (p3.x - p1.x) / 6 * tension;
    var y2 = p2.y - (p3.y - p1.y) / 6 * tension;
    
    path += ` ${x1},${y1},${x2},${y2},${p2.x},${p2.y}`;
  }
  
  return closed ? path + "z" : path;
}

function catmullRom(data, closed, alpha) {
    
  if (data.length < 1) return `M0,0`;
  if (alpha == null) alpha = 0.5;
  
  var size = data.length - (closed ? 0 : 1);
  var path = `M${data[0].x},${data[0].y} C`;
  
  for (var i = 0; i < size; i++) {
    
    var p0, p1, p2, p3;
    
    if (closed) {
      p0 = data[(i - 1 + size) % size];
      p1 = data[i];
      p2 = data[(i + 1) % size];
      p3 = data[(i + 2) % size];
      
    } else {
      p0 = i == 0 ? data[0] : data[i - 1];
      p1 = data[i];
      p2 = data[i + 1];
      p3 = i == size - 1 ? p2 : data[i + 2];
    }
        
    var d1 = Math.sqrt(Math.pow(p0.x - p1.x, 2) + Math.pow(p0.y - p1.y, 2));
    var d2 = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
    var d3 = Math.sqrt(Math.pow(p2.x - p3.x, 2) + Math.pow(p2.y - p3.y, 2));
    
    var d3powA  = Math.pow(d3, alpha);
    var d3pow2A = Math.pow(d3, 2 * alpha);
    
    var d2powA  = Math.pow(d2, alpha);
    var d2pow2A = Math.pow(d2, 2 * alpha);
    
    var d1powA  = Math.pow(d1, alpha);
    var d1pow2A = Math.pow(d1, 2 * alpha);
    
    var A = 2 * d1pow2A + 3 * d1powA * d2powA + d2pow2A;
    var B = 2 * d3pow2A + 3 * d3powA * d2powA + d2pow2A;
    var N = 3 * d1powA * (d1powA + d2powA);
    
    if (N > 0) N = 1 / N;        
    var M = 3 * d3powA * (d3powA + d2powA);    
    if (M > 0) M = 1 / M;
        
    var x1 = (-d2pow2A * p0.x + A * p1.x + d1pow2A * p2.x) * N;
    var y1 = (-d2pow2A * p0.y + A * p1.y + d1pow2A * p2.y) * N;

    var x2 = (d3pow2A * p1.x + B * p2.x - d2pow2A * p3.x) * M;
    var y2 = (d3pow2A * p1.y + B * p2.y - d2pow2A * p3.y) * M;
        
    if (x1 == 0 && y1 == 0) {
      x1 = p1.x;
      y1 = p1.y;
    }
    
    if (x2 == 0 && y2 == 0) {
      x2 = p2.x;
      y2 = p2.y;
    }
        
    path += ` ${x1},${y1},${x2},${y2},${p2.x},${p2.y}`;
  }
  
  return closed ? path + "z" : path;  
}







              
            
!
999px

Console