index.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <head>
  2. <style> body { margin: 0; } </style>
  3. <script src="//unpkg.com/3d-force-graph"></script>
  4. <!--<script src="../../dist/3d-force-graph.js"></script>-->
  5. </head>
  6. <body>
  7. <div id="3d-graph"></div>
  8. <script>
  9. const initData = {
  10. nodes: [ {id: 0 } ],
  11. links: []
  12. };
  13. const elem = document.getElementById("3d-graph");
  14. const Graph = ForceGraph3D()(elem)
  15. .enableNodeDrag(false)
  16. .onNodeClick(removeNode)
  17. .graphData(initData);
  18. setInterval(() => {
  19. const { nodes, links } = Graph.graphData();
  20. const id = nodes.length;
  21. Graph.graphData({
  22. nodes: [...nodes, { id }],
  23. links: [...links, { source: id, target: Math.round(Math.random() * (id-1)) }]
  24. });
  25. }, 1000);
  26. //
  27. function removeNode(node) {
  28. let { nodes, links } = Graph.graphData();
  29. links = links.filter(l => l.source !== node && l.target !== node); // Remove links attached to node
  30. nodes.splice(node.id, 1); // Remove node
  31. nodes.forEach((n, idx) => { n.id = idx; }); // Reset node ids to array index
  32. Graph.graphData({ nodes, links });
  33. }
  34. </script>
  35. </body>