css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="hud">
  <div class="control">
    Tolerance
    <input id="range" type="range" min="1" max="50" value="10" />
    <span id="value"></span>
  </div>
  <div class="control">
    <label>
      <input id="bounds" type="checkbox" checked />
      Show Bounds
    </label>
  </div>
</div>

<svg>  
  <path id="path" d="M442,110C401.31878469950175,105.57767648800454,359.47080848998564,112.94342312451512,322.5930893286429,130.6790385079006C285.7153701746909,148.41465390023046,253.89786154244618,176.43033534034151,231.34676233632214,210.57658180957964C208.7956631130965,244.72282826996718,195.5360786961558,284.89019400895984,192.97906876309293,325.7311039365253C190.4220588365102,366.5720138614294,198.53656628326283,407.99261281744003,216,445C232.74721449959134,480.4896214127731,258.1313932786577,511.9789423167759,289.7721982744892,535.1919102599977C321.4130032632071,558.4048781616015,359.2948131880203,573.2147263100956,398.3760300537772,576.7701316915739C437.45724694346205,580.3255371266929,477.6233856848572,572.5064154600085,512.1223824796566,553.8034306303325C546.6213792749674,535.1004458010804,575.2415818134962,505.48433233544563,592,470C615.199253709763,420.8778202894561,614.4143475674808,360.96282026702283,589.4359289914801,312.7210073072364C564.4575104026865,264.47919434796313,515.7089126299984,229.15761097979646,462,221C431.9955631730854,216.44275779067857,400.49063874061335,220.16324740568174,373.19445604416256,233.42797869043505C345.89827337266394,246.6927099522216,323.11712349483867,269.76094461417404,312,298C303.6156886450541,319.29732867184487,301.97321711676113,343.1145852444666,306.84733345086823,365.47785592240746C311.7214497732169,387.84112655784713,323.03065643788875,408.6935880779201,338.7386340265318,425.3408798550347C354.4466116573395,441.9881716845046,374.47769085397937,454.43307999933916,396.20041418118427,461.643917520404C417.92313749207136,468.8547550469652,441.29188746696923,470.8660968342038,464,468C485.3191433839489,465.3092114258893,506.3148761097661,458.22322826208676,523.7846374242501,445.71108786202353C541.2543987391783,433.198947465062,555.0069596059385,415.0353166590521,560.4053442184367,394.23619020409296C565.8037288286658,373.4370637470417,562.3023380305032,350.0969446534396,549.4571191475032,332.87061104398595C543.0345097083332,324.2574442289445,534.4043505922914,317.2643672171917,524.5420398503556,313.0015768843996C514.6797291156685,308.7387865692055,503.60006599915647,307.2463879812324,493,309C481.51165905224434,310.9005629499548,470.8728095136464,316.5026338251378,461.70424678901077,323.6811834574133C452.53568406676214,330.8597330890791,444.7147021324714,339.59573349548094,437.2190496724121,348.5069237304666C429.72339721013634,357.41811396653054,422.48179703455213,366.5745250429274,414.2492160788833,374.8097377339816C406.01663512364445,383.0449504235981,396.69536983844273,390.39530877835324,386,395C369.48569090256706,402.10992655980533,350.2828362404378,402.1761587115694,333.305126622215,396.25747331946684C316.3274170193578,390.3387879159263,301.59468026681895,378.66978060702496,290.96704744932845,364.1671297816885C280.3394146056189,349.6644789617498,273.717024653147,332.42616634019936,270.7018651994973,314.7009787774448C267.68670574483895,296.9757912175643,268.1997141830885,278.76040145699307,271,261"></path>
    
  <g id="outer">
    <g id="inner"></g>
  </g>
  
  <line id="dist"></line>
  <line id="edge"></line>
</svg>
            
          
!
            
              body {
  overflow: hidden;
  background: #111;
  color: #fff;
}

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

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

#edge {
  stroke: #ec0000;  
  stroke-width: 4;
}

#dist {
  stroke: orange;
  stroke-width: 2;
}

.hud {
  position: absolute;
  background: #111;
  z-index: 10;
  top: 0;
  left: 0;
  border: 1px solid #222;
  padding: 10px;
}

.control {
  padding: 6px;
}

[type=range] {
  margin: 0 10px;
}

input {
  vertical-align: middle;  
}

rect {
  fill: none;
  stroke: cornflowerblue;
}


            
          
!
            
              console.clear();

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

var svgns  = "http://www.w3.org/2000/svg";
var create = document.createElementNS.bind(document, svgns);
var mouse  = { x: 0, y: 0 };
var points = [];

var svg   = $$("svg");
var range = $$("#range");
var value = $$("#value");
var rects = $$("#bounds");
var inner = $$("#inner");
var outer = $$("#outer")
var path  = $$("#path");
var edge  = $$("#edge");
var dist  = $$("#dist");

var threshold = 15; //px
var tolerance = 25;

var values = MorphSVGPlugin.pathDataToBezier("#path");
var total  = values.length;

rects.addEventListener("change", toggleBounds);
range.addEventListener("input",  update);
range.addEventListener("change", update);

document.body.addEventListener("mousemove", event => {
  mouse.x = event.clientX;
  mouse.y = event.clientY;
  hitTest(mouse);
});

update();

//
// UPDATE
// ===========================================================================
function update() {

  clearInner();

  tolerance = value.innerHTML = +range.value;
  
  points = getContour(values, tolerance);

  var total = points.length - 1;

  for (var i = 0; i < total; i++) {
    
    var p1 = points[i];
    var p2 = points[i + 1];    
    addRect(p1, p2);
  } 
}

//
// GET CONTOUR
// ===========================================================================
function getContour(bezier, tolerance) {
     
  var points = [bezier[0]];
  var total  = bezier.length;
  
  for (var i = 1; i < total;) {
    var p1 = bezier[i-1];
    var p2 = bezier[i++];
    var p3 = bezier[i++];
    var p4 = bezier[i++];
    addPoint(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
  } 
  
  points.push(bezier[total - 1]);
  
  return points;
  
  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 });    
      return;
    }
    
    // Continue subdivision
    //----------------------
    addPoint(x1, y1, x12, y12, x123, y123, x1234, y1234);
    addPoint(x1234, y1234, x234, y234, x34, y34, x4, y4); 
  }
}

//
// HIT TEST
// ===========================================================================
function hitTest(point) {
  
  var total = points.length - 1;
  
  var res = {
    edge:   0,
    point:  {},
    dist:   Infinity,
    distSq: Infinity
  };
  
  for (var i = 0; i < total; i++) {
    
    var p1 = points[i];
    var p2 = points[i + 1] || points[0];
    
    pointDist(point, p1, p2, i, res);
  }
    
  var e1 = points[res.edge];
  var e2 = points[res.edge + 1];
  
  TweenLite.set(edge, { 
    stroke: res.dist < threshold ? "green" : "#ec0000",
    attr: {    
      x1: e1.x, 
      y1: e1.y, 
      x2: e2.x, 
      y2: e2.y,
    }});
  
  TweenLite.set(dist, { attr: {
    x1: point.x, 
    y1: point.y, 
    x2: res.point.x, 
    y2: res.point.y
  }});
}

//
// POINT DIST
// ===========================================================================
function pointDist(point, p1, p2, edge, res) {

  var x  = point.x;
  var y  = point.y;
  var x1 = p1.x;
  var y1 = p1.y;
  var x2 = p2.x;
  var y2 = p2.y;

  var A = x  - x1;
  var B = y  - y1;
  var C = x2 - x1;
  var D = y2 - y1;

  var dotProd = A * C + B * D;
  var distSq  = C * C + D * D;
  var determ  = dotProd / distSq;

  var xx, yy;

  if (determ < 0 || (x1 === x2 && y1 === y2)) {
    xx = x1;
    yy = y1;

  } else if (determ > 1) {
    xx = x2;
    yy = y2;

  } else {
    xx = x1 + determ * C;
    yy = y1 + determ * D;        
  }

  var dx = x - xx;
  var dy = y - yy;
  
  distSq = dx * dx + dy * dy;
  
  if (distSq < res.distSq) {

    res.edge    = edge;
    res.dist    = Math.sqrt(distSq);
    res.distSq  = distSq;    
    res.point.x = xx;
    res.point.y = yy;
  }
}

//
// ADD RECT
// ===========================================================================
function addRect(p1, p2) {
  
  var rect = create("rect");
  inner.appendChild(rect);
  
  var x1 = Math.min(p1.x, p2.x);
  var x2 = Math.max(p1.x, p2.x);
  var y1 = Math.min(p1.y, p2.y);
  var y2 = Math.max(p1.y, p2.y);
  
  rect.setAttribute("x", x1);
  rect.setAttribute("y", y1);
  rect.setAttribute("width",  x2 - x1);
  rect.setAttribute("height", y2 - y1);
  
  return rect;
}

//
// CLEAR INNER
// ===========================================================================
function clearInner() {
  while (inner.lastChild) {
    inner.removeChild(inner.lastChild);
  }
}

//
// TOGGLE BOUNDS
// ===========================================================================
function toggleBounds(event) {
  var autoAlpha = event.target.checked ? 1 : 0;
  TweenLite.set(outer, { autoAlpha });
}

//
// RENDER
// ===========================================================================
function render() {  
  hitTest(mouse);
}









            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console