q5 2.4.5 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/readme.md CHANGED
@@ -54,6 +54,11 @@ WebGPU rendering modules are in development:
54
54
  - [webgpu-drawing](#webgpu-drawing)
55
55
  - [webgpu-image](#webgpu-image)
56
56
  - [webgpu-text](#webgpu-text)
57
+ - [Default Font](#default-font)
58
+ - [Loading Custom Fonts](#loading-custom-fonts)
59
+ - [Displaying Emojis](#displaying-emojis)
60
+ - [Lightweight Use](#lightweight-use)
61
+ - [Implemented functions](#implemented-functions)
57
62
  - [math](#math)
58
63
  - [noisier](#noisier)
59
64
 
@@ -105,7 +110,7 @@ Image based features in this module require the q5-2d-image module.
105
110
 
106
111
  `textImage(img, x, y)` displays text images, complying with the user's text position settings instead of their image position settings. The idea is that text will appear in the same place as it would if it were drawn with the `text` function.
107
112
 
108
- `textCache(bool, maxSize)` enables or disables text caching. As of June 2024, drawing rotated text is super slow in all browsers, so q5 creates and stores images of text and rotates that instead. Can improve rendering performance 90x but uses more memory. `maxSize` param determines the maximum number of text images to cache, default is 500 since these images will typically be quite small. The text image cache (tic) is a timed cache, so the oldest images are removed first.
113
+ `textCache(bool, maxSize)` enables or disables text caching.
109
114
 
110
115
  ## webgpu-canvas
111
116
 
@@ -179,21 +184,83 @@ Implemented functions:
179
184
 
180
185
  ## webgpu-text
181
186
 
182
- > Use `textFill` and `textStroke` to set text colors.
187
+ The q5 WebGPU text renderer uses the multi-channel signed distance fields (MSDF) technique for high performance and high quality real-time text rendering. Text can be rapidly recolored, rotated, and scaled without any loss in quality or performance.
183
188
 
184
- WebGPU (and WebGL) don't have fast HTML5 based text rasterization functionality, like Canvas2D does.
189
+ MSDF, introduced by Chlumsky Viktor in his master's thesis ["Shape Decomposition for Multi-channel Distance Fields" (2015)](https://dspace.cvut.cz/bitstream/handle/10467/62770/F8-DP-2015-Chlumsky-Viktor-thesis.pdf), improves upon the signed distance field (SDF) technique, popularized by Chris Green and [Valve Software](https://www.valvesoftware.com/en/) in ["Improved Alpha-Tested Magnification for Vector Textures and Special Effects" (2007)](https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf).
185
190
 
186
- In p5.js WebGL mode, text is drawn directly to the canvas. This is a complex task, since letters have intricate geometry: thus many triangles must be used to render text at high resolution. Unless a user wants to render a lot of text, the performance cost is actually negligible. Yet since p5.js depends on opentype.js for this, which is 528kb (171kb minified), a different approach was needed to keep q5 lightweight.
191
+ | SDF | MSDF |
192
+ | -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
193
+ | ![demo-sdf16](https://user-images.githubusercontent.com/18639794/106391905-e679af00-63ef-11eb-96c3-993176330911.png) | ![demo-msdf16](https://user-images.githubusercontent.com/18639794/106391899-e37ebe80-63ef-11eb-988b-4764004bb196.png) |
187
194
 
188
- Internally, q5's WebGPU renderer uses a q5 graphics object to draw text to a Canvas2D canvas via `createTextImage`, then converts that canvas to a WebGPU texture. Each texture is cached, so it doesn't have to be recreated every frame that users want to display the same text.
195
+ ### Default Font
189
196
 
190
- Creating a text image is slower than rendering text if it's only displayed for one frame, but displaying static text multiple frames from a cached image is way faster than re-rendering the text. So just try not to have long strings of text that change every frame.
197
+ For convenience, if no font is loaded before `text` is run, then q5's default MSDF font is loaded: https://q5js.org/fonts/YaHei-msdf.json
191
198
 
192
- For typical use cases, this is a great trade-off!
199
+ ![YaHei msdf texture](https://q5js.org/fonts/YaHei.png)
193
200
 
194
- Complete implementation of text rendering in WebGPU.
201
+ This 512x512 msdf texture (207kb) was made with the [Microsoft YaHei](https://learn.microsoft.com/en-us/typography/font-list/microsoft-yahei) font and stores every character visible on a standard English keyboard, letters with diacritics (accents) used in European languages, and mathematical symbols.
195
202
 
196
- `loadFont`,`textFont`, `textSize`, `textLeading`, `textStyle`, `textAlign`, `textWidth`, `textAscent`, `textDescent`, `textFill`, `textStroke`, `text`
203
+ ```
204
+ !"#$%&'()\*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^\_`abcdefghijklmnopqrstuvwxyz{|}~€¡¢£¥©®°²³´·¹º¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ‘’“”π
205
+ ```
206
+
207
+ > Do you think any other characters ought to be included in the default set? Let us know! https://github.com/q5js/q5.js/issues
208
+
209
+ ### Loading Custom Fonts
210
+
211
+ You can choose a custom set of characters and convert fonts to MSDF format by using the [msdf-bmfont-xml](https://msdf-bmfont.donmccurdy.com/) website, created by Don McCurdy.
212
+
213
+ Here's how to load an MSDF font:
214
+
215
+ ```js
216
+ function preload() {
217
+ loadFont('arial-msdf.json');
218
+ }
219
+
220
+ function setup() {
221
+ createCanvas(200, 200);
222
+ }
223
+
224
+ function draw() {
225
+ fill(0.71, 0.92, 1);
226
+ text('Hello, World!', mouseX, mouseY);
227
+ }
228
+
229
+ Q5.webgpu();
230
+ ```
231
+
232
+ ### Displaying Emojis
233
+
234
+ Full color emoji characters can't be rendered using the MSDF technique, so use `createTextImage` and display them with `textImage`:
235
+
236
+ ```js
237
+ let puppy;
238
+
239
+ function setup() {
240
+ createCanvas(200, 200);
241
+ textSize(100);
242
+ puppy = createTextImage('🐶');
243
+ }
244
+
245
+ function draw() {
246
+ textAlign(CENTER, CENTER);
247
+ textImage(puppy, 0, 0);
248
+ }
249
+
250
+ Q5.webgpu();
251
+ ```
252
+
253
+ ### Lightweight Use
254
+
255
+ For super lightweight use load <https://q5js.org/fonts/YaHei-256-msdf.json>, which has a limited character set of english letters and some common punctuation symbols that completely fill in a 256x256 texture (73kb).
256
+
257
+ ```
258
+ !@'",-.0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
259
+ ```
260
+
261
+ ### Implemented functions
262
+
263
+ `loadFont`, `text`, `textSize`, `textAlign`, `textWidth`, `createTextImage`, `textImage`
197
264
 
198
265
  ## math
199
266