Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Font on HTML5 Canvas

I am completely baffled as to how to change the font I'm using when drawing text on a canvas. The following is the font I have defined in my CSS:

@font-face{
 font-family:"Officina";
 src:url(OfficinaSansStd-Book.otf);
}

Now in my HTML/JavaScript I'm tempted to say:

context.font = '30px "Officina"';

But this doesn't work. It works fine if I use a web available font (like Arial), and the Officina font shows up fine when I just write plain text directly to the webpage. What am I missing?

like image 200
cryptic_star Avatar asked May 24 '11 22:05

cryptic_star


People also ask

How do I add a font to canvas?

canvas = document. querySelector('canvas'); var myFont = new FontFace('myFont', 'url(assets/fonts/myFont/myFont. otf)'); Now we create our font object using the javascript class FontFace which receives the font family and the source.

Can I use different fonts in canvas?

There is currently no way to change the font anywhere in canvas via the normal rich text editor. You can change the font by using the HTML editor.

Is font supported in HTML5?

Not Supported in HTML5. The <font> tag was used in HTML 4 to specify the font face, font size, and color of text.

How to create text using different font in HTML5 canvas?

HTML5 canvas provides capabilities to create text using different font and text properties listed below −. font [ = value ] This property returns the current font settings and can be set, to change the font. textAlign [ = value ] This property returns the current text alignment settings and can be set, to change the alignment.

What is the use of font in canvas?

Definition and Usage. The font property sets or returns the current font properties for text content on the canvas. The font property uses the same syntax as the CSS font property.

How to draw text using HTML5 canvas?

To draw text using HTML5 Canvas, we can use the font property and the fillText () method of the canvas context. To set the font, size, and style of HTML5 Canvas text, we can set the font property of the canvas context to a string containing the font style, size, and family, delimited by spaces.

What is the font property in HTML?

The font property sets or returns the current font properties for text content on the canvas. The font property uses the same syntax as the CSS font property. Specifies the font style. Possible values: Specifies the font variant.


4 Answers

To get cross-browser compatibility you should use CSS for the embedded font like this:

@font-face {
    font-family: 'BankGothicMdBTMedium';
    src: url('bankgthd-webfont.eot');
    src: local('BankGothic Md BT'), local('BankGothicBTMedium'), url('bankgthd-webfont.woff') format('woff'), url('bankgthd-webfont.ttf') format('truetype'), url('bankgthd-webfont.svg#webfontNgZtDOtM') format('svg');
    font-weight: normal;
    font-style: normal;
}

Note: I got those font files somewhere at http://fontsquirrel.com

This is working for me, but I'm using this font also in HTML markup, so maybe a workaround (if the font-face definition doesn't help) can be using that font in some hidden div, of course I'm running all JS after body loads.

like image 76
tomasb Avatar answered Oct 19 '22 19:10

tomasb


For folks coming to this question circa 2017 onwards it would be best to use the Web Font Loader that's co-produced by Google and Typekit located here: - https://github.com/typekit/webfontloader

like image 25
JoeManFoo Avatar answered Oct 19 '22 19:10

JoeManFoo


You can use the Google Web Font loader, but that's rather heavyweight and/or annoying for many uses. Instead, I'll recommend Jennifer Simonds' FontDetect library, available on GitHub:

A JavaScript class you can use to determine whether a webfont got loaded, which font is being used by an element, or react to a webfont getting loaded (or failing to load).

like image 2
John Whitley Avatar answered Oct 19 '22 18:10

John Whitley


This is previous question should help you. Drawing text to <canvas> with @font-face does not work at the first time

like image 1
tcnarss Avatar answered Oct 19 '22 19:10

tcnarss