Skip to content

Commit 548f5e4

Browse files
authored
docs: Add text quality information (#3620)
* docs: Add text quality information * add another playground * add mount visible
1 parent f46f72a commit 548f5e4

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

site/docs/04-graphics/04.1-text.mdx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ slug: /text
44
section: Graphics
55
---
66

7+
import FontQuality from '!!raw-loader!./examples/font-quality.ts';
8+
import FontLabel from '!!raw-loader!./examples/font.ts';
9+
710
You may want to display [[Text]] in your game, this can be achieved with the [[Text]], [[Font]], and [[SpriteFont]].
811

912
## Text
@@ -53,6 +56,8 @@ const label = new ex.Label({
5356
});
5457
```
5558

59+
60+
5661
## Font
5762

5863
A [[Font]] is a traditional system or loaded web font.
@@ -98,9 +103,36 @@ var text = new ex.Text({
98103
});
99104
```
100105

106+
<PlaygroundEmbed
107+
title="Font"
108+
code={FontLabel}
109+
mount="visible"
110+
/>
111+
112+
### Loading External Fonts
113+
114+
101115
It is recommended you load any font important to your game and not rely on any pre-installed system fonts, those will be different from computer to computer.
102116

103-
For example the [google fonts](https://fonts.google.com/) loader is a common way to do this.
117+
```typescript
118+
const fontSource = new ex.FontSource('./Gorgeous Pixel.ttf', 'Gorgeous Pixel');
119+
120+
// load the font in ex.Loader or directly with .load()
121+
await fontSource.load();
122+
123+
const font = fontSource.toFont({
124+
family: 'Gorgeous Pixel',
125+
color: ex.Color.White,
126+
size: 30,
127+
shadow: {
128+
blur: 15,
129+
color: ex.Color.Black
130+
}
131+
});
132+
133+
```
134+
135+
It is also possible to use the [google fonts](https://fonts.google.com/) loader to do this.
104136

105137
```html
106138
<link rel="preconnect" href="https://fonts.googleapis.com" />
@@ -119,7 +151,7 @@ const font = new ex.Font({
119151
})
120152
```
121153

122-
One potential issue with using web fonts is they won't render correctly until loaded see [article for details](https://erikonarheim.com/posts/dont-test-fonts/).
154+
One potential issue with using web fonts from google is they won't render correctly until loaded see [article for details](https://erikonarheim.com/posts/dont-test-fonts/).
123155

124156
```typescript
125157
async function waitForFontLoad(font, timeout = 2000, interval = 100) {
@@ -147,6 +179,25 @@ const game = new ex.Engine({...})
147179
await game.start();
148180
```
149181

182+
## Text Quality
183+
184+
Depending on your game you may run into a situation where you have blurry looking text, this can be caused by a few reasons and fixed by adjusting a few settings.
185+
186+
Most of the strategies involve artificially giving the text more pixels to work with to render a more crisp result.
187+
188+
1. Increase the `quality` (default 2), this works by upscaling the bitmap the text is drawn to internally, more pixels can yield higher fidelity.
189+
2. Increase the "resolution" of your game using the `pixelRatio`, this is especially necessary if you have a very low resolutions (less than 500x500).
190+
3. If you cannot upscale (or don't want to) you need to line up your font glyphs to align with pixels in the canvas.
191+
4. Reach for a sprite font tailored for the resolution you're working in (often this is useful in low res situations)>
192+
193+
194+
<PlaygroundEmbed
195+
title="FontQuality"
196+
code={FontQuality}
197+
mount="visible"
198+
/>
199+
200+
150201
## SpriteFont
151202

152203
Sometimes you want to use a custom font based on your [spritesheet](#spritesheet) of character glyphs
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as ex from 'excalibur';
2+
3+
const game = new ex.Engine({
4+
canvasElementId: 'preview-canvas',
5+
displayMode: ex.DisplayMode.FitContainer,
6+
width: 200, // <-- Low res game (< 500x500)
7+
height: 200, // logical resolution remains at 200x200
8+
pixelRatio: 4, // <-- Upscale to something bigger than 500x500, in this case 800x800
9+
});
10+
11+
const loader = new ex.Loader();
12+
13+
const fontSource = new ex.FontSource(
14+
'https://fonts.gstatic.com/s/pixelifysans/v3/CHylV-3HFUT7aC4iv1TxGDR9Jn0Eiw.woff2',
15+
'Pixelify Sans'
16+
);
17+
loader.addResource(fontSource);
18+
19+
const font = fontSource.toFont({
20+
padding: 10,
21+
lineHeight: 25,
22+
color: ex.Color.White,
23+
quality: 4, // <-- Increase quality scales up the backing image used to draw text (default 2)
24+
size: 30,
25+
shadow: {
26+
blur: 15,
27+
color: ex.Color.Black
28+
}
29+
});
30+
31+
const label = new ex.Label({
32+
pos: ex.vec(0, 0),
33+
text: 'Some Pixel text can fill up the screen a whole bunch with nice quality!!!!',
34+
maxWidth: 220,
35+
font
36+
});
37+
38+
game.add(label);
39+
game.start(loader);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as ex from 'excalibur';
2+
3+
const game = new ex.Engine({
4+
canvasElementId: 'preview-canvas',
5+
displayMode: ex.DisplayMode.FitContainer,
6+
width: 200,
7+
height: 200,
8+
pixelRatio: 4
9+
});
10+
11+
const label = new ex.Label({
12+
text: 'Some Default\nSans Serif',
13+
pos: ex.vec(100, 100),
14+
font: new ex.Font({
15+
family: 'sans-serif',
16+
size: 24,
17+
color: ex.Color.Red,
18+
baseAlign: ex.BaseAlign.Middle,
19+
textAlign: ex.TextAlign.Center,
20+
unit: ex.FontUnit.Px
21+
})
22+
});
23+
game.add(label);
24+
25+
game.start();

0 commit comments

Comments
 (0)