Skip to content

Instantly share code, notes, and snippets.

@romsson
Created December 27, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romsson/c7ee19ff9f90b195313e7268e8518cc8 to your computer and use it in GitHub Desktop.
Save romsson/c7ee19ff9f90b195313e7268e8518cc8 to your computer and use it in GitHub Desktop.
random small curves (grids)
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
rect, path {
fill: none;
stroke: black;
stroke-width: 1;
}
</style>
<body>
<script src="http://d3js.org/d3.v4.js"></script>
<script src="http://romsson.github.io/d3-gridding/build/d3-gridding.js"></script>
<script>
var width = 400,
height = 400,
n = 9,
data = d3.range(n),
max_depth = 4,
root_grid = 49,
node_grid = 3;
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
var root_params = {
"size": [width, height],
"offset": [0, 0],
"mode": "grid"
};
var level = 0;
var line = d3.line().curve(d3.curveBasis);
line
.x(function(d) { return d.cx; })
.y(function(d) { return d.cy; });
function generate_layout(params, data = d3.range(root_grid), depth = 0) {
var gridding = d3.gridding()
.params(params);
var griddingData = gridding(data);
if(depth === 3) {
level++;
var lines = svg.selectAll(".line_" + level)
.data([griddingData]);
lines.enter().insert("path")
.attr("class", "line_" + level)
.attr("d", function(d) { return line(d); });
}
if(depth < max_depth) {
griddingData.forEach(function(d, i) {
var grid_type = (i % 2) === 0 ? "coordinate": "horizontal";
var node_params = {
"size": [d.width, d.height],
"offset": [d.x, d.y],
"mode": "coordinate"
};
if(depth === 2) {
node_params.size = [node_params.size[0] * 5, node_params.size[1] * 5];
node_params.offset = [node_params.offset[0] - node_params.size[0], node_params.offset[1] - node_params.size[1]];
}
generate_layout(node_params, d3.range(node_grid), depth + 1);
});
}
}
generate_layout(root_params);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment