processing / processing/p5.js-web-editor

Fonts loaded by `loadFont()` cannot be used in CSS styles

Open
#2,750 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Preview Bug
Dominant language
JavaScript
Stars
1.7k
Forks
1.7k
Avg merge
3d 4h
Merged PRs (30d)
8

Description

p5.js version

1.9.0

What is your operating system?

Mac OS

Web browser and version

Chrome 120, Firefox 120, Safari 17.2

Actual Behavior

When I load a font with loadFont(), I can't use it to style HTML elements.

Expected Behavior

I should be able to set the font-family for HTML elements as shown in the last example.

Steps to reproduce

Here's a sketch that demonstrates the bug.

@raclim @lindapaiste @dhowe @paulaxisabel @SarveshLimaye @SkylerW99 @BamaCharanChhandogi @Obi-Engine10 @hannahvy @singshris @hiddenenigma I'm happy to help with this where I can!

let font;

// Load the font.
function preload() {
  font = loadFont("assets/PressStart2P-Regular.ttf");
}

function setup() {
  createCanvas(400, 400);

  // Paint the background.
  background(0);

  // Use the font in the canvas.
  fill(255);
  textAlign(CENTER);
  textFont(font, 22);
  text("Are you in?", width / 2, height / 2);

  // Create a <button> element.
  let button = createButton("Yes");
  button.size(100);
  button.position(width / 2 - 50, height / 2 + 30);
  
  // Style the button.
  button.style("background-color", "black");
  button.style("color", "white");
  button.style("border-radius", "10px");
  button.style("padding", "5px");

  // Try to set the button's font-family.

  // This works locally.
  button.style("font-family", "PressStart2P-Regular");

  // The code below makes the font-family work
  // in the p5.js Web Editor.
  
  // let f = new FontFace(
  //   "PressStart2P-Regular",
  //   "url(assets/PressStart2P-Regular.ttf)"
  // );
  // document.fonts.add(f);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproducible sketch and compare loadFont() with the FontFace workaround shown in the issue. Trace how the p5.js Web Editor loads the font, then verify that a button styled with font-family can use it in Chrome, Firefox, and Safari. Done means the issue's expected HTML styling works without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.