Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return absolute x,y position of each node #81

Open
brucejcw opened this issue Mar 29, 2016 · 7 comments
Open

return absolute x,y position of each node #81

brucejcw opened this issue Mar 29, 2016 · 7 comments

Comments

@brucejcw
Copy link

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
);
@dhotson
Copy link
Owner

dhotson commented Mar 29, 2016

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:

springy/springy.js

Lines 611 to 617 in 559a400

// Point
Layout.ForceDirected.Point = function(position, mass) {
this.p = position; // position
this.m = mass; // mass
this.v = new Vector(0, 0); // velocity
this.a = new Vector(0, 0); // acceleration
};

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:

springy/springyui.js

Lines 63 to 69 in 559a400

// convert to/from screen coordinates
var toScreen = function(p) {
var size = currentBB.topright.subtract(currentBB.bottomleft);
var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * canvas.width;
var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * canvas.height;
return new Springy.Vector(sx, sy);
};

@brucejcw
Copy link
Author

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

@brucejcw
Copy link
Author

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

@dhotson
Copy link
Owner

dhotson commented Mar 29, 2016

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

@brucejcw
Copy link
Author

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

@dhotson
Copy link
Owner

dhotson commented Mar 30, 2016

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

@brucejcw
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants