Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set 2 fonts for HTML5 canvas context?

I'm trying to get the Canvas to use two fonts when drawing text. This is because my main font is Comic Sans MS (It's a kids app). Since I can't find Comic Sans on iPad, I'm trying to substitute it with MarkerFelt-Thin.

I've tried to use several variations of the following statement:

ctx.font = "40pt MarkerFelt-Thin; 40pt Comic Sans MS";

Doesn't seem to be working. So at the moment I'm using user agent detection and manually assigning different fonts for each user agent.

Anyone know the right way to do this?

Cheers

like image 225
infiniteloop Avatar asked Nov 13 '11 18:11

infiniteloop


People also ask

Can we use multiple font family in CSS?

The font-family property can hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font. There are two types of font family names: family-name - The name of a font-family, like "times", "courier", "arial", etc.

Which property and method is used for font style on a canvas?

There are two methods fillText() and strokeText() to draw text on canvas. You can use font property (type : string) to specify a number of text setting such as style, weight, size, and font. The style can be normal, italic, or bold. Default style is normal.


1 Answers

Canvas's font uses the same syntax as the CSS font attribute. So try:

ctx.font = "40pt MarkerFelt-Thin, Comic Sans MS";
like image 112
kennytm Avatar answered Sep 30 '22 09:09

kennytm