Home > Web Front-end > HTML Tutorial > Drawing text in HTML5 using @fontface doesn't work first time

Drawing text in HTML5 using @fontface doesn't work first time

PHPz
Release: 2023-09-17 19:17:10
forward
1189 people have browsed it

Drawing text in HTML5 using @fontface doesnt work first time

Drawing text in the canvas using a font loaded via @font-face does not display the text correctly at first. This is because the browser hasn't loaded the font from the network yet. Therefore, it uses fonts that are already available.

Fonts must be loaded before use. This can be ensured using the tag. If you want to make sure that the font is available and some other elements are preloaded, then you can do this using the tag like this

<div style="font-family: PressStart;"></div>  
Copy after login

You can also load the font like this -

var newFont = new FontFace(&lsquo;New Font&#39;, &#39;url(https://samplefont.woff2)&#39;);  
newFont.load().then(function(font){  
   document.fonts.add(font);  
   alert(&#39;Font successfully loaded!&#39;);  
});                  
Copy after login

The above is the detailed content of Drawing text in HTML5 using @fontface doesn't work first time. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template