q5 2.13.3 → 2.13.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.
@@ -159,12 +159,12 @@ Q5.renderers.webgpu.canvas = ($, q) => {
159
159
 
160
160
  const MAX_TRANSFORMS = 1e7, // or whatever maximum you need
161
161
  MATRIX_SIZE = 16, // 4x4 matrix
162
- transforms = new Float32Array(MAX_TRANSFORMS * MATRIX_SIZE),
162
+ transforms = new Float32Array(MAX_TRANSFORMS * MATRIX_SIZE);
163
+
164
+ let matrix,
163
165
  matrices = [],
164
166
  matricesIndexStack = [];
165
167
 
166
- let matrix;
167
-
168
168
  // tracks if the matrix has been modified
169
169
  $._matrixDirty = false;
170
170
 
@@ -226,6 +226,8 @@ Q5.renderers.webgpu.canvas = ($, q) => {
226
226
  $.scale = (x = 1, y, z = 1) => {
227
227
  y ??= x;
228
228
 
229
+ $._scale = Math.max(Math.abs(x), Math.abs(y));
230
+
229
231
  let m = matrix;
230
232
 
231
233
  m[0] *= x;
@@ -459,7 +461,7 @@ Q5.renderers.webgpu.canvas = ($, q) => {
459
461
  new Float32Array(colorsBuffer.getMappedRange()).set(colorStack.slice(0, colorStackIndex));
460
462
  colorsBuffer.unmap();
461
463
 
462
- mainBindGroup = Q5.device.createBindGroup({
464
+ let mainBindGroup = Q5.device.createBindGroup({
463
465
  layout: mainLayout,
464
466
  entries: [
465
467
  { binding: 0, resource: { buffer: uniformBuffer } },
@@ -508,8 +510,6 @@ Q5.renderers.webgpu.canvas = ($, q) => {
508
510
  i++;
509
511
  }
510
512
  }
511
-
512
- for (let m of $._hooks.postRender) m();
513
513
  };
514
514
 
515
515
  $._finishRender = () => {
@@ -520,19 +520,19 @@ Q5.renderers.webgpu.canvas = ($, q) => {
520
520
  q.pass = $.encoder = null;
521
521
 
522
522
  // clear the stacks for the next frame
523
- $.drawStack.length = 0;
523
+ $.drawStack = drawStack = [];
524
524
  colorIndex = 1;
525
525
  colorStackIndex = 8;
526
- rotation = 0;
527
- transforms.length = MATRIX_SIZE;
528
- matrices.length = 1;
529
- matricesIndexStack.length = 0;
526
+ matrices = [matrices[0]];
527
+ matricesIndexStack = [];
528
+
529
+ for (let m of $._hooks.postRender) m();
530
530
  };
531
531
  };
532
532
 
533
533
  Q5.initWebGPU = async () => {
534
534
  if (!navigator.gpu) {
535
- console.warn('q5 WebGPU not supported on this browser!');
535
+ console.warn('q5 WebGPU not supported on this browser! Use Google Chrome or Edge.');
536
536
  return false;
537
537
  }
538
538
  if (!Q5.device) {
@@ -271,9 +271,9 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
271
271
  $.ellipseMode = (x) => ($._ellipseMode = x);
272
272
 
273
273
  $.ellipse = (x, y, w, h) => {
274
- let n = getArcSegments(Math.max(w, h));
275
- let a = Math.max(w, 1) / 2;
276
- let b = w == h ? a : Math.max(h, 1) / 2;
274
+ let n = getArcSegments(Math.max(Math.abs(w), Math.abs(h)) * $._scale);
275
+ let a = w / 2;
276
+ let b = w == h ? a : h / 2;
277
277
 
278
278
  if ($._matrixDirty) $._saveMatrix();
279
279
  let ti = $._matrixIndex;
@@ -306,8 +306,10 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
306
306
  }
307
307
  };
308
308
 
309
- $.stokeJoin = (x) => {
310
- $.log("q5 WebGPU doesn't support changing stroke join style.");
309
+ $._strokeJoin = 'round';
310
+
311
+ $.strokeJoin = (x) => {
312
+ $._strokeJoin = x;
311
313
  };
312
314
 
313
315
  $.line = (x1, y1, x2, y2) => {
@@ -328,7 +330,7 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
328
330
 
329
331
  addRect(x1 + px, -y1 - py, x1 - px, -y1 + py, x2 - px, -y2 + py, x2 + px, -y2 - py, ci, ti);
330
332
 
331
- if (sw > 2) {
333
+ if (sw > 2 && $._strokeJoin != 'none') {
332
334
  let n = getArcSegments(sw);
333
335
  addEllipse(x1, y1, hsw, hsw, n, ci, ti);
334
336
  addEllipse(x2, y2, hsw, hsw, n, ci, ti);
@@ -419,17 +421,26 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
419
421
  }
420
422
 
421
423
  if ($._doFill) {
422
- // triangulate the shape
423
- for (let i = 1; i < shapeVertCount - 1; i++) {
424
- let v0 = 0;
425
- let v1 = i * 4;
426
- let v2 = (i + 1) * 4;
427
-
428
- addVert(sv[v0], sv[v0 + 1], sv[v0 + 2], sv[v0 + 3]);
429
- addVert(sv[v1], sv[v1 + 1], sv[v1 + 2], sv[v1 + 3]);
430
- addVert(sv[v2], sv[v2 + 1], sv[v2 + 2], sv[v2 + 3]);
424
+ if (shapeVertCount == 5) {
425
+ // for quads, draw two triangles
426
+ addVert(sv[0], sv[1], sv[2], sv[3]); // v0
427
+ addVert(sv[4], sv[5], sv[6], sv[7]); // v1
428
+ addVert(sv[12], sv[13], sv[14], sv[15]); // v3
429
+ addVert(sv[8], sv[9], sv[10], sv[11]); // v2
430
+ drawStack.push(0, 4);
431
+ } else {
432
+ // triangulate the shape
433
+ for (let i = 1; i < shapeVertCount - 1; i++) {
434
+ let v0 = 0;
435
+ let v1 = i * 4;
436
+ let v2 = (i + 1) * 4;
437
+
438
+ addVert(sv[v0], sv[v0 + 1], sv[v0 + 2], sv[v0 + 3]);
439
+ addVert(sv[v1], sv[v1 + 1], sv[v1 + 2], sv[v1 + 3]);
440
+ addVert(sv[v2], sv[v2 + 1], sv[v2 + 2], sv[v2 + 3]);
441
+ }
442
+ drawStack.push(0, (shapeVertCount - 2) * 3);
431
443
  }
432
- drawStack.push(0, (shapeVertCount - 2) * 3);
433
444
  }
434
445
 
435
446
  if ($._doStroke) {
@@ -470,7 +481,7 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
470
481
  };
471
482
 
472
483
  $.background = (r, g, b, a) => {
473
- $.push();
484
+ $.pushMatrix();
474
485
  $.resetMatrix();
475
486
  $._doStroke = false;
476
487
  if (r.src) {
@@ -485,7 +496,7 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
485
496
  $.rect(-c.hw, -c.hh, c.w, c.h);
486
497
  $._rectMode = og;
487
498
  }
488
- $.pop();
499
+ $.popMatrix();
489
500
  if (!$._fillSet) $._fill = 1;
490
501
  };
491
502
 
@@ -505,6 +516,7 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
505
516
  });
506
517
 
507
518
  $._hooks.postRender.push(() => {
519
+ drawStack = $.drawStack;
508
520
  vertIndex = 0;
509
521
  });
510
522
  };
@@ -373,11 +373,19 @@ fn fragmentMain(f : FragmentParams) -> @location(0) vec4f {
373
373
 
374
374
  $.text = (str, x, y, w, h) => {
375
375
  if (!$._font) {
376
- // check if online and loading the default font
377
- // hasn't been attempted yet
378
- if (navigator.onLine && !initLoadDefaultFont) {
376
+ // check if loading the default font hasn't been attempted
377
+ if (!initLoadDefaultFont) {
379
378
  initLoadDefaultFont = true;
380
- $.loadFont('https://q5js.org/fonts/YaHei-msdf.json');
379
+
380
+ if (navigator.onLine) {
381
+ $.loadFont('https://q5js.org/defaultFont-msdf.json');
382
+ } else {
383
+ $.loadFont('/node_modules/q5/defaultFont-msdf.json');
384
+ }
385
+ // else if (Q5._esm && import.meta.url) {
386
+ // let path = new URL('defaultFont-msdf.json', import.meta.url);
387
+ // $.loadFont(path.href);
388
+ // }
381
389
  }
382
390
  return;
383
391
  }
@@ -577,7 +585,7 @@ fn fragmentMain(f : FragmentParams) -> @location(0) vec4f {
577
585
  });
578
586
 
579
587
  $._hooks.postRender.push(() => {
580
- charStack.length = 0;
581
- textStack.length = 0;
588
+ charStack = [];
589
+ textStack = [];
582
590
  });
583
591
  };