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

              
                <svg>
  <path />
  <polyline id="poly1" />
  <polyline id="poly2" />
  <g id="group1"></g>
  <g id="group2"></g>
</svg>

              
            
!

CSS

              
                body {
  overflow: hidden;
  background: #2a2a2a;
}

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

path {
  fill: none;
  stroke: #dadada;
  stroke-opacity: 0.3;
  stroke-width: 1;
  
  stroke: #dadada;
  stroke-opacity: 1;
}

#poly1 {
  fill: none;
  stroke: #ff9900;
  stroke: #ddd;
  stroke: cornflowerblue;
  stroke-width: 1.5;
}

#poly2 {
  fill: none;
  stroke: #fc0000;
  stroke: tomato;
  stroke-width: 1.5;
}

#group1 circle {
  fill: cornflowerblue;
  stroke: #333;
  stroke-width: 1.5;  
}

#group2 circle {
  fill: tomato;
  stroke: #333;
  stroke-width: 1.5;  
}

              
            
!

JS

              
                
var bezier1 = [
  55,296,96.31,369.22,149.56,435.7,212,492,263.44,538.38,322.88,578.67,390.44,593.92,424.22,601.54,459.68,602.66,493.49,595.17,527.3,587.68,559.4,571.37,584,547,622,509.35,640,453.74,635.85,400.41,631.71,347.08,606.6,296.43,570.04,257.38,533.48,218.33,485.98,190.47,435.47,172.83,384.97,155.2,331.44,147.47,278,145,247.42,143.59,216.13,143.97,187.14,153.78,172.64,158.69,158.85,165.97,147.24,175.93,135.62,185.9,126.22,198.61,121,213,115.11,229.22,114.69,247.23,118.85,263.98,123.02,280.73,131.64,296.23,142.93,309.28,165.51,335.39,197.88,351.26,231,361,331.73,390.63,445.48,366.64,529,303,565.52,275.17,597,240.13,637.63,218.76,657.95,208.07,680.57,201.02,703.53,201.32,726.49,201.62,749.78,209.76,766,226,775.97,235.98,783.04,248.74,786.69,262.37,790.34,276,790.62,290.46,787.98,304.32,782.71,332.04,765.94,356.75,744.42,375.01,722.91,393.26,696.85,405.49,670.05,414.31,643.24,423.12,615.5,428.7,588,435,409.9,475.82,239.11,548.29,86,648
];

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

var piTwo = Math.PI / 2;
var atan2 = (p1, p2) => Math.atan2(p1.y - p2.y, p1.x - p2.x);
var hypot = (p1, p2) => Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2);
var mid   = (p1, p2) => ({ x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 });

var root   = document.querySelector("svg");
var path   = document.querySelector("path");
var poly1  = document.querySelector("#poly1");
var poly2  = document.querySelector("#poly2");
var group1 = document.querySelector("#group1");
var group2 = document.querySelector("#group2");

var len   = bezier1.length;
var first = { x: bezier1[0], y: bezier1[1] };
var last  = { x: bezier1[len - 2], y: bezier1[len - 1] };

var data = `M${first.x},${first.y} C`;
var bezier2 = [first];

for (var i = 2; i < len; i += 2) {  
  var x = bezier1[i];
  var y = bezier1[i+1];  
  data += ` ${x} ${y}`;
  bezier2.push({ x, y });
}

path.setAttribute("d", data);

var tolerance = 30;

var settings = QuickSettings.create(10, 10, "Settings")
  .addRange("Tolerance", 0, 50, tolerance, 1, update)
  .addBoolean(" Adaptive", true, value => {
    group1.style.visibility = value ? "visible" : "hidden";
    poly1.style.visibility = value ? "visible" : "hidden";
  })
  .addBoolean(" Flatten", false, value => {
    group2.style.visibility = value ? "visible" : "hidden";
    poly2.style.visibility = value ? "visible" : "hidden";
  })
  .setBoolean(" Flatten", false);

update(tolerance);

function update(value) {
  
  if (!value) value = 0.3;
  
  while (group1.firstChild) {
    group1.removeChild(group1.firstChild);
  }
  
  while (group2.firstChild) {
    group2.removeChild(group2.firstChild);
  }
  
  contour(bezier1, value, poly1, group1);
  flatten(bezier2, value, poly2, group2);  
}

function contour(bezier, tolerance, polyline, group) {
    
  var x0 = first.x;
  var y0 = first.y;
  
  var size = bezier.length;
  
  var points = [first];
  var coords = [first.x, first.y];  
  
  for (var i = 2; i < size; i += 6) {
    
    var x1 = bezier[i];
    var y1 = bezier[i+1];
    var x2 = bezier[i+2];
    var y2 = bezier[i+3];
    var x3 = bezier[i+4];
    var y3 = bezier[i+5];
    
    addPoint(x0, y0, x1, y1, x2, y2, x3, y3);
    
    x0 = x3;
    y0 = y3;
  } 
  
  points.push(last);
  coords.push(last.x, last.y);
  
  polyline.setAttribute("points", coords); 
  
  points.forEach(point => {
    
    var circle = create("circle");
    group.appendChild(circle);      
    
    circle.setAttribute("cx", point.x);
    circle.setAttribute("cy", point.y);
    circle.setAttribute("r",  3.5);
  });
  
  
  function addPoint(x1, y1, x2, y2, x3, y3, x4, y4) {
    
    // Calculate all the mid-points of the line segments
    var x12 = (x1 + x2) / 2;
    var y12 = (y1 + y2) / 2;
    var x23 = (x2 + x3) / 2;
    var y23 = (y2 + y3) / 2;
    var x34 = (x3 + x4) / 2;
    var y34 = (y3 + y4) / 2;
    
    var x123  = (x12 + x23) / 2;
    var y123  = (y12 + y23) / 2;
    var x234  = (x23 + x34) / 2;
    var y234  = (y23 + y34) / 2;
    var x1234 = (x123 + x234) / 2;
    var y1234 = (y123 + y234) / 2;
    
    // Try to approximate the full cubic curve by a single straight line
    var dx = x4 - x1;
    var dy = y4 - y1;
    
    var d2 = Math.abs(((x2 - x4) * dy - (y2 - y4) * dx));
    var d3 = Math.abs(((x3 - x4) * dy - (y3 - y4) * dx));
    
    if ((d2 + d3) * (d2 + d3) < tolerance * (dx * dx + dy * dy)) {
      points.push({ x: x1234, y: y1234 });    
      coords.push(x1234, y1234); 
      return;
    }
    
    // Continue subdivision
    addPoint(x1, y1, x12, y12, x123, y123, x1234, y1234);
    addPoint(x1234, y1234, x234, y234, x34, y34, x4, y4);    
  }
}

function flatten(bezier, tolerance, polyline, group) {

  var size = bezier.length;
  var prev = first;
    
  var points = [first];
  var coords = [first.x, first.y];  
  
  for (var i = 1; i < size; i += 3) {
    
    addPoint(prev, bezier[i], bezier[i+1], bezier[i+2]);
    prev = bezier[i+2];
  } 
  
  points.push(last);
  coords.push(last.x, last.y);
  
  polyline.setAttribute("points", coords); 
  
  points.forEach(point => {
    
    var circle = create("circle");
    group.appendChild(circle);      
    
    circle.setAttribute("cx", point.x);
    circle.setAttribute("cy", point.y);
    circle.setAttribute("r",  3.5);
  });  
  
  function addPoint(p0, p1, p2, p3) {

    var pt1 = mid(p0, p1);
    var pt2 = mid(p1, p2);
    var pt3 = mid(p2, p3);  
    var pt4 = mid(pt1, pt2);
    var pt5 = mid(pt2, pt3);  
    var pt6 = mid(pt4, pt5);

    var a  = atan2(p3, p0);
    var b  = atan2(p1, p0);
    var c  = atan2(p3, p2);
    var h1 = hypot(p0, p1);
    var h2 = hypot(p3, p2);
    var d1 = Math.abs(Math.sin(b - a) * h1);
    var d2 = Math.abs(Math.sin(c - a) * h2);

    var dmax  = d1 > d2 ? d1 : d2;
    var angle = piTwo - atan2(pt4, pt5);

    if (dmax > tolerance) {
      addPoint(p0, pt1, pt4, pt6);
      points.push({ x: pt6.x, y: pt6.y, angle});    
      coords.push(pt6.x, pt6.y);    
      addPoint(pt6, pt5, pt3, p3);
    } else {
      points.push({ x: pt6.x, y: pt6.y, angle});    
      coords.push(pt6.x, pt6.y);
    }
  }
}



              
            
!
999px

Console