Giter VIP home page Giter VIP logo

Comments (3)

vasturiano avatar vasturiano commented on May 28, 2024

@Jing1524 you can try setting the node's velocities (via v* attributes) instead of their positions. This will probably get you closer to what you have in mind. And possibly it helps too to set d3VelocityDecay and d3AlphaDecay to 0.

Take a look at this example.

const nodes = [...Array(N).keys()].map(() => ({
// Initial velocity in random direction
vx: (Math.random() * 2) - 1,
vy: (Math.random() * 2) - 1
}));

from react-force-graph.

Jing1524 avatar Jing1524 commented on May 28, 2024

@vasturiano thank you so much for the quick response! will try it out! :)

from react-force-graph.

Jing1524 avatar Jing1524 commented on May 28, 2024

@vasturiano hey sorry to bother you again.
I just spend a few hours working on it again, but having a hard time to get it to work, feels like I'm missing something still,
i did manage to have the node keep moving, but the resulting movement is way too dramatic. I'm attaching my code and sandbox to show you the currently result. wondering if you could help.

here is the current result
Below useEffect is doing the heavy lift for the "floating" movement.

useEffect(() => {
    graphData.nodes.forEach((node) => {
      const initialColor = "#E4CDC6";
      const targetColor = "#a7715f";
      setNodeTransition((prev) => ({ ...prev, [node.id]: initialColor }));
      setTimeout(() => {
        animateColorTransition(node.id, initialColor, targetColor, 500); // Transition over 2 seconds
      }, Math.random() * 5000 + 1000); // Random interval between 3s and 8s
    });
    graphData.nodes.forEach((node) => {
      node.vx = Math.random() * 2 - 1; 
      node.vy = Math.random() * 2 - 1;
      node.vz = Math.random() * 2 - 1;
    });

    const BOUNDING_BOX_SIZE = 10;

    
    const enforceBoundingBox = () => {
      graphData.nodes.forEach((node) => {
        if (Math.abs(node.x) > BOUNDING_BOX_SIZE) node.vx *= -1;
        if (Math.abs(node.y) > BOUNDING_BOX_SIZE) node.vy *= -1;
        if (Math.abs(node.z) > BOUNDING_BOX_SIZE) node.vz *= -1;
      });
    };

    
    const interval = setInterval(enforceBoundingBox, 1000);

    return () => clearInterval(interval);
  }, [graphData.nodes]);

return (
 <ForceGraph3D
      ref={forceGraphRef}
      width={width}
      height={height}
      graphData={graphData}
      backgroundColor="#ffffff"
      showNavInfo={false}
      nodeColor={(node) => nodeTransition[node.id] || "#E4CDC6"}
      nodeOpacity={1}
      linkColor={(d) => (d.type === "OK" ? "#E4CDC6" : "#E4CDC6")}
      linkOpacity={0.75}
      enableNavigationControls={false}
      d3AlphaDecay={0}
      d3VelocityDecay={0}
    />
)

from react-force-graph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.