Saturday, September 28, 2024
Google search engine
HomeLanguagesJavascriptp5.js loadFont() Function

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 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 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);
}


Output: 
 

ex1

Example 2: 
 

javascript




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');
}


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.
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments