Skip to content

Instantly share code, notes, and snippets.

@wemakeweb
Created January 31, 2012 03:33
Show Gist options
  • Save wemakeweb/1708583 to your computer and use it in GitHub Desktop.
Save wemakeweb/1708583 to your computer and use it in GitHub Desktop.
measures the Height of a given Text
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