q5 2.9.21 → 2.9.23
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/.vscode/launch.json +26 -0
- package/bun.lockb +0 -0
- package/p5-tests/js/chai_helpers.js +20 -0
- package/p5-tests/js/mocha_setup.js +2 -0
- package/p5-tests/js/modernizr.js +5 -0
- package/p5-tests/js/p5_helpers.js +135 -0
- package/p5-tests/js/sinon.js +5949 -0
- package/p5-tests/mocha.css +289 -0
- package/p5-tests/test.html +71 -0
- package/p5-tests/unit/color/color_conversion.js +68 -0
- package/p5-tests/unit/color/creating_reading.js +217 -0
- package/p5-tests/unit/color/p5.Color.js +1000 -0
- package/p5-tests/unit/color/setting.js +289 -0
- package/p5-tests/unit/core/2d_primitives.js +490 -0
- package/p5-tests/unit/core/attributes.js +115 -0
- package/p5-tests/unit/core/curves.js +139 -0
- package/p5-tests/unit/core/environment.js +248 -0
- package/p5-tests/unit/core/error_helpers.js +1158 -0
- package/p5-tests/unit/core/main.js +340 -0
- package/p5-tests/unit/core/p5.Element.js +773 -0
- package/p5-tests/unit/core/p5.Graphics.js +179 -0
- package/p5-tests/unit/core/preload.js +285 -0
- package/p5-tests/unit/core/rendering.js +116 -0
- package/p5-tests/unit/core/structure.js +293 -0
- package/p5-tests/unit/core/transform.js +144 -0
- package/p5-tests/unit/core/version.js +28 -0
- package/p5-tests/unit/core/vertex.js +137 -0
- package/p5-tests/unit/dom/dom.js +2146 -0
- package/p5-tests/unit/events/acceleration.js +213 -0
- package/p5-tests/unit/events/keyboard.js +179 -0
- package/p5-tests/unit/events/mouse.js +487 -0
- package/p5-tests/unit/events/touch.js +180 -0
- package/p5-tests/unit/image/downloading.js +379 -0
- package/p5-tests/unit/image/filters.js +92 -0
- package/p5-tests/unit/image/loading.js +413 -0
- package/p5-tests/unit/image/p5.Image.js +201 -0
- package/p5-tests/unit/image/pixels.js +234 -0
- package/p5-tests/unit/io/files.js +378 -0
- package/p5-tests/unit/io/loadBytes.js +149 -0
- package/p5-tests/unit/io/loadImage.js +123 -0
- package/p5-tests/unit/io/loadJSON.js +185 -0
- package/p5-tests/unit/io/loadModel.js +215 -0
- package/p5-tests/unit/io/loadShader.js +176 -0
- package/p5-tests/unit/io/loadStrings.js +140 -0
- package/p5-tests/unit/io/loadTable.js +183 -0
- package/p5-tests/unit/io/loadXML.js +127 -0
- package/p5-tests/unit/io/saveModel.js +113 -0
- package/p5-tests/unit/io/saveTable.js +142 -0
- package/p5-tests/unit/math/calculation.js +452 -0
- package/p5-tests/unit/math/noise.js +66 -0
- package/p5-tests/unit/math/p5.Vector.js +1886 -0
- package/p5-tests/unit/math/random.js +177 -0
- package/p5-tests/unit/math/trigonometry.js +144 -0
- package/p5-tests/unit/spec.js +50 -0
- package/p5-tests/unit/typography/attributes.js +120 -0
- package/p5-tests/unit/typography/loadFont.js +162 -0
- package/p5-tests/unit/typography/p5.Font.js +63 -0
- package/p5-tests/unit/utilities/conversion.js +329 -0
- package/p5-tests/unit/utilities/time_date.js +133 -0
- package/package.json +1 -1
- package/q5.js +158 -54
- package/q5.min.js +1 -1
- package/src/q5-2d-canvas.js +8 -2
- package/src/q5-2d-drawing.js +20 -7
- package/src/q5-2d-image.js +4 -1
- package/src/q5-2d-text.js +7 -0
- package/src/q5-canvas.js +6 -5
- package/src/q5-color.js +5 -0
- package/src/q5-core.js +3 -1
- package/src/q5-input.js +12 -0
- package/src/q5-math.js +11 -3
- package/src/q5-record.js +2 -0
- package/src/q5-vector.js +33 -0
- package/src/q5-webgpu-canvas.js +11 -7
- package/src/q5-webgpu-drawing.js +15 -12
- package/src/q5-webgpu-image.js +1 -1
- package/src/q5-webgpu-text.js +22 -15
package/src/q5-webgpu-text.js
CHANGED
|
@@ -114,6 +114,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
114
114
|
mipmapFilter: 'linear',
|
|
115
115
|
maxAnisotropy: 16
|
|
116
116
|
});
|
|
117
|
+
|
|
117
118
|
let fontBindGroupLayout = Q5.device.createBindGroupLayout({
|
|
118
119
|
label: 'MSDF font group layout',
|
|
119
120
|
entries: [
|
|
@@ -151,6 +152,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
151
152
|
primitive: { topology: 'triangle-strip', stripIndexFormat: 'uint32' },
|
|
152
153
|
multisample: { count: 4 }
|
|
153
154
|
};
|
|
155
|
+
|
|
154
156
|
$._pipelines[2] = Q5.device.createRenderPipeline($._pipelineConfigs[2]);
|
|
155
157
|
|
|
156
158
|
class MsdfFont {
|
|
@@ -166,8 +168,9 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
166
168
|
getChar(charCode) {
|
|
167
169
|
return this.chars[charCode] ?? this.defaultChar;
|
|
168
170
|
}
|
|
169
|
-
// Gets the distance in pixels a line should advance for a given
|
|
170
|
-
// character code
|
|
171
|
+
// Gets the distance in pixels a line should advance for a given
|
|
172
|
+
// character code. If the upcoming character code is given any
|
|
173
|
+
// kerning between the two characters will be taken into account.
|
|
171
174
|
getXAdvance(charCode, nextCharCode = -1) {
|
|
172
175
|
let char = this.getChar(charCode);
|
|
173
176
|
if (nextCharCode >= 0) {
|
|
@@ -209,8 +212,8 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
209
212
|
});
|
|
210
213
|
Q5.device.queue.copyExternalImageToTexture({ source: img }, { texture }, imgSize);
|
|
211
214
|
|
|
212
|
-
//
|
|
213
|
-
//
|
|
215
|
+
// chars and kernings can be stored as csv strings, making the file
|
|
216
|
+
// size smaller, but they need to be parsed into arrays of objects
|
|
214
217
|
if (typeof atlas.chars == 'string') {
|
|
215
218
|
atlas.chars = $.CSV.parse(atlas.chars, ' ');
|
|
216
219
|
atlas.kernings = $.CSV.parse(atlas.kernings, ' ');
|
|
@@ -299,6 +302,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
299
302
|
$.textFont = (fontName) => {
|
|
300
303
|
$._font = fonts[fontName];
|
|
301
304
|
};
|
|
305
|
+
|
|
302
306
|
$.textSize = (size) => {
|
|
303
307
|
$._textSize = size;
|
|
304
308
|
if (!leadingSet) {
|
|
@@ -306,12 +310,14 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
306
310
|
leadDiff = leading - size;
|
|
307
311
|
}
|
|
308
312
|
};
|
|
313
|
+
|
|
309
314
|
$.textLeading = (lineHeight) => {
|
|
310
315
|
$._font.lineHeight = leading = lineHeight;
|
|
311
316
|
leadDiff = leading - $._textSize;
|
|
312
317
|
leadPercent = leading / $._textSize;
|
|
313
318
|
leadingSet = true;
|
|
314
319
|
};
|
|
320
|
+
|
|
315
321
|
$.textAlign = (horiz, vert) => {
|
|
316
322
|
$._textAlign = horiz;
|
|
317
323
|
if (vert) $._textBaseline = vert;
|
|
@@ -333,7 +339,7 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
333
339
|
let charCode = nextCharCode;
|
|
334
340
|
nextCharCode = i < text.length - 1 ? text.charCodeAt(i + 1) : -1;
|
|
335
341
|
switch (charCode) {
|
|
336
|
-
case 10: //
|
|
342
|
+
case 10: // newline
|
|
337
343
|
lineWidths.push(offsetX);
|
|
338
344
|
line++;
|
|
339
345
|
maxWidth = Math.max(maxWidth, offsetX);
|
|
@@ -342,11 +348,11 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
342
348
|
break;
|
|
343
349
|
case 13: // CR
|
|
344
350
|
break;
|
|
345
|
-
case 32: //
|
|
351
|
+
case 32: // space
|
|
346
352
|
// advance the offset without actually adding a character
|
|
347
353
|
offsetX += font.getXAdvance(charCode);
|
|
348
354
|
break;
|
|
349
|
-
case 9: //
|
|
355
|
+
case 9: // tab
|
|
350
356
|
offsetX += font.getXAdvance(charCode) * 2;
|
|
351
357
|
break;
|
|
352
358
|
default:
|
|
@@ -371,7 +377,8 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
371
377
|
|
|
372
378
|
$.text = (str, x, y, w, h) => {
|
|
373
379
|
if (!$._font) {
|
|
374
|
-
// check if online and loading the default font
|
|
380
|
+
// check if online and loading the default font
|
|
381
|
+
// hasn't been attempted yet
|
|
375
382
|
if (navigator.onLine && !initLoadDefaultFont) {
|
|
376
383
|
initLoadDefaultFont = true;
|
|
377
384
|
$.loadFont('https://q5js.org/fonts/YaHei-msdf.json');
|
|
@@ -530,27 +537,27 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
530
537
|
$._hooks.preRender.push(() => {
|
|
531
538
|
if (!$._charStack.length) return;
|
|
532
539
|
|
|
533
|
-
//
|
|
540
|
+
// calculate total buffer size for text data
|
|
534
541
|
let totalTextSize = 0;
|
|
535
542
|
for (let charsData of $._charStack) {
|
|
536
543
|
totalTextSize += charsData.length * 4;
|
|
537
544
|
}
|
|
538
545
|
|
|
539
|
-
//
|
|
546
|
+
// create a single buffer for all the char data
|
|
540
547
|
let charBuffer = Q5.device.createBuffer({
|
|
541
548
|
size: totalTextSize,
|
|
542
549
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
543
550
|
mappedAtCreation: true
|
|
544
551
|
});
|
|
545
552
|
|
|
546
|
-
//
|
|
553
|
+
// copy all the text data into the buffer
|
|
547
554
|
new Float32Array(charBuffer.getMappedRange()).set($._charStack.flat());
|
|
548
555
|
charBuffer.unmap();
|
|
549
556
|
|
|
550
|
-
//
|
|
557
|
+
// calculate total buffer size for metadata
|
|
551
558
|
let totalMetadataSize = $._textStack.length * 6 * 4;
|
|
552
559
|
|
|
553
|
-
//
|
|
560
|
+
// create a single buffer for all metadata
|
|
554
561
|
let textBuffer = Q5.device.createBuffer({
|
|
555
562
|
label: 'textBuffer',
|
|
556
563
|
size: totalMetadataSize,
|
|
@@ -558,11 +565,11 @@ fn fragmentMain(input : VertexOutput) -> @location(0) vec4f {
|
|
|
558
565
|
mappedAtCreation: true
|
|
559
566
|
});
|
|
560
567
|
|
|
561
|
-
//
|
|
568
|
+
// copy all metadata into the buffer
|
|
562
569
|
new Float32Array(textBuffer.getMappedRange()).set($._textStack.flat());
|
|
563
570
|
textBuffer.unmap();
|
|
564
571
|
|
|
565
|
-
//
|
|
572
|
+
// create a single bind group for the text buffer and metadata buffer
|
|
566
573
|
$._textBindGroup = Q5.device.createBindGroup({
|
|
567
574
|
label: 'msdf text bind group',
|
|
568
575
|
layout: textBindGroupLayout,
|