made with esnextbin
Created
January 22, 2019 00:58
-
-
Save vespakoen/2e6c6f817f168a1882bec0ae12c137f5 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stl = require('stl') | |
const ocl = require('opencamlibjs')() | |
const scrollEl = document.createElement('div') | |
scrollEl.style.height = '100%' | |
scrollEl.style.overflowY = 'auto' | |
document.body.appendChild(scrollEl) | |
function appendHeader(str) { | |
const headerEl = document.createElement('h2') | |
headerEl.innerText = str | |
scrollEl.appendChild(headerEl) | |
} | |
function appendPre(str) { | |
const preEl = document.createElement('pre') | |
preEl.innerText = str | |
preEl.style.fontSize = '10px' | |
scrollEl.appendChild(preEl) | |
} | |
function waterline(surface, cutter, z, sampling) { | |
appendHeader('Waterline') | |
const wl = new ocl.Waterline() | |
wl.setSTL(surface) | |
wl.setCutter(cutter) | |
wl.setZ(z) | |
wl.setSampling(sampling) | |
wl.run() | |
let gcode = '' | |
const loops = wl.getLoops() | |
for (var i = 0; i < loops.size(); i++) { | |
const loop = loops.get(i) | |
for (var j = 0; j < loop.size(); j++) { | |
const point = loop.get(j) | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n' | |
} | |
} | |
appendPre(gcode) | |
} | |
function adaptiveWaterline(surface, cutter, z, sampling, minSampling) { | |
appendHeader('AdaptiveWaterline') | |
const awl = new ocl.AdaptiveWaterline() | |
awl.setSTL(surface) | |
awl.setCutter(cutter) | |
awl.setZ(z) | |
awl.setSampling(sampling) | |
awl.setMinSampling(minSampling) | |
awl.run() | |
let gcode = '' | |
const loops = awl.getLoops() | |
for (var i = 0; i < loops.size(); i++) { | |
const loop = loops.get(i) | |
for (var j = 0; j < loop.size(); j++) { | |
const point = loop.get(j) | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n' | |
} | |
} | |
appendPre(gcode) | |
} | |
function pathDropCutter(surface, cutter, sampling, path) { | |
appendHeader('PathDropCutter') | |
const pdc = new ocl.PathDropCutter() | |
pdc.setSTL(surface) | |
pdc.setCutter(cutter) | |
pdc.setPath(path) | |
pdc.setSampling(sampling) | |
pdc.run() | |
const points = pdc.getPoints() | |
let gcode = '' | |
for (var j = 0; j < points.size(); j++) { | |
const point = points.get(j) | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n' | |
} | |
appendPre(gcode) | |
} | |
function adaptivePathDropCutter(surface, cutter, sampling, minSampling, path) { | |
appendHeader('AdaptivePathDropCutter') | |
const apdc = new ocl.AdaptivePathDropCutter() | |
apdc.setSTL(surface) | |
apdc.setCutter(cutter) | |
apdc.setPath(path) | |
apdc.setSampling(sampling) | |
apdc.setMinSampling(minSampling) | |
apdc.run() | |
const points = apdc.getPoints() | |
let gcode = '' | |
for (var j = 0; j < points.size(); j++) { | |
const point = points.get(j) | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n' | |
} | |
appendPre(gcode) | |
} | |
ocl.then(function () { | |
const surface = new ocl.STLSurf() | |
fetch('https://cdn.jsdelivr.net/gh/aewallin/opencamlib@master/stl/gnu_tux_mod.stl') | |
.then(res => res.text()) | |
.then(stlContents => { | |
var facets = stl.toObject(stlContents); | |
facets.facets.forEach(facet => { | |
const tri = new ocl.Triangle( | |
new ocl.Point(facet.verts[0][0], facet.verts[0][1], facet.verts[0][2]), | |
new ocl.Point(facet.verts[1][0], facet.verts[1][1], facet.verts[1][2]), | |
new ocl.Point(facet.verts[2][0], facet.verts[2][1], facet.verts[2][2]) | |
) | |
surface.addTriangle(tri) | |
}) | |
const cutter = new ocl.CylCutter(4, 20) | |
setImmediate(() => waterline(surface, cutter, 1, 0.1)) | |
setImmediate(() => adaptiveWaterline(surface, cutter, 1, 0.1, 0.001)) | |
const path = new ocl.Path() | |
const p1 = new ocl.Point(0, 1, 0) | |
const p2 = new ocl.Point(10, 1, 0) | |
const l = new ocl.Line(p1, p2) | |
path.appendLine(l) | |
setImmediate(() => pathDropCutter(surface, cutter, 0.1, path)) | |
setImmediate(() => adaptivePathDropCutter(surface, cutter, 0.04, 0.01, path)) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"stl": "1.1.1", | |
"opencamlibjs": "0.0.1", | |
"babel-runtime": "6.26.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _setImmediate2 = require('babel-runtime/core-js/set-immediate'); | |
var _setImmediate3 = _interopRequireDefault(_setImmediate2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var stl = require('stl'); | |
var ocl = require('opencamlibjs')(); | |
var scrollEl = document.createElement('div'); | |
scrollEl.style.height = '100%'; | |
scrollEl.style.overflowY = 'auto'; | |
document.body.appendChild(scrollEl); | |
function appendHeader(str) { | |
var headerEl = document.createElement('h2'); | |
headerEl.innerText = str; | |
scrollEl.appendChild(headerEl); | |
} | |
function appendPre(str) { | |
var preEl = document.createElement('pre'); | |
preEl.innerText = str; | |
preEl.style.fontSize = '10px'; | |
scrollEl.appendChild(preEl); | |
} | |
function waterline(surface, cutter, z, sampling) { | |
appendHeader('Waterline'); | |
var wl = new ocl.Waterline(); | |
wl.setSTL(surface); | |
wl.setCutter(cutter); | |
wl.setZ(z); | |
wl.setSampling(sampling); | |
wl.run(); | |
var gcode = ''; | |
var loops = wl.getLoops(); | |
for (var i = 0; i < loops.size(); i++) { | |
var loop = loops.get(i); | |
for (var j = 0; j < loop.size(); j++) { | |
var point = loop.get(j); | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n'; | |
} | |
} | |
appendPre(gcode); | |
} | |
function adaptiveWaterline(surface, cutter, z, sampling, minSampling) { | |
appendHeader('AdaptiveWaterline'); | |
var awl = new ocl.AdaptiveWaterline(); | |
awl.setSTL(surface); | |
awl.setCutter(cutter); | |
awl.setZ(z); | |
awl.setSampling(sampling); | |
awl.setMinSampling(minSampling); | |
awl.run(); | |
var gcode = ''; | |
var loops = awl.getLoops(); | |
for (var i = 0; i < loops.size(); i++) { | |
var loop = loops.get(i); | |
for (var j = 0; j < loop.size(); j++) { | |
var point = loop.get(j); | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n'; | |
} | |
} | |
appendPre(gcode); | |
} | |
function pathDropCutter(surface, cutter, sampling, path) { | |
appendHeader('PathDropCutter'); | |
var pdc = new ocl.PathDropCutter(); | |
pdc.setSTL(surface); | |
pdc.setCutter(cutter); | |
pdc.setPath(path); | |
pdc.setSampling(sampling); | |
pdc.run(); | |
var points = pdc.getPoints(); | |
var gcode = ''; | |
for (var j = 0; j < points.size(); j++) { | |
var point = points.get(j); | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n'; | |
} | |
appendPre(gcode); | |
} | |
function adaptivePathDropCutter(surface, cutter, sampling, minSampling, path) { | |
appendHeader('AdaptivePathDropCutter'); | |
var apdc = new ocl.AdaptivePathDropCutter(); | |
apdc.setSTL(surface); | |
apdc.setCutter(cutter); | |
apdc.setPath(path); | |
apdc.setSampling(sampling); | |
apdc.setMinSampling(minSampling); | |
apdc.run(); | |
var points = apdc.getPoints(); | |
var gcode = ''; | |
for (var j = 0; j < points.size(); j++) { | |
var point = points.get(j); | |
gcode += 'G01 X' + point.x + ' Y' + point.y + ' Z' + point.z + '\n'; | |
} | |
appendPre(gcode); | |
} | |
ocl.then(function () { | |
var surface = new ocl.STLSurf(); | |
fetch('https://cdn.jsdelivr.net/gh/aewallin/opencamlib@master/stl/gnu_tux_mod.stl').then(function (res) { | |
return res.text(); | |
}).then(function (stlContents) { | |
var facets = stl.toObject(stlContents); | |
facets.facets.forEach(function (facet) { | |
var tri = new ocl.Triangle(new ocl.Point(facet.verts[0][0], facet.verts[0][1], facet.verts[0][2]), new ocl.Point(facet.verts[1][0], facet.verts[1][1], facet.verts[1][2]), new ocl.Point(facet.verts[2][0], facet.verts[2][1], facet.verts[2][2])); | |
surface.addTriangle(tri); | |
}); | |
var cutter = new ocl.CylCutter(4, 20); | |
(0, _setImmediate3.default)(function () { | |
return waterline(surface, cutter, 1, 0.1); | |
}); | |
(0, _setImmediate3.default)(function () { | |
return adaptiveWaterline(surface, cutter, 1, 0.1, 0.001); | |
}); | |
var path = new ocl.Path(); | |
var p1 = new ocl.Point(0, 1, 0); | |
var p2 = new ocl.Point(10, 1, 0); | |
var l = new ocl.Line(p1, p2); | |
path.appendLine(l); | |
(0, _setImmediate3.default)(function () { | |
return pathDropCutter(surface, cutter, 0.1, path); | |
}); | |
(0, _setImmediate3.default)(function () { | |
return adaptivePathDropCutter(surface, cutter, 0.04, 0.01, path); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment