Skip to content

Instantly share code, notes, and snippets.

@talltyler
Last active December 16, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talltyler/5345959 to your computer and use it in GitHub Desktop.
Save talltyler/5345959 to your computer and use it in GitHub Desktop.
In most browsers the ctx.measureText() method returns an object with only a width property on it. actualBoundingBoxAscent & actualBoundingBoxDescent are in the spec but not implemented yet by any browser. because of this only fonts of normal proportions will have correct height values, things like condensed fonts will be off. We measure the widt…
var measureText = function (ctx,font,text) {
ctx.font = font;
var width = ctx.measureText(text).width;
var metrics = ctx.measureText('M');
return {width:width, height: metrics.width + (metrics.actualBoundingBoxAscent||0) + (metrics.actualBoundingBoxDescent||0)};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment