parent
95c90a3d04
commit
159390018b
@ -0,0 +1,26 @@ |
|||||||
|
// found this hoe on stack overflow
|
||||||
|
|
||||||
|
module.exports = function isOverflowing(el) { |
||||||
|
return getTextWidth(el.innerHTML, getCanvasFontSize(el)) > el.clientWidth - 30; |
||||||
|
}; |
||||||
|
|
||||||
|
function getTextWidth(text, font) { |
||||||
|
// re-use canvas object for better performance
|
||||||
|
const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas")); |
||||||
|
const context = canvas.getContext("2d"); |
||||||
|
context.font = font; |
||||||
|
const metrics = context.measureText(text); |
||||||
|
return metrics.width; |
||||||
|
} |
||||||
|
|
||||||
|
function getCssStyle(element, prop) { |
||||||
|
return window.getComputedStyle(element, null).getPropertyValue(prop); |
||||||
|
} |
||||||
|
|
||||||
|
function getCanvasFontSize(el = document.body) { |
||||||
|
const fontWeight = getCssStyle(el, 'font-weight') || 'normal'; |
||||||
|
const fontSize = getCssStyle(el, 'font-size') || '16px'; |
||||||
|
const fontFamily = getCssStyle(el, 'font-family') || 'Times New Roman'; |
||||||
|
|
||||||
|
return `${fontWeight} ${fontSize} ${fontFamily}`; |
||||||
|
} |
Loading…
Reference in new issue