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

An unexpected situation occurred when using path. toSVG(), where NaN caused text rendering errors #673

Open
hanke677 opened this issue Feb 4, 2024 · 4 comments
Assignees
Labels

Comments

@hanke677
Copy link

hanke677 commented Feb 4, 2024

Recently, while testing the file again, I accidentally encountered several rendering failures after using toSVG() for text. The path data is shown in the figure:
Snipaste_2024-02-04_15-14-05

Then I briefly analyzed the problem and its corresponding source code, and found out where the problem lies:
Snipaste_2024-02-04_15-18-11
Because JS uses Scientific notation by default for floating point numbers with large numbers of digits, according to the previous code writing method, a NaN will be returned here, resulting in an error in the path. I think that although the probability of this situation is relatively low, it should also be considered.
Snipaste_2024-02-04_15-19-12

My personal suggestion is that if I want to solve this problem without refactoring the original code, I try to use tofix () to prevent js from using Scientific notation, and the accuracy parameters are controlled by the user to ensure correctness. After my simple test, I found that this problem can be effectively solved.
Snipaste_2024-02-04_15-19-28

So, does anyone have better suggestions or methods?

@Connum Connum added the bug label Feb 4, 2024
@Connum Connum self-assigned this Feb 4, 2024
@Connum
Copy link
Contributor

Connum commented Feb 4, 2024

Thanks for reporting this!
We had the discussion before about the rounding method, but it's still the most accurate. I don't know why it would return NaN in those cases, but in order to catch this, we shall use isNaN and fall back to toFixed.

@aukeroorda
Copy link

aukeroorda commented Feb 14, 2024

Hi - This happens when a number very close above an integer is given as input, e.g. the float version of the number 18: 18.000000000000004. This then gets split up as an integerPart = 18 and a decimalPart = 3.552713678800501e-15. When using places = 3, the first decimalPart + 'e+' + places 'computation' results in the string '3.552713678800501e-15e+3', which cannot be parsed by Math.round(). I would suggest to use either toFixed() as suggested earlier, but preferably don't do multiplication and division by using string concatenation, and use Math.pow() or a similar approach:

  const roundedDecimalPart =
    +(Math.round(decimalPart * (Math.pow(10, places))) *
      (Math.pow(10, -places)));

@ILOVEPIE
Copy link
Contributor

ILOVEPIE commented Jun 2, 2024

Hi - This happens when a number very close above an integer is given as input, e.g. the float version of the number 18: 18.000000000000004. This then gets split up as an integerPart = 18 and a decimalPart = 3.552713678800501e-15. When using places = 3, the first decimalPart + 'e+' + places 'computation' results in the string '3.552713678800501e-15e+3', which cannot be parsed by Math.round(). I would suggest to use either toFixed() as suggested earlier, but preferably don't do multiplication and division by using string concatenation, and use Math.pow() or a similar approach:

  const roundedDecimalPart =
    +(Math.round(decimalPart * (Math.pow(10, places))) *
      (Math.pow(10, -places)));

@Connum have we looked into this?

@kartsims
Copy link

kartsims commented Nov 6, 2024

fwiw this fixed the issue for me too!

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

No branches or pull requests

5 participants