p5.js | loadFont() Function

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 laoded. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

let newFont;
   
// Loading font
function preload() {
  newFont = 
    loadFont('fonts/Montserrat.otf');
}
   
//  Canvas area and color 
function 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 font
function mouseClicked() {
  textFont(newFont);
  textSize(20);
  text('Using the Montserrat font',
                        20, 140);
    
  text('This is text written using'
        + ' the new font', 20, 160);
}

chevron_right


Output:
ex1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

let newFont;
   
// Canvas area and loading font
function setup() {
  createCanvas(500, 200);
  textSize(20);
   
  text('The new font would be displayed'
        + ' when loaded', 20, 20);
  loadFont('fonts/Montserrat.otf', fontLoaded);
}
   
// Changing font
function 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');
}

chevron_right


Output:
ex2

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/loadfont

Note: This function can not be accessible through an online compiler because the font file is missing.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.