Coder Social home page Coder Social logo

handlefontresize.js's Introduction

handleFontResize.js

handleFontResize.js is a Javascript library to dynamically resize the input font-size of text to fit a fixed size div. This was designed to work for text input primarily, but could work in other use cases.

How to use it

I built handleFontResize.js to work on 'keyup' event handlers, but it should work with any other event handler that can pass data with events.

jQuery: $('YourSelectorHere').keyup({'maxFontSize':60,'paddingWidth':50},handleFontResize);

Live demo available here.

How does it work

I originally built this because I couldn't find anything that would work to dynamically resize font as I entered text, but during the project I discovered a few options out there. Make sure you check these out:

handleFontResize.js works by taking two variables:

maxFontSize - set the upper limit for your font sizes. Kinda self explanatory.

paddingWidth - effectively, sets the 'padding-right' on the 'invisible line' which triggers your text resize.

Input Text box:			
 ________________________
|	Variable	   |	|
|    Length        |    |
|_____Input________|____|

				   ^    ^
				   |----|
		  var paddingWidth
						

Other, non-declarable variables:

maxWidth - the width of the in-focus element when handleFontSize() is called.

measureResult - the returned value from measureText(), which copies the input out into an invisible div (id='text-width-tester'), and returns the length in pixels.

widthDelta - the difference between measureResult & maxWidth.

flip - prevents the 'too big -> too small -> too big' infinite loop of font re-sizing. Allows the sign of widthDelta to flip 3 times consecutively before breaking.

Set 'debug' to true to console.log these values, as needed.

Contact me

If you have any questions, please grab me on Twitter: @nfFrenchie.


  • possible extension: add in default values for maxFontSize & paddingWidth so that it doesn't error out if these aren't set.

handlefontresize.js's People

Contributors

frenchi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.