index.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <head>
  2. <style>
  3. body { margin: 0; }
  4. #emit-particles-btn {
  5. position: absolute;
  6. top: 10px;
  7. right: 10px;
  8. font-size: 13px;
  9. }
  10. </style>
  11. <script src="//unpkg.com/3d-force-graph"></script>
  12. <!--<script src="../../dist/3d-force-graph.js"></script>-->
  13. </head>
  14. <body>
  15. <div id="3d-graph"></div>
  16. <button id="emit-particles-btn">Emit 10 Random Particles</button>
  17. <script>
  18. // Random tree
  19. const N = 50;
  20. const gData = {
  21. nodes: [...Array(N).keys()].map(i => ({ id: i })),
  22. links: [...Array(N).keys()]
  23. .filter(id => id)
  24. .map(id => ({
  25. source: id,
  26. target: Math.round(Math.random() * (id-1))
  27. }))
  28. };
  29. const Graph = ForceGraph3D()
  30. (document.getElementById('3d-graph'))
  31. .linkDirectionalParticleColor(() => 'red')
  32. .linkDirectionalParticleWidth(4)
  33. .linkHoverPrecision(10)
  34. .graphData(gData);
  35. Graph.onLinkClick(Graph.emitParticle); // emit particles on link click
  36. document.getElementById('emit-particles-btn').addEventListener('click', () => {
  37. [...Array(10).keys()].forEach(() => {
  38. const link = gData.links[Math.floor(Math.random() * gData.links.length)];
  39. Graph.emitParticle(link);
  40. });
  41. });
  42. </script>
  43. </body>