Created
January 31, 2012 03:33
-
-
Save wemakeweb/1708583 to your computer and use it in GitHub Desktop.
measures the Height of a given Text
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 measureTextHeight(text, width, font, lineHeight) { | |
var ctx = document.createElement("canvas").getContext("2d"), | |
lines; | |
ctx.font = font; | |
lines = Math.round(ctx.measureText(text).width / width); | |
return { | |
lines: lines, | |
height: lines * lineHeight | |
} | |
} | |
console.log(measureTextHeight("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren.", 150, "16px Arial", 35)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment