springy icon indicating copy to clipboard operation
springy copied to clipboard

return absolute x,y position of each node

Open brucejcw opened this issue 8 years ago • 7 comments

Is there any API can return absolute x,y position of each node? I couldn't find it in layout

var layout = new Springy.Layout.ForceDirected(
  graph,
  400.0, // Spring stiffness
  400.0, // Node repulsion
  0.5 // Damping
);

brucejcw avatar Mar 29 '16 08:03 brucejcw

I'd suggest looking at the layout.eachNode function. Usage looks something like this:

layout.eachNode(function(node, point) {
  console.log(node); // Original graph node
  // The "point" is what the layout uses to track position/velocity/mass of a node
  console.log(point.p.x); 
  console.log(point.p.y);
});

Point properties: https://github.com/dhotson/springy/blob/559a40033116192a6b5b6ff645c78f1d11c2903c/springy.js#L611-L617

Also, I usually transform the underlying "world" coordinates to screen coordinates separately to the layout algorithm.

e.g. here's how it's done in springyui.js: https://github.com/dhotson/springy/blob/559a40033116192a6b5b6ff645c78f1d11c2903c/springyui.js#L63-L69

dhotson avatar Mar 29 '16 09:03 dhotson

That's so kind of you. thanks, let me try first

brucejcw avatar Mar 29 '16 09:03 brucejcw

@dhotson Does the springyui.js depends on springy.js? I didn't found Graph definition in springyui.js

brucejcw avatar Mar 29 '16 09:03 brucejcw

Yep, that's right—you need to include springy.js first.

dhotson avatar Mar 29 '16 11:03 dhotson

hi, dhotson, I've tried to using your toScreen algorithm, but seems the screen x,y not looks like the point one.

toScreen = (p)->
    size = currentBB.topright.subtract(currentBB.bottomleft);
    sx = p.subtract(currentBB.bottomleft).divide(size.x).x * 1000;
    sy = p.subtract(currentBB.bottomleft).divide(size.y).y * 1000;
    return new Springy.Vector(sx, sy);
layout = new Springy.Layout.ForceDirected graph, 1000, 1000
currentBB = layout.getBoundingBox();
j = 0
layout.eachNode (node, point)->
    console.log node, point
    list[j].position = toScreen point.p
    console.log list[j].position
    j++
<canvas id="my_canvas" width="1000" height="1000" />

2016-03-30 10 19 52

brucejcw avatar Mar 30 '16 02:03 brucejcw

Hey sorry, I'm not quite sure I'm following what you mean—what are you expecting to see here?

dhotson avatar Mar 30 '16 02:03 dhotson

Ok, I may not explain well.
The screenshot shows two graphs, one is springy, the other one is html div
and the absolute position of each div is using toScreen method which picked from springyui.js.
see 2016-03-30 15 14 03

What I mean is I'm converting canvas coordinate to window's using toScreen method, but the result shows the two graphs are not the same

brucejcw avatar Mar 30 '16 07:03 brucejcw