Public
Edited
Oct 5, 2019
31 stars
Also listed in…
Algorithms
Hello
Insert cell
Insert cell
Insert cell
viewof illo = {
const context = DOM.context2d(width, height, 1),
element = context.canvas;

const X = d3
.scaleLinear()
.domain([0, data.width])
.range([-width / 2, width / 2]),
Y = d3
.scaleLinear()
.domain([0, data.height])
.range([-height / 2, height / 2]),
H = (x, y) => tin.heightAt(Math.round(x), Math.round(y)),
Z = (x, y) => 2 * (H(x, y) - 100);

const illo = (element.value = new Zdog.Illustration({
element,
dragRotate: true,
onDragMove: update,
rotate: { x: Zdog.TAU * -.1 }
}));
const g = new Zdog.Anchor({addTo: illo, rotate: {x: Zdog.TAU / 4}});

for (let i = 0; i < tin.triangles.length; i += 3) {
const p0 = tin.triangles[i],
p1 = tin.triangles[i + 1],
p2 = tin.triangles[i + 2],
x0 = tin.coords[2 * p0],
y0 = tin.coords[2 * p0 + 1],
x1 = tin.coords[2 * p1],
y1 = tin.coords[2 * p1 + 1],
x2 = tin.coords[2 * p2],
y2 = tin.coords[2 * p2 + 1];

new Zdog.Shape({
addTo: g,
path: [
{ x: X(x0), y: Y(y0), z: Z(x0, y0) },
{ x: X(x1), y: Y(y1), z: Z(x1, y1) },
{ x: X(x2), y: Y(y2), z: Z(x2, y2) }
],
color: color(H((x0 + x1 + x2) / 3, (y0 + y1 + y2) / 3)),
stroke: 1,
fill: true
});
}
function update() {
illo.updateRenderGraph();
}
update();
yield element;
}
Insert cell
Insert cell
{
const context = DOM.context2d(width, height),
scale = width / data.width;

for (let i = 0; i < tin.triangles.length; i += 3) {
const p0 = tin.triangles[i],
p1 = tin.triangles[i + 1],
p2 = tin.triangles[i + 2],
x0 = tin.coords[2 * p0],
y0 = tin.coords[2 * p0 + 1],
x1 = tin.coords[2 * p1],
y1 = tin.coords[2 * p1 + 1],
x2 = tin.coords[2 * p2],
y2 = tin.coords[2 * p2 + 1];

context.beginPath();
context.moveTo(scale * x0, scale * y0);
context.lineTo(scale * x1, scale * y1);
context.lineTo(scale * x2, scale * y2);
context.lineTo(scale * x0, scale * y0);

context.fillStyle = color(
tin.heightAt(
Math.round((x0 + x1 + x2) / 3),
Math.round((y0 + y1 + y2) / 3)
)
);
context.fill();
context.stroke();
}

return context.canvas;
}
Insert cell
Insert cell
Delatin = (await import('delatin@0.1/index.js?module')).default
Insert cell
data = fetch(
"https://gist.githubusercontent.com/mbostock/4241134/raw/ee3244eaf405667623241b3d5e1477424623d12b/volcano.json"
).then(d => d.json())
Insert cell
tin = {
const tin = new Delatin(data.values, data.width, data.height);
tin.run(2);
return tin;
}
Insert cell
tin.getMaxError()
Insert cell
Insert cell
color = d3
.scaleSequential(interpolateTerrain)
.domain(d3.extent(data.values))
.nice()
Insert cell
interpolateTerrain = {
const i0 = d3.interpolateHsvLong(d3.hsv(120, 1, 0.65), d3.hsv(60, 1, 0.9));
const i1 = d3.interpolateHsvLong(d3.hsv(60, 1, 0.9), d3.hsv(0, 0, 0.95));
return t => (t < 0.5 ? i0(t * 2) : i1((t - 0.5) * 2));
}
Insert cell
height = ((width * data.height) / data.width) | 0
Insert cell
d3 = require("d3@5", "d3-hsv")
Insert cell
Zdog = require("zdog@1/dist/zdog.dist.min.js")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more