Skip to content

Instantly share code, notes, and snippets.

@talltyler talltyler/gist:5345959
Last active Dec 16, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.