Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to use custom font on startup, often shows the browser's default font #15

Open
derolf opened this issue Aug 16, 2021 · 3 comments

Comments

@derolf
Copy link

derolf commented Aug 16, 2021

I am loading the Roboto font in index.html:

<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet" />

Then trying to use it in a TextSprite:

 const instance = new TextSprite({
    alignment: "left",
    color: "#24ff00",
    fontFamily: "Roboto",
    fontSize: 1,
    text: text,
  });

Sometimes, the TextSprite renders the browser's default font instead of Roboto. Any idea to safely wait for Roboto?

@derolf derolf changed the title Trying to use Robot on startup, often it shows the browser default font Trying to use custom font on startup, often shows the browser's default font Aug 16, 2021
@Filyus
Copy link

Filyus commented Jun 29, 2022

Possible solution from https://stackoverflow.com/a/64192936:

export async function waitForFontLoad(
    font: string,
    timeout = 1000,
    interval = 10
) {
    return new Promise((resolve, reject) => {
        // repeatedly poll check
        const poller = setInterval(async () => {
            try {
                await document.fonts.load(font);
            } catch (err) {
                reject(err);
            }
            if (document.fonts.check(font)) {
                clearInterval(poller);
                resolve(true);
            }
        }, interval);
        setTimeout(() => clearInterval(poller), timeout);
    });
}

@Filyus
Copy link

Filyus commented Jun 29, 2022

Recommendation to use document.fonts.onloadingdone instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants