index.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. <div style="position: absolute; top: 5px; right: 5px;">
  9. <button id="animationToggle" style="margin: 8px; height: 25px; width: 150px;">
  10. Pause Animation
  11. </button>
  12. <button id="rotationToggle" style="margin: 8px; height: 25px; width: 150px;">
  13. Pause Rotation
  14. </button>
  15. </div>
  16. <script>
  17. // Random tree
  18. const N = 300;
  19. const gData = {
  20. nodes: [...Array(N).keys()].map(i => ({ id: i })),
  21. links: [...Array(N).keys()]
  22. .filter(id => id)
  23. .map(id => ({
  24. source: id,
  25. target: Math.round(Math.random() * (id-1))
  26. }))
  27. };
  28. const distance = 1400;
  29. let isRotationActive = true;
  30. const Graph = ForceGraph3D()
  31. (document.getElementById('3d-graph'))
  32. .enableNodeDrag(false)
  33. .enableNavigationControls(false)
  34. .showNavInfo(false)
  35. .cameraPosition({ z: distance })
  36. .graphData(gData);
  37. // camera orbit
  38. let angle = 0;
  39. setInterval(() => {
  40. if (isRotationActive) {
  41. Graph.cameraPosition({
  42. x: distance * Math.sin(angle),
  43. z: distance * Math.cos(angle)
  44. });
  45. angle += Math.PI / 300;
  46. }
  47. }, 10);
  48. document.getElementById('rotationToggle').addEventListener('click', event => {
  49. isRotationActive = !isRotationActive;
  50. event.target.innerHTML = `${(isRotationActive ? 'Pause' : 'Resume')} Rotation`;
  51. });
  52. let isAnimationActive = true;
  53. document.getElementById('animationToggle').addEventListener('click', event => {
  54. isAnimationActive ? Graph.pauseAnimation() : Graph.resumeAnimation();
  55. isAnimationActive = !isAnimationActive;
  56. event.target.innerHTML = `${(isAnimationActive ? 'Pause' : 'Resume')} Animation`;
  57. });
  58. </script>
  59. </body>