index.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. <script src="//unpkg.com/d3-octree"></script>
  6. <script src="//unpkg.com/d3-force-3d"></script>
  7. </head>
  8. <body>
  9. <div id="3d-graph"></div>
  10. <script>
  11. const N = 50;
  12. const nodes = [...Array(N).keys()].map(() => ({
  13. // Initial velocity in random direction
  14. vx: Math.random(),
  15. vy: Math.random(),
  16. vz: Math.random()
  17. }));
  18. const Graph = ForceGraph3D()
  19. (document.getElementById('3d-graph'));
  20. Graph.cooldownTime(Infinity)
  21. .d3AlphaDecay(0)
  22. .d3VelocityDecay(0)
  23. // Deactivate existing forces
  24. .d3Force('center', null)
  25. .d3Force('charge', null)
  26. // Add collision and bounding box forces
  27. .d3Force('collide', d3.forceCollide(Graph.nodeRelSize()))
  28. .d3Force('box', () => {
  29. const CUBE_HALF_SIDE = Graph.nodeRelSize() * N * 0.5;
  30. nodes.forEach(node => {
  31. const x = node.x || 0, y = node.y || 0, z = node.z || 0;
  32. // bounce on box walls
  33. if (Math.abs(x) > CUBE_HALF_SIDE) { node.vx *= -1; }
  34. if (Math.abs(y) > CUBE_HALF_SIDE) { node.vy *= -1; }
  35. if (Math.abs(z) > CUBE_HALF_SIDE) { node.vz *= -1; }
  36. });
  37. })
  38. // Add nodes
  39. .graphData({ nodes, links: [] });
  40. </script>
  41. </body>