tisoap / react-flow-smart-edge

Custom Edge for React Flow that never intersects with other nodes
https://tisoap.github.io/react-flow-smart-edge/
MIT License
242 stars 26 forks source link

Bending of edge from source #49

Open adarshsuresh12 opened 1 year ago

adarshsuresh12 commented 1 year ago

Describe the Bug

The edge seems to have a bend when it origins from the source. After that it's coming as expected (I tried for smooth step edge). Is there any way to avoid that? Please help me out here

Screenshot 2023-04-17 at 3 43 00 PM

Even if you check in the codesandbox example(not mine) given below, you will be able to see the issue

Minimal Example

https://codesandbox.io/s/old-rgb-k9buls

Steps to Reproduce the Bug or Issue

  1. Create a smooth step edge
  2. You will see a bend at the source

Expected behavior

Expected to see no bend. If the line is a straight line, from the source itself, it should be straight

Screenshots or Videos

No response

Platform

Additional context

No response

tisoap commented 1 year ago

Hey @adarshsuresh12, thanks for reporting! To give you a bit of context on why this is happening:

This package uses a path-finding algorithm behind the scenes. It works by generating a 2D grid representation of the graph, with "free" and "occupied" cells, and then it "walks" through these cells using A-star. The problem is, path-finding is very resource intensive, so treating every pixel on screen as a possible free/occupied cell would have made this Edge terribly slow. My solution was to work with 10x10 pixel cells instead of individual pixels by default, so the problem scope is 10x smaller. You can actually control the cell size with the gridRatio advanced option.

So, what you're seeing as a "bend" in the edge at this position, is the edge being drawn to the start of one of those cells before it follows the computed path. To fix this, I'd need to add edge cases for the start/end of the computed path, which I didn't have the time to implement. It's similar to the issue described here: https://github.com/tisoap/react-flow-smart-edge/issues/45

As a palliative measure in the meantime, you could try to either decrease the gridRatio value (at the expense of more computing power), or add grid snapping to your React Flow graph so the nodes themselves will be aligned.

brootle commented 1 year ago

@tisoap maybe you can just update the function that returns svgPathString and straight up the ends? I checked it and it shouldn't be hard, for example

from M 792.652 840 L 790 830 L 790 820 L 780 820 L 770 820 L 760 820 L 750 820 L 740 820 L 730 820 L 720 820 L 710 820 L 710 810 L 710 800 L 710 790 L 710 780 L 710 770 L 710 760 L 710 750 L 710 740 L 710 730 L 710 720 L 710 710 L 710 700 L 710 690 L 710 680 L 710 670 L 710 660 L 710 650 L 710 640 L 710 630 L 710 620 L 710 610 L 700 610 L 690 610 L 680 610 L 670 610 L 660 610 L 650 610 L 640 610 L 630 610 L 620 610 L 610 610 L 600 610 L 590 610 L 580 610 L 570 610 L 560 610 L 550 610 L 540 610 L 530 610 L 520 610 L 510 610 L 500 610 L 490 610 L 480 610 L 470 610 L 460 610 L 450 610 L 440 610 L 430 610 L 420 610 L 410 610 L 400 610 L 390 610 L 380 610 L 370 610 L 360 610 L 360 600 L 364.348 594

to M 792.652 840 L 792.652 830 L 792.652 820 L 780 820 L 770 820 L 760 820 L 750 820 L 740 820 L 730 820 L 720 820 L 710 820 L 710 810 L 710 800 L 710 790 L 710 780 L 710 770 L 710 760 L 710 750 L 710 740 L 710 730 L 710 720 L 710 710 L 710 700 L 710 690 L 710 680 L 710 670 L 710 660 L 710 650 L 710 640 L 710 630 L 710 620 L 710 610 L 700 610 L 690 610 L 680 610 L 670 610 L 660 610 L 650 610 L 640 610 L 630 610 L 620 610 L 610 610 L 600 610 L 590 610 L 580 610 L 570 610 L 560 610 L 550 610 L 540 610 L 530 610 L 520 610 L 510 610 L 500 610 L 490 610 L 480 610 L 470 610 L 460 610 L 450 610 L 440 610 L 430 610 L 420 610 L 410 610 L 400 610 L 390 610 L 380 610 L 370 610 L 364.348 610 L 364.348 600 L 364.348 594

tisoap commented 1 year ago

@brootle I'm happy to take pull requests!

josewweee commented 2 months ago

My solution, create a custom drawEdge method ( the same as the default ones) and set the first and last paths X positions to be the same as the next/prev ones, here are my options:

const smartOptions = {
    nodePadding: 16,
    gridRatio: 5,
    drawEdge: (source: any, target: any, path: any) => {
      let svgPathString = `M ${path[0][0]}, ${source.y}`;

      path.forEach((point: any) => {
        const [x, y] = point;
        svgPathString += `L ${x}, ${y} `;
      });

      svgPathString += `L ${path[path.length - 1][0]}, ${target.y} `;
      console.log(svgPathString);
      return svgPathString;
    },
    generatePath: pathfindingJumpPointNoDiagonal,
  };

it works like a charm and there is no bend anymore