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

We can get rid of the binary search in the font size algorithm #20

Open
jt3k opened this issue Jun 1, 2021 · 2 comments
Open

We can get rid of the binary search in the font size algorithm #20

jt3k opened this issue Jun 1, 2021 · 2 comments

Comments

@jt3k
Copy link

jt3k commented Jun 1, 2021

I read in the Readme that you are using binary search. It seems that performance can be improved by not using binary search.

The algorithm for finding the font size is as follows:

  1. get the width of the text const {width} = myElement. getBoundingClientRect();
  2. coefficient = element_width / fontSize
  3. to get the font size that will fit without breaking a line in a div with a width of 400 pixels, you need to divide 400 by this coefficient;
    newFontSize = 400 / coefficient
  4. ...
  5. Profit! :)

A working demo and code can be viewed here

and here you can look at this algorithm:

@steida
Copy link

steida commented Nov 7, 2021

@jt3k Such algorithm is nice but unfortunately imprecise. Text can overlap or be not as big as possible.

@oeed
Copy link

oeed commented Dec 19, 2023

I know this is an old issue, but did you have any examples of this algorithm breaking? In my testing it's working okay, but it sounds like the issues are tricky edge case.

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