Build textile-like fabric data structures using node-link relationships, rendered as a 3d force-directed layout using 3d-force-graph.
Last active
March 29, 2023 10:10
-
-
Save vasturiano/03569a6bffa0fe5cc6e8710ca18de1d9 to your computer and use it in GitHub Desktop.
Knitting with the Force
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
<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> |
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
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