p5.js | loadFont() Function

16 Oct, 2021
The loadFont() function is used to load a font from file or an URL, and return it as a PFont Object. The font loaded is an opentype font file with the formats .otf or .ttf. This method is asynchronous in nature and therefore it may not finish before the font can be used. It is hence advised to load the font in the preload() function. The font is preferably loaded from a relative path as some browsers may prevent loading from other remote locations due to the browser’s security features.Syntax:  
loadFont(path, callback, onError)
Parameters: This function accepts three parameter as mentioned above and described below:  
path: It is the path from where the font has to be loaded. It can be a
callback: This is a function which after the font is loaded and it is an optional parameter.
onError: This is a function which is called if the font does not load due to any error and it is an optional parameter.
Return Value: It returns a p5.Font object with the given font.Below examples illustrate the loadFont() function in p5.js:Example 1: 
javascript
let newFont;  // Loading fontfunction preload() {  newFont =    loadFont('fonts/Montserrat.otf');}  //  Canvas area and colorfunction setup() {  createCanvas(400, 200);  textSize(20);  fill("red");  text('Click once to write using '        + 'a new font', 20, 20);  fill("black");    text('Using the default font', 20, 60);  text('This is text written using'        + ' the new font', 20, 80);}  // Changing fontfunction mouseClicked() {  textFont(newFont);  textSize(20);  text('Using the Montserrat font',                        20, 140);     text('This is text written using'        + ' the new font', 20, 160);}
Output:  
Example 2:  
javascript
let newFont;  // Canvas area and loading fontfunction setup() {  createCanvas(500, 200);  textSize(20);    text('The new font would be displayed'        + ' when loaded', 20, 20);  loadFont('fonts/Montserrat.otf', fontLoaded);}  // Changing fontfunction fontLoaded(newFont) {  textFont(newFont);  text('The font has completed loading!', 20, 60);    let textDiv = createDiv('This is text written'                + ' using the new font');  textDiv.position(30, 80);  textDiv.style('font-family', 'Montserrat');}
Output:  
Online editor: https://editor.p5js.org/Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/Reference: https://p5js.org/reference/#/p5/loadfontNote: This function can not be accessible through an online compiler because the font file is missing. 
surindertarika1234
JavaScript-p5.js
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Roadmap to Learn JavaScript For Beginners
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?