Skip to content

Instantly share code, notes, and snippets.

@vasturiano
Last active March 29, 2023 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasturiano/03569a6bffa0fe5cc6e8710ca18de1d9 to your computer and use it in GitHub Desktop.
Save vasturiano/03569a6bffa0fe5cc6e8710ca18de1d9 to your computer and use it in GitHub Desktop.
Knitting with the Force

Build textile-like fabric data structures using node-link relationships, rendered as a 3d force-directed layout using 3d-force-graph.

<head>
<style>
body {
text-align: center;
font-family: Sans-serif;
margin: 0;
}
</style>
<script src="//unpkg.com/3d-force-graph"></script>
</head>
<body>
<div id="3d-graph"></div>
<script src="index.js"></script>
</body>
ForceGraph3D()
.cooldownTicks(300)
.forceEngine('ngraph')
.graphData(getFabricData())
(document.getElementById("3d-graph"));
//
function getFabricData() {
const perimeter = 12, length = 30;
const getId = (col, row) => `${col},${row}`;
let nodes = [], links = [];
for (let colIdx=0; colIdx<perimeter; colIdx++) {
for (let rowIdx=0; rowIdx<length; rowIdx++) {
const id = getId(colIdx, rowIdx);
nodes.push({id});
// Link vertically
if (rowIdx>0) {
links.push({ source: getId(colIdx, rowIdx-1), target: id });
}
// Link horizontally
links.push({ source: getId((colIdx || perimeter) - 1, rowIdx), target: id });
}
}
return { nodes, links };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment