Last active
March 28, 2023 22:40
-
-
Save crazy4groovy/ffcc197b0d806e8645319caf61e00b0b to your computer and use it in GitHub Desktop.
logic to compress / expand a text element to fit on one line (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fitText(el, { maxFontSizePx, minFontSizePx, compressor = 1 } = {}) { | |
const s1 = el.clientWidth / (compressor * 10); | |
const s2 = parseFloat(maxFontSizePx); | |
const s3 = parseFloat(minFontSizePx); | |
el.style.fontSize = Math.max(Math.min(s1, s2), s3) + 'px'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credit: https://github.com/adactio/FitText.js