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

Incorrect Position returned for some lengths on SVG Bezier Curves #1879

Open
FrontSide opened this issue Jan 20, 2025 · 2 comments
Open

Incorrect Position returned for some lengths on SVG Bezier Curves #1879

FrontSide opened this issue Jan 20, 2025 · 2 comments

Comments

@FrontSide
Copy link

It looks like, when using the Path.getPositionAtLength function the incorrect position is returned for some lengths. This seems to be happening around curvatures.

Example: Take this SVG Path

m 1633.8176,1077.4212 c 0,0 18.4277,-511.56464 -14.7423,-535.31585 -32.2488,-23.0917 -318.7995,-9.13506 -318.7995,-9.13506 H 830.3691 c 0,0 -182.43438,-54.81052 -189.80546,-129.7182 -3.97298,-40.37463 -16.58496,-164.43147 -16.58496,-164.43147 0,0 -60.81148,-89.5238 -180.59162,-95.00485 C 323.6069,138.33472 -4.4064367,134.68069 -4.4064367,134.68069")

And now following positions

 let p0 = p.getPointAtLength(87.9549) //black
 let p1 = p.getPointAtLength(91.9549) //red - incorrect 
 let p2 = p.getPointAtLength(94.8549) //green
 let p3 = p.getPointAtLength(97.7549) //blue

When drawing circles at these points (using the annotated colours) I get the following picture

Image

As you can see, the Red dot is above the green dot whereas the order should be in the same order as the Length along the path, i.e.:
black, red, green, blue

@VanquishedWombat
Copy link
Contributor

I have found issues with path measuring in Konva before. The math is pretty tricky and it is possibly a slightly old part of the library that was never correct since original. The easy workaround is to make yourself a function that instantiates an HTL SVG element and use the getPointAtLength function of the SVG object. You don't need to add the SVG element to the page.

Granted this is not a solution and it would be great if Konva was correct, but its a viable right-now workaround with low overhead.

@lavrton
Copy link
Member

lavrton commented Feb 18, 2025

I would prefer a native solution as much as possible where possible, to have less code in Konva and better solution from the platform.

But, we have a case when we run Konva from node.js. And it doesn't have HTML svg. As workaround, we can do similar to image loading. It means we will have a special function to calculate svg path, so we can easily overwrite such function in node.js with some plain code (or external library).

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

3 participants