q5 2.4.4 → 2.4.10

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.
@@ -9,6 +9,8 @@ Q5.renderers.webgpu.text = ($, q) => {
9
9
  q._preloadCount--;
10
10
  });
11
11
  };
12
+
13
+ // directly add these text setting functions to the webgpu renderer
12
14
  $.textFont = t.textFont;
13
15
  $.textSize = t.textSize;
14
16
  $.textLeading = t.textLeading;
@@ -24,7 +26,20 @@ Q5.renderers.webgpu.text = ($, q) => {
24
26
  $.text = (str, x, y, w, h) => {
25
27
  let img = t.createTextImage(str, w, h);
26
28
 
27
- if (img.canvas.textureIndex == undefined) $._createTexture(img);
29
+ if (img.canvas.textureIndex === undefined) {
30
+ $._createTexture(img);
31
+ } else if (img.modified) {
32
+ let cnv = img.canvas;
33
+ let textureSize = [cnv.width, cnv.height, 1];
34
+ let texture = $._textures[cnv.textureIndex];
35
+
36
+ Q5.device.queue.copyExternalImageToTexture(
37
+ { source: cnv },
38
+ { texture, colorSpace: $.canvas.colorSpace },
39
+ textureSize
40
+ );
41
+ img.modified = false;
42
+ }
28
43
 
29
44
  $.textImage(img, x, y);
30
45
  };
@@ -32,12 +47,20 @@ Q5.renderers.webgpu.text = ($, q) => {
32
47
  $.createTextImage = t.createTextImage;
33
48
 
34
49
  $.textImage = (img, x, y) => {
35
- if (t.ctx.textAlign == 'center') x -= img.width * 0.5;
36
- else if (t.ctx.textAlign == 'right') x -= img.width;
37
- if (t.ctx.textBaseline == 'alphabetic') y -= t._textLeading;
38
- if (t.ctx.textBaseline == 'middle') y -= img._descent + img._ascent * 0.5 + t._textLeadDiff;
39
- else if (t.ctx.textBaseline == 'bottom') y -= img._ascent + img._descent + t._textLeadDiff;
40
- else if (t.ctx.textBaseline == 'top') y -= img._descent + t._textLeadDiff;
50
+ let og = $._imageMode;
51
+ $._imageMode = 'corner';
52
+
53
+ let ta = t._textAlign;
54
+ if (ta == 'center') x -= img.canvas.hw;
55
+ else if (ta == 'right') x -= img.width;
56
+
57
+ let bl = t._textBaseline;
58
+ if (bl == 'alphabetic') y -= t._textLeading;
59
+ else if (bl == 'middle') y -= img._middle;
60
+ else if (bl == 'bottom') y -= img._bottom;
61
+ else if (bl == 'top') y -= img._top;
62
+
41
63
  $.image(img, x, y);
64
+ $._imageMode = og;
42
65
  };
43
66
  };
package/src/readme.md CHANGED
@@ -105,7 +105,7 @@ Image based features in this module require the q5-2d-image module.
105
105
 
106
106
  `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
107
 
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.
108
+ `textCache(bool, maxSize)` enables or disables text caching.
109
109
 
110
110
  ## webgpu-canvas
111
111
 
@@ -181,17 +181,9 @@ Implemented functions:
181
181
 
182
182
  > Use `textFill` and `textStroke` to set text colors.
183
183
 
184
- WebGPU (and WebGL) don't have HTML5 based text rasterization functionality like Canvas2D does.
184
+ 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.
185
185
 
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. For typical use, 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.
187
-
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.
189
-
190
- As long as text content doesn't change often, this method is 4x more efficient.
191
-
192
- As a best practice, separate static text from dynamic text rendering. For example render labels like "Score: " and corresponding values with separate calls to `text`.
193
-
194
- Complete implementation of text rendering in WebGPU.
186
+ Implemented functions:
195
187
 
196
188
  `loadFont`,`textFont`, `textSize`, `textLeading`, `textStyle`, `textAlign`, `textWidth`, `textAscent`, `textDescent`, `textFill`, `textStroke`, `text`
197
189