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

Use Text3D with already loaded font #1050

Open
wbobeirne opened this issue Sep 13, 2022 · 3 comments
Open

Use Text3D with already loaded font #1050

wbobeirne opened this issue Sep 13, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@wbobeirne
Copy link

Describe the feature you'd like:

I've already loaded a font using FontLoader, and I'd like to re-use it rather than fetching it again. However, Text3D doesn't have a way to pass the already loaded font, only a string URL.

Suggested implementation:

Either a new prop on Text3D, or extend the font: string | FontData prop to be font: string | FontData | Font

Sorry if I'm incorrect about something or just not using this properly!

@wbobeirne wbobeirne added the enhancement New feature or request label Sep 13, 2022
@drcmda
Copy link
Member

drcmda commented Sep 21, 2022

i think the easiest/fastest would be if you add it https://github.com/pmndrs/drei/blob/master/src/core/Text3D.tsx

@FarazzShaikh
Copy link
Member

Text3D already supports FontData input. FontLoader.load results in a FontData object right? I’m not sure where your Font type comes from

@mz8i
Copy link

mz8i commented Sep 16, 2024

@FarazzShaikh The problem with the current interface is that it doesn't fit well with the interface of useFont.
In fact, the current docs show an example that will actually error:

const font = useFont('/fonts/helvetiker_regular.typeface.json')
return <Text3D font={font} />

To summarise why:

  • FontData is the interface for the font JSON file structure. FontLoader.load(url: string) returns an object that should conform to FontData
  • FontLoader.parse(fontData: FontData) is really just return new Font(fontData) under the hood, and it returns an instance of class Font, where the data property is of type FontData
  • useFont() also returns an instance of Font
  • Text3D accepts FontData | string for the font attribute

@wbobeirne probably tried to follow the example, which didn't work, hence the suggestion.

As a workaround, you could do:

const font = useFont('/fonts/helvetiker_regular.typeface.json')
const fontData = font.data
return <Text3D font={fontData} />

But I would agree that it should be possible to pass a Font loaded elsewhere to Text3D.
I am currently about to submit a PR for #2094 - I will fix the current useFont documentation there, but can later submit another PR to fix the issue presented here.

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

No branches or pull requests

4 participants