@holtjs/stage 0.5.1 → 0.6.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.
@@ -1,9 +1,12 @@
1
- import { _ as rgbToCss, a as EventEmitter, g as parseColor, h as packColorAbgr, l as Scenes, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, u as TintModes } from "./types-DRFB0Klc.js";
2
- import { _ as TileSpriteObject, a as Container, d as InstancedBatchObject, f as MsdfIconObject, g as TextObject, h as RectangleObject, i as BlitterObject, l as Graphics, m as PanelObject, o as CustomPassObject, p as NineSliceObject, r as Renderer2D, s as DisplayList, t as RendererWebGpu, u as ImageObject } from "./renderer-webgpu-BE6fTBzo.js";
1
+ import { _ as rgbToCss, a as EventEmitter, c as Scale, h as packColorAbgr, l as Scenes, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, u as TintModes } from "./types-CXsCvp1p.js";
2
+ import { _ as TextObject, a as Container, d as InstancedBatchObject, f as MsdfIconObject, g as ShapeBatchObject, h as RectangleObject, i as BlitterObject, l as Graphics, m as PanelObject, o as CustomPassObject, p as NineSliceObject, r as Renderer2D, s as DisplayList, t as RendererWebGpu, u as ImageObject, v as TileSpriteObject, y as Camera } from "./renderer-webgpu-DJVQHR70.js";
3
3
  import { TweenManager } from "./tween.js";
4
4
  import { StageKernel } from "./kernel.js";
5
5
  // src/texture.ts
6
- var TextureSource = class {
6
+ var TextureSource = class TextureSource {
7
+ static nextId = 1;
8
+ id = TextureSource.nextId++;
9
+ version = 0;
7
10
  texture;
8
11
  image;
9
12
  width;
@@ -47,6 +50,7 @@ var TextureSource = class {
47
50
  update() {
48
51
  this.width = sourceWidth(this.image);
49
52
  this.height = sourceHeight(this.image);
53
+ this.version++;
50
54
  this.dirty = true;
51
55
  if (this.host?.gl && !this.host.contextLost && this.width > 0 && this.height > 0) this.host.uploadTextureSource(this);
52
56
  }
@@ -280,264 +284,7 @@ var TextureManager = class {
280
284
  this.host = null;
281
285
  }
282
286
  };
283
- // src/camera.ts
284
- var Camera = class {
285
- x = 0;
286
- y = 0;
287
- width;
288
- height;
289
- scrollX = 0;
290
- scrollY = 0;
291
- zoom = 1;
292
- alpha = 1;
293
- originX = .5;
294
- originY = .5;
295
- roundPixels = false;
296
- useBounds = false;
297
- boundsX = 0;
298
- boundsY = 0;
299
- boundsWidth = 0;
300
- boundsHeight = 0;
301
- backgroundColor;
302
- transparent;
303
- worldView = {
304
- x: 0,
305
- y: 0,
306
- width: 0,
307
- height: 0,
308
- right: 0,
309
- bottom: 0
310
- };
311
- matrixCombined = createMatrix2D();
312
- followTarget = null;
313
- followLerpX = 1;
314
- followLerpY = 1;
315
- followOffsetX = 0;
316
- followOffsetY = 0;
317
- deadzoneW = 0;
318
- deadzoneH = 0;
319
- constructor(width, height, transparent = false, backgroundColor) {
320
- this.width = Math.max(1, width);
321
- this.height = Math.max(1, height);
322
- this.transparent = transparent;
323
- this.backgroundColor = backgroundColor !== void 0 ? parseColor(backgroundColor, transparent ? 0 : 1) : {
324
- r: 0,
325
- g: 0,
326
- b: 0,
327
- a: transparent ? 0 : 1
328
- };
329
- this.syncMetrics();
330
- }
331
- get displayWidth() {
332
- return this.width / (this.zoom || 1);
333
- }
334
- get displayHeight() {
335
- return this.height / (this.zoom || 1);
336
- }
337
- setBackgroundColor(color) {
338
- this.backgroundColor = parseColor(color, 1);
339
- this.transparent = this.backgroundColor.a <= 0;
340
- return this;
341
- }
342
- setRoundPixels(v) {
343
- this.roundPixels = !!v;
344
- return this;
345
- }
346
- setOrigin(x = .5, y = x) {
347
- this.originX = x;
348
- this.originY = y;
349
- this.syncMetrics();
350
- return this;
351
- }
352
- setZoom(z) {
353
- this.zoom = z > 0 ? z : 1;
354
- if (this.useBounds) {
355
- this.scrollX = this.clampX(this.scrollX);
356
- this.scrollY = this.clampY(this.scrollY);
357
- }
358
- this.syncMetrics();
359
- return this;
360
- }
361
- setScroll(x, y) {
362
- this.scrollX = this.useBounds ? this.clampX(x) : x;
363
- this.scrollY = this.useBounds ? this.clampY(y) : y;
364
- this.syncMetrics();
365
- return this;
366
- }
367
- setViewport(x, y, width, height) {
368
- this.x = x;
369
- this.y = y;
370
- this.width = Math.max(1, width);
371
- this.height = Math.max(1, height);
372
- if (this.useBounds) {
373
- this.scrollX = this.clampX(this.scrollX);
374
- this.scrollY = this.clampY(this.scrollY);
375
- }
376
- this.syncMetrics();
377
- return this;
378
- }
379
- setSize(width, height) {
380
- this.width = Math.max(1, width);
381
- this.height = Math.max(1, height);
382
- if (this.useBounds) {
383
- this.scrollX = this.clampX(this.scrollX);
384
- this.scrollY = this.clampY(this.scrollY);
385
- }
386
- this.syncMetrics();
387
- return this;
388
- }
389
- setAlpha(a) {
390
- this.alpha = Math.max(0, Math.min(1, a));
391
- return this;
392
- }
393
- setBounds(x, y, width, height) {
394
- this.useBounds = true;
395
- this.boundsX = x;
396
- this.boundsY = y;
397
- this.boundsWidth = width;
398
- this.boundsHeight = height;
399
- this.scrollX = this.clampX(this.scrollX);
400
- this.scrollY = this.clampY(this.scrollY);
401
- this.syncMetrics();
402
- return this;
403
- }
404
- clampX(sx) {
405
- if (!this.useBounds) return sx;
406
- const ox = this.width * this.originX;
407
- const dw = this.displayWidth;
408
- if (dw >= this.boundsWidth) return this.boundsX + this.boundsWidth * .5 - ox;
409
- const minSx = this.boundsX - ox + ox / this.zoom;
410
- const maxSx = this.boundsX + this.boundsWidth - dw - ox + ox / this.zoom;
411
- return sx < minSx ? minSx : sx > maxSx ? maxSx : sx;
412
- }
413
- clampY(sy) {
414
- if (!this.useBounds) return sy;
415
- const oy = this.height * this.originY;
416
- const dh = this.displayHeight;
417
- if (dh >= this.boundsHeight) return this.boundsY + this.boundsHeight * .5 - oy;
418
- const minSy = this.boundsY - oy + oy / this.zoom;
419
- const maxSy = this.boundsY + this.boundsHeight - dh - oy + oy / this.zoom;
420
- return sy < minSy ? minSy : sy > maxSy ? maxSy : sy;
421
- }
422
- centerOn(x, y) {
423
- const ox = this.width * this.originX;
424
- const oy = this.height * this.originY;
425
- return this.setScroll(x - ox, y - oy);
426
- }
427
- /**
428
- * One-line HiDPI camera + backing-buffer configurator: maps logical `(0, 0)..(viewportW, viewportH)`
429
- * 1:1 onto crisp physical `viewportW * dpr` pixels without copy-pasting origin/scroll math.
430
- * Returns the clamped `dpr` applied.
431
- */
432
- configureHiDpi(scale, viewportWidth, viewportHeight, dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1, maxDpr = 2) {
433
- const safeDpr = Math.min(Math.max(1, maxDpr), Math.max(1, Number.isFinite(dpr) ? dpr : 1));
434
- const vw = Math.max(1, Math.round(viewportWidth));
435
- const vh = Math.max(1, Math.round(viewportHeight));
436
- scale.zoom = 1 / safeDpr;
437
- scale.resize(Math.round(vw * safeDpr), Math.round(vh * safeDpr));
438
- if (scale.canvas?.style) {
439
- scale.canvas.style.width = `${vw}px`;
440
- scale.canvas.style.height = `${vh}px`;
441
- }
442
- const ox = this.width * this.originX;
443
- const oy = this.height * this.originY;
444
- this.setZoom(safeDpr);
445
- this.setScroll(ox / safeDpr - ox, oy / safeDpr - oy);
446
- return safeDpr;
447
- }
448
- /** Convert canvas/backing-buffer coordinates `(screenX, screenY)` into world coordinates. */
449
- screenToWorld(screenX, screenY, out = {
450
- x: 0,
451
- y: 0
452
- }) {
453
- const m = this.matrixCombined;
454
- out.x = (screenX - m.tx) / (m.a || 1);
455
- out.y = (screenY - m.ty) / (m.d || 1);
456
- return out;
457
- }
458
- /** Convert world coordinates `(worldX, worldY)` into canvas/backing-buffer coordinates. */
459
- worldToScreen(worldX, worldY, out = {
460
- x: 0,
461
- y: 0
462
- }) {
463
- const m = this.matrixCombined;
464
- out.x = worldX * m.a + m.tx;
465
- out.y = worldY * m.d + m.ty;
466
- return out;
467
- }
468
- /** Lock or smoothly follow any `{ x, y }` actor/point (frame-rate independent lerp + optional deadzone). */
469
- startFollow(target, options = {}) {
470
- this.followTarget = target;
471
- this.followLerpX = Math.max(.001, Math.min(1, options.lerpX ?? 1));
472
- this.followLerpY = Math.max(.001, Math.min(1, options.lerpY ?? this.followLerpX));
473
- this.followOffsetX = options.offsetX ?? 0;
474
- this.followOffsetY = options.offsetY ?? 0;
475
- this.deadzoneW = Math.max(0, options.deadzoneWidth ?? 0);
476
- this.deadzoneH = Math.max(0, options.deadzoneHeight ?? 0);
477
- return this;
478
- }
479
- stopFollow() {
480
- this.followTarget = null;
481
- return this;
482
- }
483
- /** Internal per-step follow update invoked automatically by `Game.step()` before rendering. */
484
- stepFollow(deltaMs) {
485
- const target = this.followTarget;
486
- if (!target) return;
487
- const tx = target.x + this.followOffsetX;
488
- const ty = target.y + this.followOffsetY;
489
- const curCx = this.worldView.x + this.worldView.width * .5;
490
- const curCy = this.worldView.y + this.worldView.height * .5;
491
- let wantCx = curCx;
492
- let wantCy = curCy;
493
- const halfDzW = this.deadzoneW * .5;
494
- const halfDzH = this.deadzoneH * .5;
495
- if (tx < curCx - halfDzW) wantCx = tx + halfDzW;
496
- else if (tx > curCx + halfDzW) wantCx = tx - halfDzW;
497
- if (ty < curCy - halfDzH) wantCy = ty + halfDzH;
498
- else if (ty > curCy + halfDzH) wantCy = ty - halfDzH;
499
- const frames = Math.max(.1, deltaMs / (1e3 / 60));
500
- const ax = this.followLerpX >= 1 ? 1 : 1 - Math.pow(1 - this.followLerpX, frames);
501
- const ay = this.followLerpY >= 1 ? 1 : 1 - Math.pow(1 - this.followLerpY, frames);
502
- const nextCx = curCx + (wantCx - curCx) * ax;
503
- const nextCy = curCy + (wantCy - curCy) * ay;
504
- const ox = this.width * this.originX;
505
- const oy = this.height * this.originY;
506
- const z = this.zoom || 1;
507
- const nextSx = nextCx - this.worldView.width * .5 - ox + ox / z;
508
- const nextSy = nextCy - this.worldView.height * .5 - oy + oy / z;
509
- this.setScroll(nextSx, nextSy);
510
- }
511
- /** Recompute `worldView` and world->canvas `matrixCombined`. */
512
- syncMetrics() {
513
- const z = this.zoom || 1;
514
- const ox = this.width * this.originX;
515
- const oy = this.height * this.originY;
516
- const sx = this.roundPixels ? Math.round(this.scrollX) : this.scrollX;
517
- const sy = this.roundPixels ? Math.round(this.scrollY) : this.scrollY;
518
- const dw = this.width / z;
519
- const dh = this.height / z;
520
- const vx = sx + ox - ox / z;
521
- const vy = sy + oy - oy / z;
522
- this.worldView.x = vx;
523
- this.worldView.y = vy;
524
- this.worldView.width = dw;
525
- this.worldView.height = dh;
526
- this.worldView.right = vx + dw;
527
- this.worldView.bottom = vy + dh;
528
- const m = this.matrixCombined;
529
- m.a = z;
530
- m.b = 0;
531
- m.c = 0;
532
- m.d = z;
533
- m.tx = (-sx - ox) * z + ox + this.x;
534
- m.ty = (-sy - oy) * z + oy + this.y;
535
- m.scaleX = z;
536
- m.scaleY = z;
537
- }
538
- };
539
- // src/sdf-panel.ts
540
- const MAX_PANELS = 1024;
287
+ const MAX_PANELS = 4096;
541
288
  const MAX_VERTS$3 = MAX_PANELS * 4;
542
289
  const VERT_FLOATS$3 = 18;
543
290
  const VERT_STRIDE$3 = 72;
@@ -838,6 +585,100 @@ var SdfPanelBatcher = class {
838
585
  u32[off + 17] = glintAbgr;
839
586
  this.panelCount++;
840
587
  }
588
+ /**
589
+ * Fast-path SDF quad emitter for symmetric-radius shapes (`Graphics` circles/rounded-rects & `ShapeBatchObject`)
590
+ * with `pxScale` precomputed once per node/matrix. Caller ensures `pending < MAX_SDF_BATCH_QUADS` before calling.
591
+ */
592
+ pushSimple(m, pxScale, rx, ry, w, h, radius, fillAbgr, borderAbgr, borderWidth, pixelStep = 0) {
593
+ const maxR = Math.min(w, h) * .5;
594
+ const rad = radius <= 0 ? 0 : radius >= maxR ? maxR : radius;
595
+ const bw = borderWidth <= 0 ? 0 : borderWidth >= maxR ? maxR : borderWidth;
596
+ const step = pixelStep > 0 ? pixelStep : 0;
597
+ const pad = step > 0 ? 0 : 1 / pxScale;
598
+ const lx0 = rx - pad, ly0 = ry - pad;
599
+ const lx1 = rx + w + pad, ly1 = ry + h + pad;
600
+ const u0 = -pad, v0 = -pad, u1 = w + pad, v1 = h + pad;
601
+ const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
602
+ const f32 = this.vertF32;
603
+ const u32 = this.vertU32;
604
+ let off = this.panelCount * 4 * VERT_FLOATS$3;
605
+ f32[off] = a * lx0 + c * ly0 + tx;
606
+ f32[off + 1] = b * lx0 + d * ly0 + ty;
607
+ f32[off + 2] = u0;
608
+ f32[off + 3] = v0;
609
+ f32[off + 4] = w;
610
+ f32[off + 5] = h;
611
+ f32[off + 6] = 0;
612
+ f32[off + 7] = rad;
613
+ f32[off + 8] = rad;
614
+ f32[off + 9] = rad;
615
+ f32[off + 10] = rad;
616
+ f32[off + 11] = bw;
617
+ f32[off + 12] = step;
618
+ f32[off + 13] = pxScale;
619
+ f32[off + 14] = 0;
620
+ u32[off + 15] = fillAbgr;
621
+ u32[off + 16] = borderAbgr;
622
+ u32[off + 17] = 0;
623
+ off += VERT_FLOATS$3;
624
+ f32[off] = a * lx1 + c * ly0 + tx;
625
+ f32[off + 1] = b * lx1 + d * ly0 + ty;
626
+ f32[off + 2] = u1;
627
+ f32[off + 3] = v0;
628
+ f32[off + 4] = w;
629
+ f32[off + 5] = h;
630
+ f32[off + 6] = 0;
631
+ f32[off + 7] = rad;
632
+ f32[off + 8] = rad;
633
+ f32[off + 9] = rad;
634
+ f32[off + 10] = rad;
635
+ f32[off + 11] = bw;
636
+ f32[off + 12] = step;
637
+ f32[off + 13] = pxScale;
638
+ f32[off + 14] = 0;
639
+ u32[off + 15] = fillAbgr;
640
+ u32[off + 16] = borderAbgr;
641
+ u32[off + 17] = 0;
642
+ off += VERT_FLOATS$3;
643
+ f32[off] = a * lx1 + c * ly1 + tx;
644
+ f32[off + 1] = b * lx1 + d * ly1 + ty;
645
+ f32[off + 2] = u1;
646
+ f32[off + 3] = v1;
647
+ f32[off + 4] = w;
648
+ f32[off + 5] = h;
649
+ f32[off + 6] = 0;
650
+ f32[off + 7] = rad;
651
+ f32[off + 8] = rad;
652
+ f32[off + 9] = rad;
653
+ f32[off + 10] = rad;
654
+ f32[off + 11] = bw;
655
+ f32[off + 12] = step;
656
+ f32[off + 13] = pxScale;
657
+ f32[off + 14] = 0;
658
+ u32[off + 15] = fillAbgr;
659
+ u32[off + 16] = borderAbgr;
660
+ u32[off + 17] = 0;
661
+ off += VERT_FLOATS$3;
662
+ f32[off] = a * lx0 + c * ly1 + tx;
663
+ f32[off + 1] = b * lx0 + d * ly1 + ty;
664
+ f32[off + 2] = u0;
665
+ f32[off + 3] = v1;
666
+ f32[off + 4] = w;
667
+ f32[off + 5] = h;
668
+ f32[off + 6] = 0;
669
+ f32[off + 7] = rad;
670
+ f32[off + 8] = rad;
671
+ f32[off + 9] = rad;
672
+ f32[off + 10] = rad;
673
+ f32[off + 11] = bw;
674
+ f32[off + 12] = step;
675
+ f32[off + 13] = pxScale;
676
+ f32[off + 14] = 0;
677
+ u32[off + 15] = fillAbgr;
678
+ u32[off + 16] = borderAbgr;
679
+ u32[off + 17] = 0;
680
+ this.panelCount++;
681
+ }
841
682
  flush(resW, resH) {
842
683
  const count = this.panelCount;
843
684
  if (count === 0 || !this.program || !this.vbo || !this.quadIbo) return false;
@@ -1791,6 +1632,19 @@ function buildLineSeg(x0, y0, x1, y1, halfW, rgb, alpha) {
1791
1632
  const ny = dx / len * halfW;
1792
1633
  buildQuadPts(x0 + nx, y0 + ny, x1 + nx, y1 + ny, x1 - nx, y1 - ny, x0 - nx, y0 - ny, rgb, alpha);
1793
1634
  }
1635
+ const UNIT_COS = [];
1636
+ const UNIT_SIN = [];
1637
+ for (let segs = 6; segs <= 64; segs++) {
1638
+ const cs = new Float32Array(segs);
1639
+ const sn = new Float32Array(segs);
1640
+ const step = Math.PI * 2 / segs;
1641
+ for (let i = 0; i < segs; i++) {
1642
+ cs[i] = Math.cos(i * step);
1643
+ sn[i] = Math.sin(i * step);
1644
+ }
1645
+ UNIT_COS[segs] = cs;
1646
+ UNIT_SIN[segs] = sn;
1647
+ }
1794
1648
  function buildEllipseFill(cx, cy, rx, ry, scale, rgb, alpha) {
1795
1649
  if (rx <= 0 || ry <= 0) return;
1796
1650
  buildHasScaleCurves = true;
@@ -1807,13 +1661,11 @@ function buildEllipseFill(cx, cy, rx, ry, scale, rgb, alpha) {
1807
1661
  buildRgb[baseV] = rgb;
1808
1662
  buildAlphas[baseV] = alpha;
1809
1663
  buildAbgr1[baseV] = abgr;
1810
- const step = Math.PI * 2 / segs;
1664
+ const csLut = UNIT_COS[segs];
1665
+ const snLut = UNIT_SIN[segs];
1811
1666
  for (let i = 0; i < segs; i++) {
1812
- const ang = i * step;
1813
- const px = cx + Math.cos(ang) * rx;
1814
- const py = cy + Math.sin(ang) * ry;
1815
- buildXy[xyI++] = px;
1816
- buildXy[xyI++] = py;
1667
+ buildXy[xyI++] = cx + csLut[i] * rx;
1668
+ buildXy[xyI++] = cy + snLut[i] * ry;
1817
1669
  buildRgb[baseV + 1 + i] = rgb;
1818
1670
  buildAlphas[baseV + 1 + i] = alpha;
1819
1671
  buildAbgr1[baseV + 1 + i] = abgr;
@@ -1841,10 +1693,10 @@ function buildEllipseStroke(cx, cy, rx, ry, lw, scale, rgb, alpha) {
1841
1693
  const baseV = buildV;
1842
1694
  const abgr = packColorAbgr(rgb, alpha);
1843
1695
  let xyI = baseV * 2;
1844
- const step = Math.PI * 2 / segs;
1696
+ const csLut = UNIT_COS[segs];
1697
+ const snLut = UNIT_SIN[segs];
1845
1698
  for (let i = 0; i < segs; i++) {
1846
- const ang = i * step;
1847
- const cs = Math.cos(ang), sn = Math.sin(ang);
1699
+ const cs = csLut[i], sn = snLut[i];
1848
1700
  const vi = baseV + i * 2;
1849
1701
  buildXy[xyI++] = cx + cs * orx;
1850
1702
  buildXy[xyI++] = cy + sn * ory;
@@ -2330,6 +2182,7 @@ var RendererGl = class extends EventEmitter {
2330
2182
  maskWorldMat = createMatrix2D();
2331
2183
  bindTextSlot0;
2332
2184
  activeFbo = null;
2185
+ activeFboTex = null;
2333
2186
  instProgram = null;
2334
2187
  instUres = null;
2335
2188
  instVao = null;
@@ -2379,6 +2232,7 @@ var RendererGl = class extends EventEmitter {
2379
2232
  e.preventDefault();
2380
2233
  this.contextLost = true;
2381
2234
  this.activeFbo = null;
2235
+ this.activeFboTex = null;
2382
2236
  this.boundTexCache.fill(null);
2383
2237
  this.activeTexUnit = -1;
2384
2238
  this.batchStateBound = false;
@@ -2641,6 +2495,7 @@ var RendererGl = class extends EventEmitter {
2641
2495
  src.glHeight = src.height;
2642
2496
  src.glRepeat = canRepeat;
2643
2497
  src.glPremultiplyAlpha = src.premultiplyAlpha;
2498
+ this.bindTextureAt(0, this.dummyTex);
2644
2499
  } else if (!isNew && src.glWidth === src.width && src.glHeight === src.height && src.glRepeat === canRepeat && src.glPremultiplyAlpha === src.premultiplyAlpha) gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, src.image);
2645
2500
  else {
2646
2501
  const wrap = canRepeat ? gl.REPEAT : gl.CLAMP_TO_EDGE;
@@ -2707,12 +2562,13 @@ var RendererGl = class extends EventEmitter {
2707
2562
  this.batchStateBound = true;
2708
2563
  }
2709
2564
  }
2710
- /** Full state restore for external callers that may have mutated viewport/blend/textures/pixelStore/FBO directly. */
2565
+ /** Full state restore for external callers that may have mutated viewport/blend/cull/depth/stencil/colorMask/textures/pixelStore/FBO directly. */
2711
2566
  restoreState() {
2712
2567
  const gl = this.gl;
2713
2568
  this.invalidateProgramState();
2714
2569
  this.boundTexCache.fill(null);
2715
2570
  this.activeTexUnit = -1;
2571
+ if (this.activeFboTex) this.evictTextureFromUnits(this.activeFboTex);
2716
2572
  if (typeof gl.bindFramebuffer === "function") gl.bindFramebuffer(gl.FRAMEBUFFER, this.activeFbo);
2717
2573
  if (typeof gl.pixelStorei === "function") {
2718
2574
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
@@ -2722,10 +2578,21 @@ var RendererGl = class extends EventEmitter {
2722
2578
  if (gl2.UNPACK_ROW_LENGTH !== void 0) gl.pixelStorei(gl2.UNPACK_ROW_LENGTH, 0);
2723
2579
  }
2724
2580
  gl.viewport(0, 0, this.width, this.height);
2581
+ gl.disable(gl.DEPTH_TEST);
2582
+ if (gl.STENCIL_TEST !== void 0) gl.disable(gl.STENCIL_TEST);
2583
+ gl.disable(gl.CULL_FACE);
2584
+ if (typeof gl.depthMask === "function") gl.depthMask(true);
2585
+ if (typeof gl.frontFace === "function" && gl.CCW !== void 0) gl.frontFace(gl.CCW);
2586
+ if (typeof gl.colorMask === "function") gl.colorMask(true, true, true, true);
2725
2587
  gl.enable(gl.BLEND);
2588
+ if (typeof gl.blendEquation === "function" && gl.FUNC_ADD !== void 0) gl.blendEquation(gl.FUNC_ADD);
2726
2589
  this.ensureBatchState();
2727
2590
  this.applyCurrentScissor();
2728
2591
  }
2592
+ /** Evict `tex` (and populate any post-restore `null` slots with `dummyTex`) so binding its FBO never forms a GL feedback loop. */
2593
+ evictTextureFromUnits(tex) {
2594
+ for (let i = 0; i < this.boundTexCache.length; i++) if (this.boundTexCache[i] === tex || this.boundTexCache[i] === null) this.bindTextureAt(i, this.dummyTex);
2595
+ }
2729
2596
  setBlend(mode) {
2730
2597
  if (this.curBlend === mode) return;
2731
2598
  this.flushAll();
@@ -2770,11 +2637,7 @@ var RendererGl = class extends EventEmitter {
2770
2637
  const sdf = this.sdfBatcher;
2771
2638
  if (!sdf || sdf.pending === 0 || this.contextLost) return;
2772
2639
  const count = sdf.pending;
2773
- if (!this.batchStateBound) {
2774
- const gl = this.gl;
2775
- if (this.curBlend === BlendModes.ADD) gl.blendFunc(gl.ONE, gl.ONE);
2776
- else gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
2777
- }
2640
+ if (!this.batchStateBound) applyGlBlendMode(this.gl, this.curBlend);
2778
2641
  if (sdf.flush(this.width, this.height)) {
2779
2642
  this.curDrawCalls++;
2780
2643
  this.curFlushes++;
@@ -3277,6 +3140,10 @@ var RendererGl = class extends EventEmitter {
3277
3140
  }
3278
3141
  renderGraphics(g, m, alpha) {
3279
3142
  if (g.opLen === 0) return;
3143
+ if (g._hasSdfShapes && !g._hasNonSdfOps && this.sdfBatcher) {
3144
+ this.renderGraphicsSdf(g, m, alpha);
3145
+ return;
3146
+ }
3280
3147
  let cache = g._glCache;
3281
3148
  let scaleBucket = 2;
3282
3149
  if (!cache || cache.version !== g.version || cache.hasScaleCurves) {
@@ -3418,8 +3285,253 @@ var RendererGl = class extends EventEmitter {
3418
3285
  if (this.pixelAaBatcher?.pending) this.flushPixelAa();
3419
3286
  if (this.indexCount > 0) this.flush();
3420
3287
  this.setBlend(panel.blendMode);
3288
+ if (this.sdfBatcher && this.sdfBatcher.pending >= 4096) this.flushSdf();
3421
3289
  this.sdfBatcher?.push(m, ox, oy, w, h, panel.radiusTL, panel.radiusTR, panel.radiusBR, panel.radiusBL, panel.fillColor, fillAlpha, panel.borderColor, borderAlpha, borderW, panel.pixelStep, panel.openSide, panel.topGlintColor, panel.topGlintAlpha, panel.topGlintHeight, this.width, this.height);
3422
3290
  }
3291
+ renderGraphicsSdf(g, m, alpha) {
3292
+ const sdf = this.sdfBatcher;
3293
+ if (!sdf) return;
3294
+ if (this.activeCustomHandler) this.flushCustomHandler();
3295
+ if (this.msdfBatcher?.pending) this.flushMsdf();
3296
+ if (this.pixelAaBatcher?.pending) this.flushPixelAa();
3297
+ if (this.indexCount > 0) this.flush();
3298
+ this.setBlend(g.blendMode);
3299
+ const sx = Math.hypot(m.a, m.b);
3300
+ const sy = Math.hypot(m.c, m.d);
3301
+ const pxScale = Math.max(.001, Math.min(sx, sy));
3302
+ gfxStateStack.length = 0;
3303
+ let fillRgb = WHITE_RGB, fillA = 1;
3304
+ let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
3305
+ let cachedFillRgb = -1, cachedFillA = -1, fillAbgr = 0;
3306
+ let cachedLineRgb = -1, cachedLineA = -1, lineAbgr = 0;
3307
+ const getFillAbgr = () => {
3308
+ const eff = fillA * alpha;
3309
+ if (fillRgb !== cachedFillRgb || eff !== cachedFillA) {
3310
+ cachedFillRgb = fillRgb;
3311
+ cachedFillA = eff;
3312
+ fillAbgr = packColorAbgr(fillRgb, eff);
3313
+ }
3314
+ return fillAbgr;
3315
+ };
3316
+ const getLineAbgr = () => {
3317
+ const eff = lineA * alpha;
3318
+ if (lineRgb !== cachedLineRgb || eff !== cachedLineA) {
3319
+ cachedLineRgb = lineRgb;
3320
+ cachedLineA = eff;
3321
+ lineAbgr = packColorAbgr(lineRgb, eff);
3322
+ }
3323
+ return lineAbgr;
3324
+ };
3325
+ const ops = g.ops;
3326
+ const nums = g.nums;
3327
+ let ni = 0;
3328
+ for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
3329
+ case 22:
3330
+ gfxStateStack.push(fillRgb, fillA, lineW, lineRgb, lineA);
3331
+ break;
3332
+ case 23:
3333
+ if (gfxStateStack.length >= 5) {
3334
+ lineA = gfxStateStack.pop();
3335
+ lineRgb = gfxStateStack.pop();
3336
+ lineW = gfxStateStack.pop();
3337
+ fillA = gfxStateStack.pop();
3338
+ fillRgb = gfxStateStack.pop();
3339
+ }
3340
+ break;
3341
+ case 1:
3342
+ fillRgb = nums[ni++];
3343
+ fillA = nums[ni++];
3344
+ break;
3345
+ case 2:
3346
+ lineW = nums[ni++];
3347
+ lineRgb = nums[ni++];
3348
+ lineA = nums[ni++];
3349
+ break;
3350
+ case 3: {
3351
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
3352
+ if (fillA * alpha > 0 && w !== 0 && h !== 0) {
3353
+ const rx = w < 0 ? x + w : x, ry = h < 0 ? y + h : y;
3354
+ const rw = Math.abs(w), rh = Math.abs(h);
3355
+ if (!this.isOffscreen(m, rx, ry, rx + rw, ry + rh)) {
3356
+ if (sdf.pending >= 4096) this.flushSdf();
3357
+ sdf.pushSimple(m, pxScale, rx, ry, rw, rh, 0, getFillAbgr(), 0, 0);
3358
+ } else this.curCulled++;
3359
+ }
3360
+ break;
3361
+ }
3362
+ case 4: {
3363
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
3364
+ if (lineA * alpha > 0 && lineW > 0 && w !== 0 && h !== 0) {
3365
+ const hw = lineW * .5;
3366
+ const rx = (w < 0 ? x + w : x) - hw, ry = (h < 0 ? y + h : y) - hw;
3367
+ const rw = Math.abs(w) + lineW, rh = Math.abs(h) + lineW;
3368
+ if (!this.isOffscreen(m, rx, ry, rx + rw, ry + rh)) {
3369
+ if (sdf.pending >= 4096) this.flushSdf();
3370
+ sdf.pushSimple(m, pxScale, rx, ry, rw, rh, 0, 0, getLineAbgr(), lineW);
3371
+ } else this.curCulled++;
3372
+ }
3373
+ break;
3374
+ }
3375
+ case 5: {
3376
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
3377
+ if (fillA * alpha > 0 && w > 0 && h > 0) {
3378
+ if (!this.isOffscreen(m, x, y, x + w, y + h)) {
3379
+ if (sdf.pending >= 4096) this.flushSdf();
3380
+ sdf.pushSimple(m, pxScale, x, y, w, h, r, getFillAbgr(), 0, 0);
3381
+ } else this.curCulled++;
3382
+ }
3383
+ break;
3384
+ }
3385
+ case 6: {
3386
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
3387
+ if (lineA * alpha > 0 && lineW > 0 && w > 0 && h > 0) {
3388
+ const hw = lineW * .5;
3389
+ const rx = x - hw, ry = y - hw, rw = w + lineW, rh = h + lineW;
3390
+ if (!this.isOffscreen(m, rx, ry, rx + rw, ry + rh)) {
3391
+ if (sdf.pending >= 4096) this.flushSdf();
3392
+ sdf.pushSimple(m, pxScale, rx, ry, rw, rh, Math.max(0, r) + hw, 0, getLineAbgr(), lineW);
3393
+ } else this.curCulled++;
3394
+ }
3395
+ break;
3396
+ }
3397
+ case 7: {
3398
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
3399
+ if (fillA * alpha > 0 && r > 0) {
3400
+ const d = r * 2, rx = cx - r, ry = cy - r;
3401
+ if (!this.isOffscreen(m, rx, ry, rx + d, ry + d)) {
3402
+ if (sdf.pending >= 4096) this.flushSdf();
3403
+ sdf.pushSimple(m, pxScale, rx, ry, d, d, r, getFillAbgr(), 0, 0);
3404
+ } else this.curCulled++;
3405
+ }
3406
+ break;
3407
+ }
3408
+ case 8: {
3409
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
3410
+ if (lineA * alpha > 0 && lineW > 0 && r > 0) {
3411
+ const ro = r + lineW * .5, d = ro * 2, rx = cx - ro, ry = cy - ro;
3412
+ if (!this.isOffscreen(m, rx, ry, rx + d, ry + d)) {
3413
+ if (sdf.pending >= 4096) this.flushSdf();
3414
+ sdf.pushSimple(m, pxScale, rx, ry, d, d, ro, 0, getLineAbgr(), lineW);
3415
+ } else this.curCulled++;
3416
+ }
3417
+ break;
3418
+ }
3419
+ case 9: {
3420
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
3421
+ if (fillA * alpha > 0 && w > 0 && h > 0) {
3422
+ const rx = cx - w * .5, ry = cy - h * .5;
3423
+ if (!this.isOffscreen(m, rx, ry, rx + w, ry + h)) {
3424
+ if (sdf.pending >= 4096) this.flushSdf();
3425
+ sdf.pushSimple(m, pxScale, rx, ry, w, h, w * .5, getFillAbgr(), 0, 0);
3426
+ } else this.curCulled++;
3427
+ }
3428
+ break;
3429
+ }
3430
+ case 10: {
3431
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
3432
+ if (lineA * alpha > 0 && lineW > 0 && w > 0 && h > 0) {
3433
+ const rw = w + lineW, rh = h + lineW, rx = cx - rw * .5, ry = cy - rh * .5;
3434
+ if (!this.isOffscreen(m, rx, ry, rx + rw, ry + rh)) {
3435
+ if (sdf.pending >= 4096) this.flushSdf();
3436
+ sdf.pushSimple(m, pxScale, rx, ry, rw, rh, rw * .5, 0, getLineAbgr(), lineW);
3437
+ } else this.curCulled++;
3438
+ }
3439
+ break;
3440
+ }
3441
+ }
3442
+ }
3443
+ renderShapeBatch(batch, m, alpha) {
3444
+ const sdf = this.sdfBatcher;
3445
+ const cols = batch.columns;
3446
+ if (!sdf || !cols || cols.count <= 0) return;
3447
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
3448
+ if (n <= 0) return;
3449
+ if (this.activeCustomHandler) this.flushCustomHandler();
3450
+ if (this.msdfBatcher?.pending) this.flushMsdf();
3451
+ if (this.pixelAaBatcher?.pending) this.flushPixelAa();
3452
+ if (this.indexCount > 0) this.flush();
3453
+ this.setBlend(batch.blendMode);
3454
+ const sx = Math.hypot(m.a, m.b);
3455
+ const sy = Math.hypot(m.c, m.d);
3456
+ const basePxScale = Math.max(.001, Math.min(sx, sy));
3457
+ const oxRatio = batch.originX;
3458
+ const oyRatio = batch.originY;
3459
+ const step = batch.pixelStep;
3460
+ const wCol = typeof cols.width === "object" && cols.width ? cols.width : null;
3461
+ const wDef = typeof cols.width === "number" ? cols.width : batch.defaultWidth;
3462
+ const hCol = typeof cols.height === "object" && cols.height ? cols.height : null;
3463
+ const hDef = typeof cols.height === "number" ? cols.height : typeof cols.width === "number" ? cols.width : batch.defaultHeight;
3464
+ const rCol = typeof cols.radius === "object" && cols.radius ? cols.radius : null;
3465
+ const rDef = typeof cols.radius === "number" ? cols.radius : batch.defaultRadius;
3466
+ const fcCol = typeof cols.fillColor === "object" && cols.fillColor ? cols.fillColor : null;
3467
+ const fcDef = typeof cols.fillColor === "number" ? cols.fillColor : batch.defaultFillColor;
3468
+ const faCol = typeof cols.fillAlpha === "object" && cols.fillAlpha ? cols.fillAlpha : null;
3469
+ const faDef = typeof cols.fillAlpha === "number" ? cols.fillAlpha : batch.defaultFillAlpha;
3470
+ const bcCol = typeof cols.borderColor === "object" && cols.borderColor ? cols.borderColor : null;
3471
+ const bcDef = typeof cols.borderColor === "number" ? cols.borderColor : batch.defaultBorderColor;
3472
+ const baCol = typeof cols.borderAlpha === "object" && cols.borderAlpha ? cols.borderAlpha : null;
3473
+ const baDef = typeof cols.borderAlpha === "number" ? cols.borderAlpha : batch.defaultBorderAlpha;
3474
+ const bwCol = typeof cols.borderWidth === "object" && cols.borderWidth ? cols.borderWidth : null;
3475
+ const bwDef = typeof cols.borderWidth === "number" ? cols.borderWidth : batch.defaultBorderWidth;
3476
+ const rotCol = cols.rotation ?? null;
3477
+ const ordCol = cols.order ?? null;
3478
+ let lastFc = -1, lastFa = -1, fillAbgr = 0;
3479
+ let lastBc = -1, lastBa = -1, borderAbgr = 0;
3480
+ const rotMat = this.sfMat;
3481
+ for (let i = 0; i < n; i++) {
3482
+ const s = ordCol ? ordCol[i] : i;
3483
+ const w = wCol ? wCol[s] : wDef;
3484
+ const h = hCol ? hCol[s] : wCol ? w : hDef;
3485
+ if (w <= 0 || h <= 0) continue;
3486
+ const fa = (faCol ? faCol[s] : faDef) * alpha;
3487
+ const bw = bwCol ? bwCol[s] : bwDef;
3488
+ const ba = bw > 0 ? (baCol ? baCol[s] : baDef) * alpha : 0;
3489
+ if (fa <= 0 && ba <= 0) continue;
3490
+ const fc = fcCol ? fcCol[s] : fcDef;
3491
+ if (fc !== lastFc || fa !== lastFa) {
3492
+ lastFc = fc;
3493
+ lastFa = fa;
3494
+ fillAbgr = packColorAbgr(fc, fa);
3495
+ }
3496
+ if (bw > 0) {
3497
+ const bc = bcCol ? bcCol[s] : bcDef;
3498
+ if (bc !== lastBc || ba !== lastBa) {
3499
+ lastBc = bc;
3500
+ lastBa = ba;
3501
+ borderAbgr = packColorAbgr(bc, ba);
3502
+ }
3503
+ } else borderAbgr = 0;
3504
+ const rad = rCol ? rCol[s] : rDef;
3505
+ const px = cols.x[s];
3506
+ const py = cols.y[s];
3507
+ const rot = rotCol ? rotCol[s] : 0;
3508
+ if (rot === 0) {
3509
+ const rx = px - w * oxRatio;
3510
+ const ry = py - h * oyRatio;
3511
+ if (this.isOffscreen(m, rx, ry, rx + w, ry + h)) {
3512
+ this.curCulled++;
3513
+ continue;
3514
+ }
3515
+ if (sdf.pending >= 4096) this.flushSdf();
3516
+ sdf.pushSimple(m, basePxScale, rx, ry, w, h, rad, fillAbgr, borderAbgr, bw, step);
3517
+ } else {
3518
+ const cs = Math.cos(rot), sn = Math.sin(rot);
3519
+ rotMat.a = m.a * cs + m.c * sn;
3520
+ rotMat.b = m.b * cs + m.d * sn;
3521
+ rotMat.c = m.a * -sn + m.c * cs;
3522
+ rotMat.d = m.b * -sn + m.d * cs;
3523
+ rotMat.tx = m.a * px + m.c * py + m.tx;
3524
+ rotMat.ty = m.b * px + m.d * py + m.ty;
3525
+ const rx = -w * oxRatio, ry = -h * oyRatio;
3526
+ if (this.isOffscreen(rotMat, rx, ry, rx + w, ry + h)) {
3527
+ this.curCulled++;
3528
+ continue;
3529
+ }
3530
+ if (sdf.pending >= 4096) this.flushSdf();
3531
+ sdf.pushSimple(rotMat, basePxScale, rx, ry, w, h, rad, fillAbgr, borderAbgr, bw, step);
3532
+ }
3533
+ }
3534
+ }
3423
3535
  renderMsdfIcon(icon, m, alpha) {
3424
3536
  const w = icon.width;
3425
3537
  const h = icon.height;
@@ -3638,6 +3750,7 @@ var RendererGl = class extends EventEmitter {
3638
3750
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filt);
3639
3751
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filt);
3640
3752
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
3753
+ this.bindTextureAt(0, this.dummyTex);
3641
3754
  gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3642
3755
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glTex, 0);
3643
3756
  gl.bindFramebuffer(gl.FRAMEBUFFER, this.activeFbo);
@@ -3663,7 +3776,25 @@ var RendererGl = class extends EventEmitter {
3663
3776
  width: w,
3664
3777
  height: h,
3665
3778
  texture: tex,
3779
+ camera: new Camera(w, h, true),
3666
3780
  _fbo: fbo,
3781
+ readPixels: (out) => {
3782
+ const byteLen = w * h * 4;
3783
+ const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
3784
+ if (this.contextLost) return dst;
3785
+ this.flushAll();
3786
+ const raw = new Uint8Array(byteLen);
3787
+ const prev = this.activeFbo;
3788
+ gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3789
+ gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, raw);
3790
+ gl.bindFramebuffer(gl.FRAMEBUFFER, prev);
3791
+ const rowStride = w * 4;
3792
+ for (let y = 0; y < h; y++) {
3793
+ const srcOff = (h - 1 - y) * rowStride;
3794
+ dst.set(raw.subarray(srcOff, srcOff + rowStride), y * rowStride);
3795
+ }
3796
+ return dst;
3797
+ },
3667
3798
  destroy: () => {
3668
3799
  try {
3669
3800
  if (!this.contextLost) {
@@ -3676,13 +3807,14 @@ var RendererGl = class extends EventEmitter {
3676
3807
  }
3677
3808
  };
3678
3809
  }
3679
- /** Render a `DisplayList` (or custom pass callback) into an offscreen `StageRenderTarget` FBO. */
3680
- renderToTarget(target, content, camera) {
3810
+ /** Render a `DisplayList` (or custom pass callback) into an offscreen `StageRenderTarget` FBO (`camera` defaults to `target.camera`). */
3811
+ renderToTarget(target, content, camera = target.camera) {
3681
3812
  if (this.contextLost) return;
3682
3813
  this.flushAll();
3683
3814
  const gl = this.gl;
3684
3815
  const src = target.texture.source[0];
3685
3816
  if (!src?.glTexture) return;
3817
+ this.evictTextureFromUnits(src.glTexture);
3686
3818
  let fbo = target._fbo;
3687
3819
  if (!fbo) {
3688
3820
  const created = gl.createFramebuffer();
@@ -3693,9 +3825,11 @@ var RendererGl = class extends EventEmitter {
3693
3825
  target._fbo = fbo;
3694
3826
  } else gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3695
3827
  const prevFbo = this.activeFbo;
3828
+ const prevFboTex = this.activeFboTex;
3696
3829
  const prevW = this.width;
3697
3830
  const prevH = this.height;
3698
3831
  this.activeFbo = fbo;
3832
+ this.activeFboTex = src.glTexture;
3699
3833
  this.width = target.width;
3700
3834
  this.height = target.height;
3701
3835
  this.updateProjection();
@@ -3716,7 +3850,9 @@ var RendererGl = class extends EventEmitter {
3716
3850
  for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, null, camAlpha, 0);
3717
3851
  }
3718
3852
  this.flushAll();
3853
+ if (prevFboTex) this.evictTextureFromUnits(prevFboTex);
3719
3854
  this.activeFbo = prevFbo;
3855
+ this.activeFboTex = prevFboTex;
3720
3856
  gl.bindFramebuffer(gl.FRAMEBUFFER, prevFbo);
3721
3857
  this.width = prevW;
3722
3858
  this.height = prevH;
@@ -3805,6 +3941,9 @@ var RendererGl = class extends EventEmitter {
3805
3941
  case "InstancedBatch":
3806
3942
  this.renderInstancedBatch(obj, m, effAlpha);
3807
3943
  break;
3944
+ case "ShapeBatch":
3945
+ this.renderShapeBatch(obj, m, effAlpha);
3946
+ break;
3808
3947
  case "Container": {
3809
3948
  const cont = obj;
3810
3949
  const mask = cont.clipMask;
@@ -3976,9 +4115,10 @@ var Pointer = class {
3976
4115
  }
3977
4116
  };
3978
4117
  function normalizeKeyToken(codeOrKey) {
3979
- const raw = String(codeOrKey ?? "").trim();
4118
+ const str = String(codeOrKey ?? "");
4119
+ if (str === " ") return "SPACE";
4120
+ const raw = str.trim();
3980
4121
  if (!raw) return "";
3981
- if (raw === " ") return "SPACE";
3982
4122
  const up = raw.toUpperCase();
3983
4123
  if (up.length === 1 && up >= "A" && up <= "Z") return `KEY${up}`;
3984
4124
  if (up.length === 1 && up >= "0" && up <= "9") return `DIGIT${up}`;
@@ -3987,22 +4127,24 @@ function normalizeKeyToken(codeOrKey) {
3987
4127
  var KeyboardPlugin = class extends EventEmitter {
3988
4128
  win;
3989
4129
  getFrameSeq;
4130
+ getWriteFrameSeq;
3990
4131
  downCodes = /* @__PURE__ */ new Set();
3991
- pressedFrame = /* @__PURE__ */ new Map();
3992
- releasedFrame = /* @__PURE__ */ new Map();
4132
+ pressedUntil = /* @__PURE__ */ new Map();
4133
+ releasedUntil = /* @__PURE__ */ new Map();
3993
4134
  onKeyDown;
3994
4135
  onKeyUp;
3995
4136
  onBlur;
3996
- constructor(win, getFrameSeq = () => 0) {
4137
+ constructor(win, getFrameSeq = () => 0, getWriteFrameSeq = getFrameSeq) {
3997
4138
  super();
3998
4139
  this.win = win;
3999
4140
  this.getFrameSeq = getFrameSeq;
4141
+ this.getWriteFrameSeq = getWriteFrameSeq;
4000
4142
  this.onKeyDown = (e) => {
4001
4143
  const code = normalizeKeyToken(e.code || e.key);
4002
4144
  if (code) {
4003
4145
  if (!this.downCodes.has(code)) {
4004
4146
  this.downCodes.add(code);
4005
- this.pressedFrame.set(code, this.getFrameSeq());
4147
+ this.pressedUntil.set(code, this.getWriteFrameSeq());
4006
4148
  }
4007
4149
  }
4008
4150
  this.emit("keydown", e);
@@ -4010,7 +4152,7 @@ var KeyboardPlugin = class extends EventEmitter {
4010
4152
  };
4011
4153
  this.onKeyUp = (e) => {
4012
4154
  const code = normalizeKeyToken(e.code || e.key);
4013
- if (code && this.downCodes.delete(code)) this.releasedFrame.set(code, this.getFrameSeq());
4155
+ if (code && this.downCodes.delete(code)) this.releasedUntil.set(code, this.getWriteFrameSeq());
4014
4156
  this.emit("keyup", e);
4015
4157
  if (code) this.emit(`keyup-${code}`, e);
4016
4158
  };
@@ -4021,22 +4163,24 @@ var KeyboardPlugin = class extends EventEmitter {
4021
4163
  win.addEventListener("keyup", this.onKeyUp);
4022
4164
  win.addEventListener("blur", this.onBlur);
4023
4165
  }
4024
- /** Returns `true` while `codeOrKey` (e.g. `'KeyW'`, `'W'`, `'ArrowLeft'`, `'Space'`) is held down. */
4166
+ /** Returns `true` while `codeOrKey` (e.g. `'KeyW'`, `'W'`, `'ArrowLeft'`, `'Space'`, `' '`) is held down. */
4025
4167
  isDown(codeOrKey) {
4026
4168
  const code = normalizeKeyToken(codeOrKey);
4027
4169
  return code ? this.downCodes.has(code) : false;
4028
4170
  }
4029
- /** Returns `true` only on the simulation frame during which `codeOrKey` transitioned up -> down. */
4171
+ /** Returns `true` only through the simulation frame during which `codeOrKey` transitioned up -> down. */
4030
4172
  justPressed(codeOrKey) {
4031
4173
  const code = normalizeKeyToken(codeOrKey);
4032
4174
  if (!code) return false;
4033
- return this.pressedFrame.get(code) === this.getFrameSeq();
4175
+ const until = this.pressedUntil.get(code);
4176
+ return until !== void 0 && this.getFrameSeq() <= until;
4034
4177
  }
4035
- /** Returns `true` only on the simulation frame during which `codeOrKey` transitioned down -> up. */
4178
+ /** Returns `true` only through the simulation frame during which `codeOrKey` transitioned down -> up. */
4036
4179
  justReleased(codeOrKey) {
4037
4180
  const code = normalizeKeyToken(codeOrKey);
4038
4181
  if (!code) return false;
4039
- return this.releasedFrame.get(code) === this.getFrameSeq();
4182
+ const until = this.releasedUntil.get(code);
4183
+ return until !== void 0 && this.getFrameSeq() <= until;
4040
4184
  }
4041
4185
  /** Synthesize a deterministic key event from an `InputProvider` (headless timeline, replay, or Mod controller). */
4042
4186
  injectKey(type, codeOrKey, key) {
@@ -4054,8 +4198,8 @@ var KeyboardPlugin = class extends EventEmitter {
4054
4198
  }
4055
4199
  /** Release all currently held keys (called automatically on window blur). */
4056
4200
  resetKeys() {
4057
- const f = this.getFrameSeq();
4058
- for (const c of this.downCodes) this.releasedFrame.set(c, f);
4201
+ const f = this.getWriteFrameSeq();
4202
+ for (const c of this.downCodes) this.releasedUntil.set(c, f);
4059
4203
  this.downCodes.clear();
4060
4204
  }
4061
4205
  destroy() {
@@ -4063,8 +4207,8 @@ var KeyboardPlugin = class extends EventEmitter {
4063
4207
  this.win.removeEventListener("keyup", this.onKeyUp);
4064
4208
  this.win.removeEventListener("blur", this.onBlur);
4065
4209
  this.downCodes.clear();
4066
- this.pressedFrame.clear();
4067
- this.releasedFrame.clear();
4210
+ this.pressedUntil.clear();
4211
+ this.releasedUntil.clear();
4068
4212
  this.removeAllListeners();
4069
4213
  }
4070
4214
  };
@@ -4083,7 +4227,7 @@ var InputPlugin = class extends EventEmitter {
4083
4227
  coalescedScratch = [];
4084
4228
  resizeObserver = null;
4085
4229
  lastTouchMs = -1e9;
4086
- constructor(canvas, win, getCamera, config = {}, getFrameSeq = () => 0) {
4230
+ constructor(canvas, win, getCamera, config = {}, getFrameSeq = () => 0, getWriteFrameSeq = getFrameSeq) {
4087
4231
  super();
4088
4232
  this.canvas = canvas;
4089
4233
  this.win = win;
@@ -4101,7 +4245,7 @@ var InputPlugin = class extends EventEmitter {
4101
4245
  pointers: [this.mousePointer, ...this.touchPointers],
4102
4246
  canvas
4103
4247
  };
4104
- this.keyboard = config.keyboard === false ? null : new KeyboardPlugin(win, getFrameSeq);
4248
+ this.keyboard = config.keyboard === false ? null : new KeyboardPlugin(win, getFrameSeq, getWriteFrameSeq);
4105
4249
  const preventDown = config.mouse?.preventDefaultDown ?? false;
4106
4250
  const preventMove = config.mouse?.preventDefaultMove ?? false;
4107
4251
  const preventUp = config.mouse?.preventDefaultUp ?? false;
@@ -4805,6 +4949,10 @@ var GameObjectFactory = class {
4805
4949
  instancedBatch(key, maxCount = 1024, frame) {
4806
4950
  return this.register(new InstancedBatchObject(this.scene, key, maxCount, frame));
4807
4951
  }
4952
+ /** Create a zero-texture GPU analytical SDF shape batch (`circles`, `rings`, `pills`, `rounded rects`). */
4953
+ shapeBatch(maxCount = 1024, defaults) {
4954
+ return this.register(new ShapeBatchObject(this.scene, maxCount, defaults));
4955
+ }
4808
4956
  container(x = 0, y = 0, children) {
4809
4957
  return this.register(new Container(this.scene, x, y, children));
4810
4958
  }
@@ -4817,15 +4965,21 @@ var ScaleManager = class extends EventEmitter {
4817
4965
  renderer;
4818
4966
  width;
4819
4967
  height;
4968
+ logicalWidth;
4969
+ logicalHeight;
4820
4970
  zoom;
4971
+ mode;
4821
4972
  displayScale;
4822
- constructor(canvas, renderer, width, height, zoom = 1) {
4973
+ constructor(canvas, renderer, width, height, zoom = 1, mode = Scale.NONE) {
4823
4974
  super();
4824
4975
  this.canvas = canvas;
4825
4976
  this.renderer = renderer;
4826
4977
  this.width = Math.max(1, Math.round(width));
4827
4978
  this.height = Math.max(1, Math.round(height));
4979
+ this.logicalWidth = this.width;
4980
+ this.logicalHeight = this.height;
4828
4981
  this.zoom = zoom > 0 ? zoom : 1;
4982
+ this.mode = mode;
4829
4983
  this.displayScale = {
4830
4984
  x: 1 / this.zoom,
4831
4985
  y: 1 / this.zoom
@@ -4851,7 +5005,41 @@ var ScaleManager = class extends EventEmitter {
4851
5005
  });
4852
5006
  return this;
4853
5007
  }
5008
+ /**
5009
+ * Proportionally fit `canvas.style.width/height` inside `availWidth × availHeight` (or `canvas.parentElement` / `window`)
5010
+ * preserving `logicalWidth : logicalHeight` aspect ratio (zero CSS matrix distortion; `InputPlugin` & `clientToWorld` stay exact).
5011
+ */
5012
+ fitToContainer(availWidth, availHeight) {
5013
+ const parent = this.canvas?.parentElement;
5014
+ const win = this.canvas?.ownerDocument?.defaultView;
5015
+ const aw = Math.max(1, availWidth ?? parent?.clientWidth ?? win?.innerWidth ?? this.logicalWidth);
5016
+ const ah = Math.max(1, availHeight ?? parent?.clientHeight ?? win?.innerHeight ?? this.logicalHeight);
5017
+ const lw = Math.max(1, this.logicalWidth);
5018
+ const lh = Math.max(1, this.logicalHeight);
5019
+ const scale = Math.min(aw / lw, ah / lh);
5020
+ const cssWidth = Math.max(1, Math.round(lw * scale));
5021
+ const cssHeight = Math.max(1, Math.round(lh * scale));
5022
+ const offsetX = Math.floor((aw - cssWidth) * .5);
5023
+ const offsetY = Math.floor((ah - cssHeight) * .5);
5024
+ if (this.canvas?.style) {
5025
+ this.canvas.style.width = `${cssWidth}px`;
5026
+ this.canvas.style.height = `${cssHeight}px`;
5027
+ }
5028
+ this.displayScale.x = this.width / cssWidth;
5029
+ this.displayScale.y = this.height / cssHeight;
5030
+ return {
5031
+ scale,
5032
+ cssWidth,
5033
+ cssHeight,
5034
+ offsetX,
5035
+ offsetY
5036
+ };
5037
+ }
4854
5038
  refresh() {
5039
+ if (this.mode === Scale.FIT) {
5040
+ this.fitToContainer();
5041
+ return this;
5042
+ }
4855
5043
  this.displayScale.x = 1 / (this.zoom || 1);
4856
5044
  this.displayScale.y = 1 / (this.zoom || 1);
4857
5045
  return this;
@@ -5120,6 +5308,8 @@ var Game = class {
5120
5308
  pendingDestroy = false;
5121
5309
  destroyed = false;
5122
5310
  ownedCanvas = false;
5311
+ inStep = false;
5312
+ onWindowResize = null;
5123
5313
  constructor(config = {}) {
5124
5314
  this.config = config;
5125
5315
  const fallbackDoc = typeof document !== "undefined" ? document : void 0;
@@ -5176,10 +5366,20 @@ var Game = class {
5176
5366
  this.textures.invalidateAllGl();
5177
5367
  for (let i = 0; i < this.scene.scenes.length; i++) this.scene.scenes[i]._invalidateTextGl();
5178
5368
  };
5179
- this.scale = new ScaleManager(this.canvas, this.renderer, width, height, zoom);
5369
+ const scaleMode = config.scale?.mode ?? Scale.NONE;
5370
+ this.scale = new ScaleManager(this.canvas, this.renderer, width, height, zoom, scaleMode);
5371
+ if (scaleMode === Scale.FIT) {
5372
+ this.scale.fitToContainer();
5373
+ if (this.win && typeof this.win.addEventListener === "function") {
5374
+ this.onWindowResize = () => {
5375
+ if (!this.destroyed) this.scale.fitToContainer();
5376
+ };
5377
+ this.win.addEventListener("resize", this.onWindowResize);
5378
+ }
5379
+ }
5180
5380
  this.cache = { json: new JsonCache() };
5181
5381
  this.scene = new SceneManager(this);
5182
- this.input = new InputPlugin(this.canvas, this.win, () => this.scene.getPrimaryCamera(), config.input, () => this.clock.frameSeq);
5382
+ this.input = new InputPlugin(this.canvas, this.win, () => this.scene.getPrimaryCamera(), config.input, () => this.clock.frameSeq, () => this.inStep ? this.clock.frameSeq : this.clock.frameSeq + 1);
5183
5383
  this.kernel = new StageKernel(this);
5184
5384
  if (config.plugins) for (let i = 0; i < config.plugins.length; i++) this.kernel.use(config.plugins[i]);
5185
5385
  this.loop = new GameLoop(this, this.win);
@@ -5199,40 +5399,62 @@ var Game = class {
5199
5399
  /** Advance simulation, tweens, kernel plugins, and camera follow by one deterministic tick WITHOUT rendering pixels. */
5200
5400
  stepSimOnly(time, delta) {
5201
5401
  if (this.pendingDestroy || this.destroyed || this.isPaused) return false;
5202
- this.clock.advance(time, delta);
5203
- this.kernel._prestep(time, delta);
5204
- this.events.emit("prestep", time, delta);
5205
- const scenes = this.scene.scenes;
5206
- for (let i = 0; i < scenes.length; i++) {
5207
- const sc = scenes[i];
5208
- if (sc.sys.settings.status === Scenes.RUNNING) {
5209
- sc.events.emit("preupdate", time, delta);
5210
- if (sc.tweens.hasActive) sc.tweens.step(this.clock.lastDeltaMs);
5211
- sc.update?.(time, delta);
5212
- this.kernel._updateScene(sc, time, delta);
5213
- sc.cameras.main.stepFollow(this.clock.lastDeltaMs);
5214
- sc.events.emit("update", time, delta);
5215
- sc.events.emit("postupdate", time, delta);
5402
+ const prevInStep = this.inStep;
5403
+ this.inStep = true;
5404
+ try {
5405
+ this.clock.advance(time, delta);
5406
+ this.kernel._prestep(time, delta);
5407
+ this.events.emit("prestep", time, delta);
5408
+ const scenes = this.scene.scenes;
5409
+ for (let i = 0; i < scenes.length; i++) {
5410
+ const sc = scenes[i];
5411
+ if (sc.sys.settings.status === Scenes.RUNNING) {
5412
+ sc.events.emit("preupdate", time, delta);
5413
+ if (sc.tweens.hasActive) sc.tweens.step(this.clock.lastDeltaMs);
5414
+ sc.update?.(time, delta);
5415
+ this.kernel._updateScene(sc, time, delta);
5416
+ sc.cameras.main.stepFollow(this.clock.lastDeltaMs);
5417
+ sc.events.emit("update", time, delta);
5418
+ sc.events.emit("postupdate", time, delta);
5419
+ }
5216
5420
  }
5421
+ return true;
5422
+ } finally {
5423
+ this.inStep = prevInStep;
5217
5424
  }
5218
- return true;
5219
5425
  }
5220
5426
  step(time, delta) {
5221
- if (!this.stepSimOnly(time, delta)) return;
5222
- const scenes = this.scene.scenes;
5223
- this.events.emit("prerender", this.renderer);
5224
- for (let i = 0; i < scenes.length; i++) {
5225
- const sc = scenes[i];
5226
- if (sc.sys.settings.status === Scenes.RUNNING) this.renderer.render(sc._displayList, sc.cameras.main);
5427
+ const prevInStep = this.inStep;
5428
+ this.inStep = true;
5429
+ try {
5430
+ if (!this.stepSimOnly(time, delta)) return;
5431
+ const scenes = this.scene.scenes;
5432
+ const lastDt = this.clock.lastDeltaMs;
5433
+ this.events.emit("prerender", this.renderer);
5434
+ for (let i = 0; i < scenes.length; i++) {
5435
+ const sc = scenes[i];
5436
+ if (sc.sys.settings.status === Scenes.RUNNING) {
5437
+ sc.preRender?.(time, lastDt);
5438
+ this.kernel._prerenderScene(sc, this.renderer, time, lastDt);
5439
+ sc.events.emit("prerender", this.renderer, time, lastDt);
5440
+ this.renderer.render(sc._displayList, sc.cameras.main);
5441
+ }
5442
+ }
5443
+ this.kernel._postrender(this.renderer);
5444
+ this.events.emit("postrender", this.renderer);
5445
+ } finally {
5446
+ this.inStep = prevInStep;
5227
5447
  }
5228
- this.kernel._postrender(this.renderer);
5229
- this.events.emit("postrender", this.renderer);
5230
5448
  }
5231
5449
  destroy(removeCanvas = false) {
5232
5450
  if (this.destroyed) return;
5233
5451
  this.pendingDestroy = true;
5234
5452
  this.destroyed = true;
5235
5453
  this.loop.destroy();
5454
+ if (this.onWindowResize && this.win && typeof this.win.removeEventListener === "function") {
5455
+ this.win.removeEventListener("resize", this.onWindowResize);
5456
+ this.onWindowResize = null;
5457
+ }
5236
5458
  this.events.emit("destroy", this);
5237
5459
  this.kernel.dispose();
5238
5460
  this.scene.destroy();
@@ -5246,4 +5468,4 @@ var Game = class {
5246
5468
  else if (removeCanvas) this.canvas.remove();
5247
5469
  }
5248
5470
  };
5249
- export { TextureSource as S, Camera as _, JsonCache as a, Texture as b, Scene as c, KeyboardPlugin as d, Pointer as f, SdfPanelBatcher as g, MsdfIconBatcher as h, GameObjectFactory as i, SceneManager as l, PixelAaBatcher as m, Game as n, LoaderPlugin as o, RendererGl as p, GameLoop as r, ScaleManager as s, EngineClock as t, InputPlugin as u, CanvasTexture as v, TextureManager as x, Frame as y };
5471
+ export { CanvasTexture as _, JsonCache as a, TextureManager as b, Scene as c, KeyboardPlugin as d, Pointer as f, SdfPanelBatcher as g, MsdfIconBatcher as h, GameObjectFactory as i, SceneManager as l, PixelAaBatcher as m, Game as n, LoaderPlugin as o, RendererGl as p, GameLoop as r, ScaleManager as s, EngineClock as t, InputPlugin as u, Frame as v, TextureSource as x, Texture as y };