0

I need a function that does the exact same thing as ctx.measureText(string).width, but with the height of the string. I was quite surprised when I found out that ctx.measureText(string).height doesn't exist. Seems strange that there'd be one for width but not height...

(Please don't use jQuery)

Spider53
  • 31
  • 5

1 Answers1

0

You can grab this with the clientHeight attribute of an element.

const p = document.querySelector('p');

function findHeight(element) {
    return element.clientHeight;
} 

console.log(findHeight(p));
//returns paragraph height in px
Scott Ledbetter
  • 328
  • 1
  • 4
  • 9
  • It isn't an element though. It's just text being drawn to a canvas using fillText with a given string, px font size, and font (not monospace). – Spider53 Jun 26 '18 at 17:27
  • Ah, I see. That's harder :) Maybe check out this thread: https://stackoverflow.com/questions/16816071/calculate-exact-character-string-height-in-javascript – Scott Ledbetter Jun 26 '18 at 17:29