Last active
December 16, 2015 00:10
-
-
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…
This file contains hidden or 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
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