FitText.js icon indicating copy to clipboard operation
FitText.js copied to clipboard

Character size based fitting (width & height), compressor removed

Open arminrosu opened this issue 12 years ago • 8 comments

Hey,

This is my first public fork, so it might be a bit overkill. Basically I needed height fitting as well, so I changed the whole shabang to calculate optimal sizes based on character dimensions (at given font-size) vs. container width.

The one big difference in implementation, is that texts need to be set to width: auto and whitespace: nowrap; is encouraged (should work without that, but I'm thinking of @font-face fonts, which might load late and bork it up).

I've tested it on a page with 200 fit-able texts, 20 fonts, worked fine.

Hope you like it, Armin

arminrosu avatar Aug 11 '11 11:08 arminrosu

Wow. Looks fun! I'll have to download it and put it through its paces to see if it's what I think should become the standard. That might take a while given my current workload at the moment.

Do you have a JSBin or something like that to show the use case for needing it to fit a height as well as a width?

davatron5000 avatar Aug 11 '11 15:08 davatron5000

I could whip up a short jsbin, but give me a couple of days please (guess we have similar workloads). Otherwise just use the example.html. On my screen (1280x800), I can see it stop fitting the width, at a certain point (horizontal space just gets larger), meaning vertical fitting is happening (but since the height doesn't change, neither does the font-size).

arminrosu avatar Aug 11 '11 17:08 arminrosu

ok, so here's a quick test, not very pretty, but it does the job: http://jsbin.com/okosow/

The second box is for height testing. I haven't changed the js code, so depending on how you're scaling, height/width fitting might kick in.

line-height and initial font-size are important, but only to ensure proper vertical alignment.

arminrosu avatar Aug 11 '11 20:08 arminrosu

Hi Armin,

I've implemented your version of this code but sometimes the resized text randomly appears about twice as large as it should. I'm afraid I can't give you any other information on this problem. It seems to happen one out of every 3 or 4 times the page is loaded. Do you have any idea why this might be happening?

ninjaprecision avatar Sep 21 '11 21:09 ninjaprecision

That's weird. Do you have an example (jsfiddle) handy?

Are you use @font-face to load custom fonts? Try executing fitText on $(window).load(function () { $("#fittext").fitText(); });

Also, log the values fittext is using (in jquery.fittext.js), you might figure out what's happening. I'd also log to see if it does width or height fitting.

arminrosu avatar Sep 22 '11 08:09 arminrosu

Using $(window).load(function () seems to have fixed that problem! Thank you!

Another question though - does this fork support min and max font size?

ninjaprecision avatar Sep 23 '11 16:09 ninjaprecision

For extra performance, if you are using google webfonts (if I recall correctly, you are), you could bind fitText to the event that is fired when they're done loading: WebFont Loader > Events

arminrosu avatar Oct 02 '11 15:10 arminrosu

Anyway possible to merge this?

https://coderwall.com/p/_8jxgw/autoresize-text-to-fit-into-a-div-width-height (Or the jquery plugin version: https://gist.github.com/iamkirkbater/ee9741a02431290682e6)

pencilcheck avatar Jul 10 '17 07:07 pencilcheck