Files
js-util/fontScale.js
T
2022-12-07 12:05:30 +00:00

36 lines
1.0 KiB
JavaScript

/** @module fontScale */
/**
* Scale an element's font size to fit inside the given width.
* @param {HTMLElement} element
* @param {Int} width defaults to `window.innerWidth`
* @returns {Promise<Int>} resulting font size in px
*/
export function fontScale(element,width=innerWidth){
return new Promise(function(resolve){
let fontSize=0
element.style.fontSize=fontSize+'px'
width-=element.clientWidth
function down(){
if(element.clientWidth>=width){
fontSize-=1
element.style.fontSize=fontSize+'px'
requestAnimationFrame(down)
}else{
resolve(fontSize)
}
}
function up(){
if(element.clientWidth<width){
fontSize+=10
element.style.fontSize=fontSize+'px'
requestAnimationFrame(up)
}else{
requestAnimationFrame(down)
}
}
requestAnimationFrame(up)
})
}
export default fontScale