Skip to content

Commit 96c471b

Browse files
marklundinCopilot
andauthored
Add useFont hook (#166)
* Add useFont hook for loading font assets and update documentation - Introduced the useFont hook to facilitate loading font assets as SDF textures. - Updated the use-asset documentation to include usage examples for the new useFont hook. - Modified index.ts to export the useFont hook alongside existing hooks. * Update packages/lib/src/hooks/use-asset.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 928ec44 commit 96c471b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

packages/docs/content/docs/api/hooks/use-asset.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ export function RenderEnvAtlas() {
165165

166166
See the [EnvAtlas](/docs/api/components/EnvAtlas) component for more information.
167167

168+
## useFont
169+
170+
Text in PlayCanvas is rendered using Signed-Distance-Fields (SDF's), so you'll need to convert your ttf fonts into the appropriate format before loading them.
171+
172+
If you're using Vite or Rollup there's an official plugin to convert ttf files for you at build time. Check out the [@playcanvas/rollup](https://www.npmjs.com/package/@playcanvas/plugin) for more information.
173+
174+
You can import a ttf using the `?sdf` query parameter whhich will automatically convert it into an SDF texture at build time.
175+
176+
```tsx copy filename="render-font.tsx"
177+
178+
// Use the @playcanvas/rollup plugin to convert the ttf file into an SDF texture at build time.
179+
import inconsolata from "@assets/fonts/inconsolata.ttf?sdf";
180+
import { useFont } from '@playcanvas/react/hooks';
181+
182+
export function InconsolataFont() {
183+
const { asset, loading, error } = useFont(inconsolata);
184+
185+
if (loading) return <LoadingSpinner />;
186+
if (error) return <ErrorMessage message={error} />;
187+
if (!asset) return null;
188+
189+
return asset;
190+
}
191+
```
192+
168193
## useAsset
169194

170195
This is a generic hook for loading any type of asset. You can use it to load any asset type that PlayCanvas supports which is sometimes useful if you need to load an asset dynamically.

packages/lib/src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export { useScript } from './use-script';
55
export { useApp, AppContext } from './use-app';
66
export { useParent, ParentContext } from './use-parent';
77
export { useMaterial } from './use-material'
8-
export { useAsset, useSplat, useTexture, useEnvAtlas, useModel } from './use-asset';
8+
export { useAsset, useSplat, useTexture, useEnvAtlas, useModel, useFont } from './use-asset';
99
export { useFrame } from './use-frame';
1010
export type { AssetResult } from './use-asset';

packages/lib/src/hooks/use-asset.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,29 @@ export const useModel = (
253253
props: Record<string, unknown> = {},
254254
): AssetResult =>
255255
useAsset(src, 'container', props);
256+
257+
/**
258+
* Simple hook to load a font asset.
259+
*
260+
* Fonts are described SDF textures, so you'll need to convert your .ttf files to SDF textures.
261+
* into the correct format.
262+
*
263+
* Learn more about SDF textures {@link https://playcanvas-react.vercel.app/docs/api/hooks/use-asset#usefont}.
264+
*
265+
* @param src - The source URL of the font asset.
266+
* @param props - Additional properties to pass to the asset loader.
267+
* @returns An object containing the asset, loading state, and any error.
268+
*
269+
* @example
270+
* ```tsx
271+
* import roboto from '@assets/fonts/roboto.ttf?url';
272+
* export const App = () => {
273+
* const { asset, loading, error } = useFont(roboto);
274+
* }
275+
* ```
276+
*/
277+
export const useFont = (
278+
src: string,
279+
props: Record<string, unknown> = {},
280+
): AssetResult =>
281+
useAsset(src, 'font', props);

0 commit comments

Comments
 (0)