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

              
                <body>
  <div id="app"></div>
</body>
              
            
!

CSS

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

#app {
  width: 100%;
  height: 100%;
  padding: 15px;
  font-size: 18px;
}

.pointSpan {
  padding-left: 8px;
  padding-right: 8px;
}

ul {
  list-style: none;
}
              
            
!

JS

              
                const {map, filter, pipe, isEmpty, not, flatten, prop} = R;

// Fake 3D scanner - this part can be ignored!

const SCANS_PER_BUFFER = 20;

const fake3DScanner$ =
      Rx.Observable.interval(10)
      .map(iteration => iteration % 200)
      .map(toFakeScan)
      .bufferCount(SCANS_PER_BUFFER)
      .startWith([]);

function toFakeScan(y) {
  let points = [];

  for (let x = 0; x < 40; ++x) {
    points.push({
      x,
      y: y % SCANS_PER_BUFFER,
      z: calcZ(x, y)
    });
  }

  return points;
}

function calcZ(x, y) {
  // Intentionally offset the y value so that isn't a multiple of SCANS_PER_BUFFER
  if (y < 111 || y > 171) {
    return 0;
  } else {
    // Produces an elliptic paraboloid
    let z = ((x - 20) * (x - 20)) / 9 + ((y - 131) * (y - 131)) / 9;
    
    // Remove the "table top" effect
    if (z > 35) {
      return 0;
    } else {
      return z;
    }
  }
}


// Observable awesomeness begins here!

const nothingMeasured$ =
      fake3DScanner$.filter(measuredNothing);

const point$ =
      fake3DScanner$
      .map(removeMissingData)
      .filter(measuredSomething)    
      .buffer(nothingMeasured$)      
      .filter(pipe(isEmpty, not))      
      .map(shiftYValues)               
      .map(flatten);

function measuredNothing(scans) {
  return scans.every(
    scan => scan.every(
      point => point.z <= 0
    )
  );
}

function removeMissingData(scans) {
  return scans.map(
    scan => scan.filter(point => point.z > 0)
  ).filter(
    scan => scan.length > 0
  );
}

function measuredSomething(scans) {
  return scans.length > 0;
}

function shiftYValues(buffers) {
  let yOffset = 0;

  return buffers.map(
    scans => {
      const nextScans = scans.map(
        scan => scan.map(
          point => Object.assign({}, point, {
            y: point.y + yOffset
          })
        )
      );

      yOffset = nextScans[nextScans.length - 1][0].y + 1;
      
      return nextScans;
    }
  );
}


// PlotlyJS used to visualize results

let trace1 = {
  x: [], 
  y: [],
  z: [], 
  mode: 'markers',
  marker: {
    size: 2,
    color: 'rgba(0, 0, 255, 0.5)'
  },
  line: {
    color: 'rgb(255, 0, 0)'
  },
  type: 'scatter3d'
};

let data = [trace1];
let layout = {
  margin: {
    l: 0,
    r: 0,
    b: 0,
    t: 0
  }
};
Plotly.newPlot('app', data, layout);


point$.subscribe(
  points => {
    // Plotly wants these nested in an extra array
    let x = [ points.map(prop('x')) ];
    let y = [ points.map(prop('y')) ];
    let z = [ points.map(prop('z')) ];
    let colors = z[0].map(val => 'rgba(0, 0, ' + (val * 6) + ', 0.5)');
    let update = { x, y, z, 'marker.color': [ colors ]};

    Plotly.restyle('app', update);
  }
)
              
            
!
999px

Console