optical-properties
Get optical params of a character, canvas or image data. Useful to do kerning, normalize size or adjust vertical/horizontal alignment.
See demo.
Usage
const optics = let w = canvaswidth h = canvasheight ctx = canvas //get optical paramslet bounds center radius = //make sure radius of char is at least half of canvas heightlet scale = h*5 / radius*2 //optical center shift from the real centerlet diff = w*5 - center0 h*5 - center1 //draw normalized characterctxfont = size*cale + 'px sans-serif'ctx
API
let props = optics(char|canvas|imageData, options?)
Measures optical properties of a character, canvas or imageData based on options. Canvas is expected to be rectangular.
Options:
size
− size of canvas to use, bigger is slower but more precise and vice-versa. Defaults to200
.fontFamily
− font family to use for the character, defaults tosans-serif
.fontSize
− size of glyph, defaults to100
.
Returns object with properties:
center
− coordinates of optical center as[cx, cy]
.bounds
− character bounding box[left, top, right, bottom]
.radius
− distance from the optical center to the outmost point.
Credits
© 2017 Dima Yv. MIT License