How to use Google fonts in P5JS online editor


I have encountered this short video which explains how to manipulate the “index.html” and “style.css” in order to be load use Google fonts.

Hope that helps


add a example could also help:


I have a problem with this as it doesn’t use the font on the first run.
Is there a way to preload a font that is loaded in the html header?
2nd time it’s run is uses the font.

BTW that video is pretty incomplete.

Following the tips from the article below: :wink:

You can add this <link> tag to your “index.html” file in order to explicitly preload a specific font file: :blush:

  rel="preload" as="font" crossorigin

BtW, the “.woff2” file link above corresponds to the /* latin */ @font-face { from: :face_with_monocle:

P.S.: It seems to utterly fail on Firefox unfortunately! :fox_face:

Thanks, adding this to the index.html

rel="preload" as="font" crossorigin

And this to the CSS files worked great.

@font-face {
  font-family: 'Baloo';
  font-style: normal;
  font-weight: 400;
  src: local('Baloo Regular'), local('Baloo-Regular'), url( format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;

Notice that your current shortened posted CSS is already present within: :nerd_face:

Which in turn, was already being loaded by your previous “index.html” file: :money_mouth_face:

<link href="" rel="stylesheet">

Yes, I see. So i could add either the font face element into the css file or include the link style sheet above. Thanks.

Works well but how can I now future proof it as google changes these url’s baloo/v4/6xKhdSpJJ92I9MWPCm4.woff2 when the fonts updated.

<link rel="preload" href="" as="style">
<link rel="stylesheet" href="">


<link rel="preload" href="" as="font" crossorigin>
<link rel="stylesheet" href="">

Doesn’t seem to work.

We can always download those font links, like those “.woff2” files, and place them locally together w/ our “.html” file. :smiling_imp:

Now, rather than loading them from their original remote URL, we grab them locally: :bulb:

<link href="6xKhdSpJJ92I9MWPCm4.woff2" rel="preload" as="font" crossorigin>

Notice though that link actually gets a “.css” file. :link:

So we can’t use the attribute as="font" on such a link! :grimacing:

Also, “.css” files don’t preload their font links right way. :roll_eyes:

Only after their actually requested for the 1st time. :woozy_face:

Therefore, using <link rel="preload" as="style"> on them is merely a tiny loading performance gain. :flushed:

But got nothing to do in actually caching their fonts! :cry:

1 Like

I guess it would be messy to write some code that extracts the url for the woff2 font from the css file and then use that as a preload and it would have to run before the preload.

So yes, if I need the sketch to function well forever I should download the font and load it into the web editors files.

As I had warned before, I couldn’t make the Firefox-based browsers to work w/: :fox_face:
<link rel="preload" as="font" crossorigin href="">.

Therefore, if we have the font file (be it a local or a remote URL link) itself, I think we’re better off using p5js’ loadFont() inside preload() instead: :thinking:


So those fonts are ready to use right way across all browser types. :sailboat:

P.S.: Not sure whether loadFont() is compatible w/ “.woff2” font files though. :confounded:

Its reference doesn’t mention that font extension at all! :cold_sweat:

Although I’ve spotted the “.woff2” extension somewhere within p5js’ source code: :see_no_evil:

So there’s real hope it should work: :upside_down_face: