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.
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:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Note: This function can not be accessible through an online compiler because the font file is missing.