javascript - Sharing baseline between canvas text and html element -
i'm drawing text html canvas element, , next text have plain css-styled , absolutely positioned html button, position computed javascript. i'd use same font both, seems easy, , i'd both share same baseline. which, far can tell, hard @ moment, @ least given set of constraints i'm facing.
- out of box, canvas-drawn text uses
textbaseline = 'alphabetic'
, , can't change (for compatibility reasons , basline-aligned text using different fonts within canvas). on other hand, absolute positioning on html element uses top or bottom margin, not baseline. - the
textmetrics
documentation on mdn describes various vertical measurements, points out available on chrome , there after flag has been set. need works on decent percentage (say >80%) of current browsers. - i don't know font used, hard-coding metric information font not option.
so options have?
do have render font second off-screen canvas , examine pixel data in order find actual dimensions? can trust these actual dimensions, computed marked pixel on canvas, match dimensions used compute size of button of more artistic fonts?
do need resort spacer image? way, main difference between question , that one fact i'm doing positioning in javascript, don't need pure css solution, , can use canvas can give me.
is there other (i.e. without spacer image) way leverage
vertical-align
css property, creating outer box well-defined baseline , nesting button inside aligned same baseline?any other direction take?
you have added metrics differ depending on different browser's text render engine.
text difficult , not easy right without low-level metrics. , state available in chrome under experimental flags.
- any other direction take?
you can read , parse fonts manually , obtain metrics that. there this (font.js) solution this 1 (opentype.js) doing that. in turn though limit types of fonts can use.
the other approach create button using second canvas element. can still receive click events etc. have same conditions font in main canvas.
Comments
Post a Comment