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

              
                <svg>
  <g>
    <path id="path-1" />
    <path id="path-2" />
    <path id="path-3" />
    <polygon points="0,0 200,200 200,0 0,200" />
  </g>  
</svg>
              
            
!

CSS

              
                svg {
  position: absolute;
  width: 100%;
  height: 100%;
}

g {
  visibility: hidden;
}

polygon {
  fill: none;
  stroke: #000;
  stroke-width: 1;
}

path {
  stroke-width: 1;
  fill-opacity: 0.1;
}

#path-1 {
  stroke: blue;
  fill: blue;
}

#path-2 {
  stroke: red;
  fill: red;
}

#path-3 {
  stroke: green;
  fill: green;
}

              
            
!

JS

              
                
var polygon = document.querySelector("polygon");
var path1 = document.querySelector("#path-1");
var path2 = document.querySelector("#path-2");
var path3 = document.querySelector("#path-3");

var tension = 1;

var points = [
  { x: 0, y: 0 },
  { x: 200, y: 200 },
  { x: 200, y: 0 },
  { x: 0, y: 200 }
];

var settings = QuickSettings.create(10, 10, "Catmull-Roms")
  .addBoolean(" Chordal (green)", true, value => {
    TweenLite.set(path3, { autoAlpha: value ? 1 : 0 });
  })
  .addBoolean(" Centripetal (red)", true, value => {
    TweenLite.set(path2, { autoAlpha: value ? 1 : 0 });
  })
  .addBoolean(" Uniform (blue)", true, value => {
    TweenLite.set(path1, { autoAlpha: value ? 1 : 0 });
  })
  .addRange("Uniform Tension", 0, 3, tension, 0.1, value => {
    path1.setAttribute("d", cardinal(points, true, value));
  });

path1.setAttribute("d", cardinal(points, true, tension));
path2.setAttribute("d", catmullRom(points, true, 0.5));
path3.setAttribute("d", catmullRom(points, true, 1));

TweenLite.set("g", { x: 300, y: 50, autoAlpha: 1 });

// Cardinal spline is a uniform catmull-rom (alpha = 0) with a tension option
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 c1x = (-d2pow2A * p0.x + A * p1.x + d1pow2A * p2.x) * N;
    var c1y = (-d2pow2A * p0.y + A * p1.y + d1pow2A * p2.y) * N;

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



              
            
!
999px

Console