@holtjs/stage 0.5.0 → 0.5.2

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,5 +1,5 @@
1
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 { a as CustomPassObject, c as Graphics, d as MsdfIconObject, f as NineSliceObject, g as TileSpriteObject, h as TextObject, i as Container, l as ImageObject, m as RectangleObject, o as DisplayList, p as PanelObject, r as BlitterObject, t as RendererWebGpu, u as InstancedBatchObject } from "./renderer-webgpu-BorAD6Kk.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-Djk7FayG.js";
3
3
  import { TweenManager } from "./tween.js";
4
4
  import { StageKernel } from "./kernel.js";
5
5
  // src/texture.ts
@@ -18,8 +18,9 @@ var TextureSource = class {
18
18
  glWidth = 0;
19
19
  glHeight = 0;
20
20
  dirty = true;
21
+ isRenderTarget;
21
22
  host;
22
- constructor(texture, image, filterMode, host, premultiplyAlpha = true) {
23
+ constructor(texture, image, filterMode, host, premultiplyAlpha = true, isRenderTarget = false) {
23
24
  this.texture = texture;
24
25
  this.image = image;
25
26
  this.width = sourceWidth(image);
@@ -27,8 +28,9 @@ var TextureSource = class {
27
28
  this.filterMode = filterMode;
28
29
  this.premultiplyAlpha = premultiplyAlpha;
29
30
  this.glPremultiplyAlpha = premultiplyAlpha;
31
+ this.isRenderTarget = isRenderTarget;
30
32
  this.host = host;
31
- if (host?.gl && !host.contextLost && this.width > 0 && this.height > 0) host.uploadTextureSource(this);
33
+ if (!this.isRenderTarget && host?.gl && !host.contextLost && this.width > 0 && this.height > 0) host.uploadTextureSource(this);
32
34
  }
33
35
  setFilter(mode) {
34
36
  if (this.filterMode === mode) return;
@@ -110,10 +112,10 @@ var Texture = class {
110
112
  key;
111
113
  source;
112
114
  frames = Object.create(null);
113
- constructor(manager, key, image, filterMode, host, premultiplyAlpha = true) {
115
+ constructor(manager, key, image, filterMode, host, premultiplyAlpha = true, isRenderTarget = false) {
114
116
  this.manager = manager;
115
117
  this.key = key;
116
- const src = new TextureSource(this, image, filterMode, host, premultiplyAlpha);
118
+ const src = new TextureSource(this, image, filterMode, host, premultiplyAlpha, isRenderTarget);
117
119
  this.source = [src];
118
120
  this.add("__BASE", 0, 0, 0, src.width, src.height);
119
121
  }
@@ -213,7 +215,8 @@ var TextureManager = class {
213
215
  if (this.exists(key)) this.remove(key);
214
216
  const filter = options?.filterMode ?? this.defaultFilter;
215
217
  const premultiply = options?.premultiplyAlpha ?? true;
216
- const tex = new Texture(this, key, source, filter, this.host, premultiply);
218
+ const isRt = options?.isRenderTarget ?? false;
219
+ const tex = new Texture(this, key, source, filter, this.host, premultiply, isRt);
217
220
  this.list[key] = tex;
218
221
  return tex;
219
222
  }
@@ -2327,6 +2330,7 @@ var RendererGl = class extends EventEmitter {
2327
2330
  maskWorldMat = createMatrix2D();
2328
2331
  bindTextSlot0;
2329
2332
  activeFbo = null;
2333
+ activeFboTex = null;
2330
2334
  instProgram = null;
2331
2335
  instUres = null;
2332
2336
  instVao = null;
@@ -2376,6 +2380,7 @@ var RendererGl = class extends EventEmitter {
2376
2380
  e.preventDefault();
2377
2381
  this.contextLost = true;
2378
2382
  this.activeFbo = null;
2383
+ this.activeFboTex = null;
2379
2384
  this.boundTexCache.fill(null);
2380
2385
  this.activeTexUnit = -1;
2381
2386
  this.batchStateBound = false;
@@ -2626,7 +2631,20 @@ var RendererGl = class extends EventEmitter {
2626
2631
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, src.premultiplyAlpha);
2627
2632
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
2628
2633
  const canRepeat = src.repeat && (this.isWebGL2 || isPow2(src.width) && isPow2(src.height));
2629
- 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);
2634
+ if (src.isRenderTarget) {
2635
+ const wrap = canRepeat ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2636
+ const filt = src.filterMode === FilterMode.NEAREST ? gl.NEAREST : gl.LINEAR;
2637
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
2638
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
2639
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filt);
2640
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filt);
2641
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, src.width, src.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
2642
+ src.glWidth = src.width;
2643
+ src.glHeight = src.height;
2644
+ src.glRepeat = canRepeat;
2645
+ src.glPremultiplyAlpha = src.premultiplyAlpha;
2646
+ this.bindTextureAt(0, this.dummyTex);
2647
+ } 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);
2630
2648
  else {
2631
2649
  const wrap = canRepeat ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2632
2650
  const filt = src.filterMode === FilterMode.NEAREST ? gl.NEAREST : gl.LINEAR;
@@ -2692,12 +2710,13 @@ var RendererGl = class extends EventEmitter {
2692
2710
  this.batchStateBound = true;
2693
2711
  }
2694
2712
  }
2695
- /** Full state restore for external callers that may have mutated viewport/blend/textures/pixelStore/FBO directly. */
2713
+ /** Full state restore for external callers that may have mutated viewport/blend/cull/depth/stencil/colorMask/textures/pixelStore/FBO directly. */
2696
2714
  restoreState() {
2697
2715
  const gl = this.gl;
2698
2716
  this.invalidateProgramState();
2699
2717
  this.boundTexCache.fill(null);
2700
2718
  this.activeTexUnit = -1;
2719
+ if (this.activeFboTex) this.evictTextureFromUnits(this.activeFboTex);
2701
2720
  if (typeof gl.bindFramebuffer === "function") gl.bindFramebuffer(gl.FRAMEBUFFER, this.activeFbo);
2702
2721
  if (typeof gl.pixelStorei === "function") {
2703
2722
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
@@ -2707,10 +2726,21 @@ var RendererGl = class extends EventEmitter {
2707
2726
  if (gl2.UNPACK_ROW_LENGTH !== void 0) gl.pixelStorei(gl2.UNPACK_ROW_LENGTH, 0);
2708
2727
  }
2709
2728
  gl.viewport(0, 0, this.width, this.height);
2729
+ gl.disable(gl.DEPTH_TEST);
2730
+ if (gl.STENCIL_TEST !== void 0) gl.disable(gl.STENCIL_TEST);
2731
+ gl.disable(gl.CULL_FACE);
2732
+ if (typeof gl.depthMask === "function") gl.depthMask(true);
2733
+ if (typeof gl.frontFace === "function" && gl.CCW !== void 0) gl.frontFace(gl.CCW);
2734
+ if (typeof gl.colorMask === "function") gl.colorMask(true, true, true, true);
2710
2735
  gl.enable(gl.BLEND);
2736
+ if (typeof gl.blendEquation === "function" && gl.FUNC_ADD !== void 0) gl.blendEquation(gl.FUNC_ADD);
2711
2737
  this.ensureBatchState();
2712
2738
  this.applyCurrentScissor();
2713
2739
  }
2740
+ /** Evict `tex` (and populate any post-restore `null` slots with `dummyTex`) so binding its FBO never forms a GL feedback loop. */
2741
+ evictTextureFromUnits(tex) {
2742
+ for (let i = 0; i < this.boundTexCache.length; i++) if (this.boundTexCache[i] === tex || this.boundTexCache[i] === null) this.bindTextureAt(i, this.dummyTex);
2743
+ }
2714
2744
  setBlend(mode) {
2715
2745
  if (this.curBlend === mode) return;
2716
2746
  this.flushAll();
@@ -3623,14 +3653,18 @@ var RendererGl = class extends EventEmitter {
3623
3653
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filt);
3624
3654
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filt);
3625
3655
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
3656
+ this.bindTextureAt(0, this.dummyTex);
3626
3657
  gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3627
3658
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glTex, 0);
3628
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
3659
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.activeFbo);
3629
3660
  const dummyStub = {
3630
3661
  width: w,
3631
3662
  height: h
3632
3663
  };
3633
- const tex = textures.addImage(key, dummyStub, { filterMode });
3664
+ const tex = textures.addImage(key, dummyStub, {
3665
+ filterMode,
3666
+ isRenderTarget: true
3667
+ });
3634
3668
  if (!tex) throw new Error("Stage: failed to register WebGL RenderTarget texture");
3635
3669
  const src = tex.source[0];
3636
3670
  src.glTexture = glTex;
@@ -3665,6 +3699,7 @@ var RendererGl = class extends EventEmitter {
3665
3699
  const gl = this.gl;
3666
3700
  const src = target.texture.source[0];
3667
3701
  if (!src?.glTexture) return;
3702
+ this.evictTextureFromUnits(src.glTexture);
3668
3703
  let fbo = target._fbo;
3669
3704
  if (!fbo) {
3670
3705
  const created = gl.createFramebuffer();
@@ -3675,9 +3710,11 @@ var RendererGl = class extends EventEmitter {
3675
3710
  target._fbo = fbo;
3676
3711
  } else gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3677
3712
  const prevFbo = this.activeFbo;
3713
+ const prevFboTex = this.activeFboTex;
3678
3714
  const prevW = this.width;
3679
3715
  const prevH = this.height;
3680
3716
  this.activeFbo = fbo;
3717
+ this.activeFboTex = src.glTexture;
3681
3718
  this.width = target.width;
3682
3719
  this.height = target.height;
3683
3720
  this.updateProjection();
@@ -3698,7 +3735,9 @@ var RendererGl = class extends EventEmitter {
3698
3735
  for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, null, camAlpha, 0);
3699
3736
  }
3700
3737
  this.flushAll();
3738
+ if (prevFboTex) this.evictTextureFromUnits(prevFboTex);
3701
3739
  this.activeFbo = prevFbo;
3740
+ this.activeFboTex = prevFboTex;
3702
3741
  gl.bindFramebuffer(gl.FRAMEBUFFER, prevFbo);
3703
3742
  this.width = prevW;
3704
3743
  this.height = prevH;
@@ -3914,687 +3953,6 @@ var RendererGl = class extends EventEmitter {
3914
3953
  this.boundArrayBuf = null;
3915
3954
  }
3916
3955
  };
3917
- // src/renderer-2d.ts
3918
- function addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl) {
3919
- const maxR = Math.min(w, h) * .5;
3920
- const rTL = Math.max(0, Math.min(tl, maxR));
3921
- const rTR = Math.max(0, Math.min(tr, maxR));
3922
- const rBR = Math.max(0, Math.min(br, maxR));
3923
- const rBL = Math.max(0, Math.min(bl, maxR));
3924
- ctx.moveTo(x + rTL, y);
3925
- ctx.lineTo(x + w - rTR, y);
3926
- if (rTR > 0) ctx.quadraticCurveTo(x + w, y, x + w, y + rTR);
3927
- ctx.lineTo(x + w, y + h - rBR);
3928
- if (rBR > 0) ctx.quadraticCurveTo(x + w, y + h, x + w - rBR, y + h);
3929
- ctx.lineTo(x + rBL, y + h);
3930
- if (rBL > 0) ctx.quadraticCurveTo(x, y + h, x, y + h - rBL);
3931
- ctx.lineTo(x, y + rTL);
3932
- if (rTL > 0) ctx.quadraticCurveTo(x, y, x + rTL, y);
3933
- ctx.closePath();
3934
- }
3935
- function perCornerRoundedRectPath(ctx, x, y, w, h, tl, tr, br, bl) {
3936
- ctx.beginPath();
3937
- addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl);
3938
- }
3939
- function paintInnerTopGlint2D(ctx, ix, iy, iw, ih, p) {
3940
- if (p.topGlintHeight <= 0 || p.topGlintAlpha <= 0 || p.fillAlpha <= 0 || iw <= 0 || ih <= 0) return;
3941
- const gh = Math.min(p.topGlintHeight, ih);
3942
- ctx.save();
3943
- ctx.clip();
3944
- ctx.fillStyle = rgbToCss(p.topGlintColor, p.topGlintAlpha * p.fillAlpha);
3945
- ctx.fillRect(ix, iy, iw, gh);
3946
- ctx.restore();
3947
- }
3948
- function roundedRectPath(ctx, x, y, w, h, radius) {
3949
- const r = Math.max(0, Math.min(radius, w * .5, h * .5));
3950
- ctx.beginPath();
3951
- ctx.moveTo(x + r, y);
3952
- ctx.lineTo(x + w - r, y);
3953
- ctx.quadraticCurveTo(x + w, y, x + w, y + r);
3954
- ctx.lineTo(x + w, y + h - r);
3955
- ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
3956
- ctx.lineTo(x + r, y + h);
3957
- ctx.quadraticCurveTo(x, y + h, x, y + h - r);
3958
- ctx.lineTo(x, y + r);
3959
- ctx.quadraticCurveTo(x, y, x + r, y);
3960
- ctx.closePath();
3961
- }
3962
- var Renderer2D = class Renderer2D extends EventEmitter {
3963
- type = 1;
3964
- canvas;
3965
- ctx;
3966
- gl = null;
3967
- contextLost = false;
3968
- maxTextures = 0;
3969
- width;
3970
- height;
3971
- tintCanvas = null;
3972
- tintCtx = null;
3973
- maskWorldMat = createMatrix2D();
3974
- pathCache = /* @__PURE__ */ new Map();
3975
- constructor(canvas, width, height, existingCtx) {
3976
- super();
3977
- this.canvas = canvas;
3978
- this.width = Math.max(1, Math.round(width));
3979
- this.height = Math.max(1, Math.round(height));
3980
- const ctx = existingCtx ?? canvas.getContext("2d");
3981
- if (!ctx) throw new Error("Stage2D: 2D canvas context unavailable");
3982
- this.ctx = ctx;
3983
- }
3984
- stats() {
3985
- return {
3986
- drawCalls: 0,
3987
- flushes: 0,
3988
- quads: 0,
3989
- tris: 0,
3990
- culled: 0,
3991
- texBinds: 0,
3992
- batchMaxUnits: 0,
3993
- sdfPanels: 0,
3994
- sdfDraws: 0,
3995
- msdfIcons: 0,
3996
- msdfDraws: 0,
3997
- pixelAaIcons: 0,
3998
- pixelAaDraws: 0
3999
- };
4000
- }
4001
- resize(width, height) {
4002
- const w = Math.max(1, Math.round(width));
4003
- const h = Math.max(1, Math.round(height));
4004
- if (this.width === w && this.height === h && this.canvas.width === w && this.canvas.height === h) return;
4005
- this.width = w;
4006
- this.height = h;
4007
- this.canvas.width = w;
4008
- this.canvas.height = h;
4009
- }
4010
- getTintedSource(img, sx, sy, sw, sh, rgb, fill) {
4011
- const iw = Math.max(1, Math.ceil(sw));
4012
- const ih = Math.max(1, Math.ceil(sh));
4013
- if (!this.tintCanvas) {
4014
- const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
4015
- if (!doc) return null;
4016
- this.tintCanvas = doc.createElement("canvas");
4017
- this.tintCtx = this.tintCanvas.getContext("2d");
4018
- }
4019
- const tc = this.tintCanvas;
4020
- const tg = this.tintCtx;
4021
- if (!tc || !tg) return null;
4022
- if (tc.width < iw || tc.height < ih) {
4023
- tc.width = Math.max(tc.width, iw);
4024
- tc.height = Math.max(tc.height, ih);
4025
- }
4026
- tg.save();
4027
- tg.globalCompositeOperation = "source-over";
4028
- tg.clearRect(0, 0, iw, ih);
4029
- tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
4030
- tg.globalCompositeOperation = fill ? "source-in" : "multiply";
4031
- tg.fillStyle = rgbToCss(rgb, 1);
4032
- tg.fillRect(0, 0, iw, ih);
4033
- if (!fill) {
4034
- tg.globalCompositeOperation = "destination-in";
4035
- tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
4036
- }
4037
- tg.restore();
4038
- return tc;
4039
- }
4040
- renderImage(img, ctx) {
4041
- const frame = img.frame;
4042
- if (!frame || frame.width <= 0 || frame.height <= 0) return;
4043
- const srcImg = frame.source.image;
4044
- const ox = img.displayOriginX, oy = img.displayOriginY;
4045
- let sx = frame.cutX, sy = frame.cutY, sw = frame.cutWidth, sh = frame.cutHeight;
4046
- let dx = -ox, dy = -oy, dw = img.width, dh = img.height;
4047
- if (img.isCropped) {
4048
- const rx = Math.max(0, Math.min(frame.width, img.cropX));
4049
- const ry = Math.max(0, Math.min(frame.height, img.cropY));
4050
- const rw = Math.max(0, Math.min(frame.width - rx, img.cropW));
4051
- const rh = Math.max(0, Math.min(frame.height - ry, img.cropH));
4052
- if (rw <= 0 || rh <= 0) return;
4053
- sx += rx;
4054
- sy += ry;
4055
- sw = rw;
4056
- sh = rh;
4057
- dx = -ox + rx;
4058
- dy = -oy + ry;
4059
- dw = rw;
4060
- dh = rh;
4061
- }
4062
- if (img.pixelAA) {
4063
- const m = ctx.getTransform();
4064
- const dispW = Math.hypot(m.a * dw, m.b * dw);
4065
- const dispH = Math.hypot(m.c * dh, m.d * dh);
4066
- const sxRatio = dispW / Math.max(1, sw);
4067
- const syRatio = dispH / Math.max(1, sh);
4068
- ctx.imageSmoothingEnabled = !(sxRatio >= .98 && syRatio >= .98 && Math.abs(sxRatio - Math.round(sxRatio)) <= .02 && Math.abs(syRatio - Math.round(syRatio)) <= .02);
4069
- } else ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
4070
- if (img.flipX || img.flipY) {
4071
- ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
4072
- ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
4073
- }
4074
- if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
4075
- const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL);
4076
- if (tinted) {
4077
- ctx.drawImage(tinted, 0, 0, Math.ceil(sw), Math.ceil(sh), dx, dy, dw, dh);
4078
- return;
4079
- }
4080
- }
4081
- ctx.drawImage(srcImg, sx, sy, sw, sh, dx, dy, dw, dh);
4082
- }
4083
- renderNineSlice(ns, ctx) {
4084
- const frame = ns.frame;
4085
- if (!frame || frame.width <= 0 || frame.height <= 0 || ns.width <= 0 || ns.height <= 0) return;
4086
- const srcImg = frame.source.image;
4087
- ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
4088
- const w = ns.width, h = ns.height;
4089
- const ox = ns.displayOriginX, oy = ns.displayOriginY;
4090
- let L = ns.leftWidth, R = ns.rightWidth, T = ns.topHeight, B = ns.bottomHeight;
4091
- if (L + R > w && L + R > 0) {
4092
- const s = w / (L + R);
4093
- L *= s;
4094
- R *= s;
4095
- }
4096
- if (T + B > h && T + B > 0) {
4097
- const s = h / (T + B);
4098
- T *= s;
4099
- B *= s;
4100
- }
4101
- const sxs = [
4102
- frame.cutX,
4103
- frame.cutX + ns.leftWidth,
4104
- frame.cutX + frame.cutWidth - ns.rightWidth,
4105
- frame.cutX + frame.cutWidth
4106
- ];
4107
- const sys = [
4108
- frame.cutY,
4109
- frame.cutY + ns.topHeight,
4110
- frame.cutY + frame.cutHeight - ns.bottomHeight,
4111
- frame.cutY + frame.cutHeight
4112
- ];
4113
- const dxs = [
4114
- -ox,
4115
- -ox + L,
4116
- -ox + w - R,
4117
- -ox + w
4118
- ];
4119
- const dys = [
4120
- -oy,
4121
- -oy + T,
4122
- -oy + h - B,
4123
- -oy + h
4124
- ];
4125
- for (let r = 0; r < 3; r++) {
4126
- const sh = sys[r + 1] - sys[r], dh = dys[r + 1] - dys[r];
4127
- if (sh <= 0 || dh <= 0) continue;
4128
- for (let c = 0; c < 3; c++) {
4129
- const sw = sxs[c + 1] - sxs[c], dw = dxs[c + 1] - dxs[c];
4130
- if (sw <= 0 || dw <= 0) continue;
4131
- ctx.drawImage(srcImg, sxs[c], sys[r], sw, sh, dxs[c], dys[r], dw, dh);
4132
- }
4133
- }
4134
- }
4135
- renderTileSprite(ts, ctx) {
4136
- const frame = ts.frame;
4137
- if (!frame || frame.width <= 0 || frame.height <= 0 || ts.width <= 0 || ts.height <= 0) return;
4138
- const srcImg = frame.source.image;
4139
- ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
4140
- const ox = ts.displayOriginX, oy = ts.displayOriginY;
4141
- const fw = frame.width, fh = frame.height;
4142
- const offX = (ts.tilePositionX % fw + fw) % fw;
4143
- const offY = (ts.tilePositionY % fh + fh) % fh;
4144
- let drawSrc = srcImg;
4145
- let baseSx = frame.cutX, baseSy = frame.cutY;
4146
- if (ts.isTinted && ts.tint !== 0xffffff) {
4147
- const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false);
4148
- if (tinted) {
4149
- drawSrc = tinted;
4150
- baseSx = 0;
4151
- baseSy = 0;
4152
- }
4153
- }
4154
- for (let y = 0; y < ts.height;) {
4155
- const ty = (y + offY) % fh;
4156
- const dh = Math.min(fh - ty, ts.height - y);
4157
- for (let x = 0; x < ts.width;) {
4158
- const tx = (x + offX) % fw;
4159
- const dw = Math.min(fw - tx, ts.width - x);
4160
- ctx.drawImage(drawSrc, baseSx + tx, baseSy + ty, dw, dh, -ox + x, -oy + y, dw, dh);
4161
- x += dw;
4162
- }
4163
- y += dh;
4164
- }
4165
- }
4166
- renderRectangle(r, ctx) {
4167
- if (r.fillAlpha <= 0 || r.width <= 0 || r.height <= 0) return;
4168
- ctx.fillStyle = rgbToCss(r.fillColor, r.fillAlpha);
4169
- ctx.fillRect(-r.displayOriginX, -r.displayOriginY, r.width, r.height);
4170
- }
4171
- renderPanel(p, ctx) {
4172
- const w = p.width, h = p.height;
4173
- if (w <= 0 || h <= 0) return;
4174
- const x = -p.displayOriginX, y = -p.displayOriginY;
4175
- const bw = Math.max(0, Math.min(p.borderWidth, Math.min(w, h) * .5));
4176
- if (bw > 0 && p.openSide !== 0) {
4177
- if (p.borderAlpha > 0) {
4178
- perCornerRoundedRectPath(ctx, x, y, w, h, p.radiusTL, p.radiusTR, p.radiusBR, p.radiusBL);
4179
- ctx.fillStyle = rgbToCss(p.borderColor, p.borderAlpha);
4180
- ctx.fill();
4181
- }
4182
- if (p.fillAlpha > 0) {
4183
- const ix = p.openSide > 0 ? x : x + bw;
4184
- const iw = Math.max(0, w - bw);
4185
- const ih = Math.max(0, h - bw * 2);
4186
- if (iw > 0 && ih > 0) {
4187
- const irTL = p.openSide > 0 ? 0 : Math.max(0, p.radiusTL - bw);
4188
- const irTR = p.openSide < 0 ? 0 : Math.max(0, p.radiusTR - bw);
4189
- const irBR = p.openSide < 0 ? 0 : Math.max(0, p.radiusBR - bw);
4190
- const irBL = p.openSide > 0 ? 0 : Math.max(0, p.radiusBL - bw);
4191
- perCornerRoundedRectPath(ctx, ix, y + bw, iw, ih, irTL, irTR, irBR, irBL);
4192
- ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
4193
- ctx.fill();
4194
- paintInnerTopGlint2D(ctx, ix, y + bw, iw, ih, p);
4195
- }
4196
- }
4197
- return;
4198
- }
4199
- if (p.fillAlpha > 0) {
4200
- const iw = w - bw * 2, ih = h - bw * 2;
4201
- if (iw > 0 && ih > 0) {
4202
- const irTL = Math.max(0, p.radiusTL - bw);
4203
- const irTR = Math.max(0, p.radiusTR - bw);
4204
- const irBR = Math.max(0, p.radiusBR - bw);
4205
- const irBL = Math.max(0, p.radiusBL - bw);
4206
- perCornerRoundedRectPath(ctx, x + bw, y + bw, iw, ih, irTL, irTR, irBR, irBL);
4207
- ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
4208
- ctx.fill();
4209
- paintInnerTopGlint2D(ctx, x + bw, y + bw, iw, ih, p);
4210
- }
4211
- }
4212
- if (bw > 0 && p.borderAlpha > 0) {
4213
- const half = bw * .5;
4214
- perCornerRoundedRectPath(ctx, x + half, y + half, Math.max(0, w - bw), Math.max(0, h - bw), Math.max(0, p.radiusTL - half), Math.max(0, p.radiusTR - half), Math.max(0, p.radiusBR - half), Math.max(0, p.radiusBL - half));
4215
- ctx.lineWidth = bw;
4216
- ctx.strokeStyle = rgbToCss(p.borderColor, p.borderAlpha);
4217
- ctx.stroke();
4218
- }
4219
- }
4220
- renderMsdfIcon(ic, ctx) {
4221
- const w = ic.width, h = ic.height;
4222
- if (w <= 0 || h <= 0 || !ic.svgPath || typeof Path2D !== "function") return;
4223
- let p2d = this.pathCache.get(ic.svgPath);
4224
- if (!p2d) {
4225
- p2d = new Path2D(ic.svgPath);
4226
- this.pathCache.set(ic.svgPath, p2d);
4227
- }
4228
- const view = Math.max(1, ic.viewSize);
4229
- ctx.save();
4230
- ctx.translate(-ic.displayOriginX, -ic.displayOriginY);
4231
- ctx.scale(w / view, h / view);
4232
- if (ic.outlineWidth > 0 && ic.outlineAlpha > 0) {
4233
- ctx.lineJoin = "round";
4234
- ctx.lineCap = "round";
4235
- ctx.lineWidth = ic.outlineWidth * (view / Math.max(1, Math.min(w, h))) * 2;
4236
- ctx.strokeStyle = rgbToCss(ic.outlineColor, ic.outlineAlpha);
4237
- ctx.stroke(p2d);
4238
- }
4239
- if (ic.fillAlpha > 0) {
4240
- ctx.fillStyle = rgbToCss(ic.fillColor, ic.fillAlpha);
4241
- ctx.fill(p2d);
4242
- }
4243
- ctx.restore();
4244
- }
4245
- renderText(txt, ctx) {
4246
- const cv = txt.ensureCanvas();
4247
- if (!cv || txt.width <= 0 || txt.height <= 0) return;
4248
- ctx.imageSmoothingEnabled = true;
4249
- const ox = txt.displayOriginX, oy = txt.displayOriginY;
4250
- if (!txt.isCropped) {
4251
- ctx.drawImage(cv, 0, 0, cv.width, cv.height, -ox, -oy, txt.width, txt.height);
4252
- return;
4253
- }
4254
- const fw = txt.frame.cutWidth || cv.width || 1;
4255
- const fh = txt.frame.cutHeight || cv.height || 1;
4256
- const rx = Math.max(0, Math.min(fw, txt.cropX));
4257
- const ry = Math.max(0, Math.min(fh, txt.cropY));
4258
- const rw = Math.max(0, Math.min(fw - rx, txt.cropW));
4259
- const rh = Math.max(0, Math.min(fh - ry, txt.cropH));
4260
- if (rw <= 0 || rh <= 0) return;
4261
- const resX = fw / txt.width;
4262
- const resY = fh / txt.height;
4263
- ctx.drawImage(cv, rx, ry, rw, rh, -ox + rx / resX, -oy + ry / resY, rw / resX, rh / resY);
4264
- }
4265
- renderGraphics(g, ctx) {
4266
- if (g.opLen === 0) return;
4267
- let fillRgb = WHITE_RGB, fillA = 1;
4268
- let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
4269
- const ops = g.ops;
4270
- const nums = g.nums;
4271
- let ni = 0;
4272
- for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
4273
- case 22:
4274
- ctx.save();
4275
- break;
4276
- case 23:
4277
- ctx.restore();
4278
- break;
4279
- case 1:
4280
- fillRgb = nums[ni++];
4281
- fillA = nums[ni++];
4282
- ctx.fillStyle = rgbToCss(fillRgb, fillA);
4283
- break;
4284
- case 2:
4285
- lineW = nums[ni++];
4286
- lineRgb = nums[ni++];
4287
- lineA = nums[ni++];
4288
- ctx.lineWidth = lineW;
4289
- ctx.strokeStyle = rgbToCss(lineRgb, lineA);
4290
- break;
4291
- case 3: {
4292
- const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
4293
- if (fillA > 0) ctx.fillRect(x, y, w, h);
4294
- break;
4295
- }
4296
- case 4: {
4297
- const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
4298
- if (lineA > 0 && lineW > 0) ctx.strokeRect(x, y, w, h);
4299
- break;
4300
- }
4301
- case 5: {
4302
- const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
4303
- if (fillA > 0 && w > 0 && h > 0) {
4304
- roundedRectPath(ctx, x, y, w, h, r);
4305
- ctx.fill();
4306
- }
4307
- break;
4308
- }
4309
- case 6: {
4310
- const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
4311
- if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
4312
- roundedRectPath(ctx, x, y, w, h, r);
4313
- ctx.stroke();
4314
- }
4315
- break;
4316
- }
4317
- case 7: {
4318
- const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
4319
- if (fillA > 0 && r > 0) {
4320
- ctx.beginPath();
4321
- ctx.arc(cx, cy, r, 0, Math.PI * 2);
4322
- ctx.fill();
4323
- }
4324
- break;
4325
- }
4326
- case 8: {
4327
- const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
4328
- if (lineA > 0 && lineW > 0 && r > 0) {
4329
- ctx.beginPath();
4330
- ctx.arc(cx, cy, r, 0, Math.PI * 2);
4331
- ctx.stroke();
4332
- }
4333
- break;
4334
- }
4335
- case 9: {
4336
- const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
4337
- if (fillA > 0 && w > 0 && h > 0) {
4338
- ctx.beginPath();
4339
- ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
4340
- ctx.fill();
4341
- }
4342
- break;
4343
- }
4344
- case 10: {
4345
- const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
4346
- if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
4347
- ctx.beginPath();
4348
- ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
4349
- ctx.stroke();
4350
- }
4351
- break;
4352
- }
4353
- case 11: {
4354
- const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
4355
- if (fillA > 0) {
4356
- ctx.beginPath();
4357
- ctx.moveTo(x0, y0);
4358
- ctx.lineTo(x1, y1);
4359
- ctx.lineTo(x2, y2);
4360
- ctx.closePath();
4361
- ctx.fill();
4362
- }
4363
- break;
4364
- }
4365
- case 12: {
4366
- const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
4367
- if (lineA > 0 && lineW > 0) {
4368
- ctx.beginPath();
4369
- ctx.moveTo(x0, y0);
4370
- ctx.lineTo(x1, y1);
4371
- ctx.lineTo(x2, y2);
4372
- ctx.closePath();
4373
- ctx.stroke();
4374
- }
4375
- break;
4376
- }
4377
- case 13: {
4378
- const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++];
4379
- if (lineA > 0 && lineW > 0) {
4380
- ctx.beginPath();
4381
- ctx.moveTo(x0, y0);
4382
- ctx.lineTo(x1, y1);
4383
- ctx.stroke();
4384
- }
4385
- break;
4386
- }
4387
- case 14:
4388
- ctx.beginPath();
4389
- break;
4390
- case 15:
4391
- ctx.moveTo(nums[ni++], nums[ni++]);
4392
- break;
4393
- case 16:
4394
- ctx.lineTo(nums[ni++], nums[ni++]);
4395
- break;
4396
- case 17: {
4397
- const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
4398
- ctx.arc(cx, cy, r, a0, a1, ccw);
4399
- break;
4400
- }
4401
- case 18: {
4402
- const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
4403
- ctx.moveTo(cx, cy);
4404
- ctx.arc(cx, cy, r, a0, a1, ccw);
4405
- ctx.closePath();
4406
- break;
4407
- }
4408
- case 19:
4409
- ctx.closePath();
4410
- break;
4411
- case 20:
4412
- if (fillA > 0) ctx.fill();
4413
- break;
4414
- case 21: if (lineA > 0 && lineW > 0) ctx.stroke();
4415
- }
4416
- }
4417
- renderInstancedBatch(batch, ctx) {
4418
- const cols = batch.columns;
4419
- if (!cols || cols.count <= 0) return;
4420
- const baseAlpha = ctx.globalAlpha;
4421
- const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
4422
- for (let i = 0; i < n; i++) {
4423
- const s = cols.order ? cols.order[i] : i;
4424
- const a = baseAlpha * (cols.alpha ? cols.alpha[s] : 1);
4425
- if (a <= 0) continue;
4426
- const fr = cols.frameIndex ? batch.texture.get(cols.frameIndex[s]) : batch.defaultFrame;
4427
- const sx = cols.scaleX ? cols.scaleX[s] : 1;
4428
- const sy = cols.scaleY ? cols.scaleY[s] : sx;
4429
- const rot = cols.rotation ? cols.rotation[s] : 0;
4430
- const px = cols.x[s];
4431
- const py = cols.y[s];
4432
- ctx.save();
4433
- ctx.globalAlpha = a;
4434
- ctx.imageSmoothingEnabled = fr.source.filterMode !== FilterMode.NEAREST;
4435
- ctx.translate(px, py);
4436
- if (rot !== 0) ctx.rotate(rot);
4437
- if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
4438
- ctx.drawImage(fr.source.image, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, -fr.width * batch.originX, -fr.height * batch.originY, fr.width, fr.height);
4439
- ctx.restore();
4440
- }
4441
- ctx.globalAlpha = baseAlpha;
4442
- }
4443
- /** Create an offscreen Canvas2D render target backed by a registered Stage `Texture` (`key`). */
4444
- createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
4445
- const w = Math.max(1, Math.round(width));
4446
- const h = Math.max(1, Math.round(height));
4447
- const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
4448
- if (!doc) throw new Error("Stage: ownerDocument unavailable for Canvas2D RenderTarget");
4449
- const rtCanvas = doc.createElement("canvas");
4450
- rtCanvas.width = w;
4451
- rtCanvas.height = h;
4452
- const tex = textures.addCanvas(key, rtCanvas);
4453
- if (!tex) throw new Error("Stage: failed to register Canvas2D RenderTarget texture");
4454
- tex.source[0]?.setFilter(filterMode);
4455
- const rtRenderer = new Renderer2D(rtCanvas, w, h);
4456
- return {
4457
- key,
4458
- width: w,
4459
- height: h,
4460
- texture: tex,
4461
- _rtRenderer: rtRenderer,
4462
- destroy: () => {
4463
- rtRenderer.destroy();
4464
- textures.remove(key);
4465
- }
4466
- };
4467
- }
4468
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node. */
4469
- renderToTarget(target, content, camera) {
4470
- const sub = target._rtRenderer;
4471
- if (!sub) return;
4472
- if (typeof content === "function") {
4473
- camera.syncMetrics();
4474
- content(sub, camera);
4475
- } else sub.render(content, camera);
4476
- const src = target.texture.source[0];
4477
- if (src) src.dirty = true;
4478
- }
4479
- renderBlitter(blitter, ctx) {
4480
- const list = blitter.children.list;
4481
- const baseAlpha = ctx.globalAlpha;
4482
- for (let i = 0; i < list.length; i++) {
4483
- const bob = list[i];
4484
- if (!bob.visible || bob.alpha <= 0) continue;
4485
- const frame = bob.frame;
4486
- ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
4487
- ctx.globalAlpha = baseAlpha * bob.alpha;
4488
- ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
4489
- }
4490
- ctx.globalAlpha = baseAlpha;
4491
- }
4492
- renderNode(obj, camera, ctx, isRoot) {
4493
- if (!obj.visible || obj.alpha <= 0) return;
4494
- ctx.save();
4495
- if (isRoot) {
4496
- if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) {
4497
- const cm = camera.matrixCombined;
4498
- ctx.setTransform(cm.a, cm.b, cm.c, cm.d, cm.tx, cm.ty);
4499
- } else {
4500
- const z = camera.zoom || 1;
4501
- const ox = camera.width * camera.originX;
4502
- const oy = camera.height * camera.originY;
4503
- const sx = camera.roundPixels ? Math.round(camera.scrollX) : camera.scrollX;
4504
- const sy = camera.roundPixels ? Math.round(camera.scrollY) : camera.scrollY;
4505
- ctx.setTransform(z, 0, 0, z, (-sx * obj.scrollFactorX - ox) * z + ox + camera.x, (-sy * obj.scrollFactorY - oy) * z + oy + camera.y);
4506
- }
4507
- }
4508
- if (obj.type === "Container") {
4509
- const mask = obj.clipMask;
4510
- if (mask && mask.active && mask.graphics?.maskRect) {
4511
- const mr = mask.graphics.maskRect;
4512
- const mw = mask.graphics.getWorldTransformMatrix(this.maskWorldMat);
4513
- const cm = camera.matrixCombined;
4514
- const wx0 = mw.a * mr.x + mw.c * mr.y + mw.tx;
4515
- const wy0 = mw.b * mr.x + mw.d * mr.y + mw.ty;
4516
- const wx1 = mw.a * (mr.x + mr.w) + mw.c * (mr.y + mr.h) + mw.tx;
4517
- const wy1 = mw.b * (mr.x + mr.w) + mw.d * (mr.y + mr.h) + mw.ty;
4518
- const sx0 = cm.a * wx0 + cm.c * wy0 + cm.tx;
4519
- const sy0 = cm.b * wx0 + cm.d * wy0 + cm.ty;
4520
- const sx1 = cm.a * wx1 + cm.c * wy1 + cm.tx;
4521
- const sy1 = cm.b * wx1 + cm.d * wy1 + cm.ty;
4522
- ctx.save();
4523
- ctx.setTransform(1, 0, 0, 1, 0, 0);
4524
- ctx.beginPath();
4525
- ctx.rect(Math.min(sx0, sx1), Math.min(sy0, sy1), Math.abs(sx1 - sx0), Math.abs(sy1 - sy0));
4526
- ctx.restore();
4527
- ctx.clip();
4528
- }
4529
- }
4530
- ctx.globalAlpha *= obj.alpha;
4531
- if (obj.blendMode === BlendModes.ADD) ctx.globalCompositeOperation = "lighter";
4532
- else if (obj.blendMode === BlendModes.MULTIPLY) ctx.globalCompositeOperation = "multiply";
4533
- else if (obj.blendMode === BlendModes.SCREEN) ctx.globalCompositeOperation = "screen";
4534
- if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
4535
- if (obj.rotation !== 0) ctx.rotate(obj.rotation);
4536
- if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
4537
- switch (obj.type) {
4538
- case "Image":
4539
- this.renderImage(obj, ctx);
4540
- break;
4541
- case "NineSlice":
4542
- this.renderNineSlice(obj, ctx);
4543
- break;
4544
- case "TileSprite":
4545
- this.renderTileSprite(obj, ctx);
4546
- break;
4547
- case "Rectangle":
4548
- this.renderRectangle(obj, ctx);
4549
- break;
4550
- case "Panel":
4551
- this.renderPanel(obj, ctx);
4552
- break;
4553
- case "MsdfIcon":
4554
- this.renderMsdfIcon(obj, ctx);
4555
- break;
4556
- case "Text":
4557
- this.renderText(obj, ctx);
4558
- break;
4559
- case "Graphics":
4560
- this.renderGraphics(obj, ctx);
4561
- break;
4562
- case "Blitter":
4563
- this.renderBlitter(obj, ctx);
4564
- break;
4565
- case "InstancedBatch":
4566
- this.renderInstancedBatch(obj, ctx);
4567
- break;
4568
- case "Container": {
4569
- const list = obj.sortedChildren();
4570
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
4571
- break;
4572
- }
4573
- }
4574
- ctx.restore();
4575
- }
4576
- render(displayList, camera) {
4577
- camera.syncMetrics();
4578
- const ctx = this.ctx;
4579
- ctx.setTransform(1, 0, 0, 1, 0, 0);
4580
- ctx.globalAlpha = 1;
4581
- ctx.globalCompositeOperation = "source-over";
4582
- if (camera.transparent) ctx.clearRect(0, 0, this.width, this.height);
4583
- else {
4584
- const bg = camera.backgroundColor;
4585
- ctx.fillStyle = `rgba(${Math.round(bg.r * 255)},${Math.round(bg.g * 255)},${Math.round(bg.b * 255)},${bg.a})`;
4586
- ctx.fillRect(0, 0, this.width, this.height);
4587
- }
4588
- ctx.globalAlpha = camera.alpha;
4589
- const list = displayList.sorted();
4590
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
4591
- }
4592
- destroy() {
4593
- this.removeAllListeners();
4594
- this.tintCanvas = null;
4595
- this.tintCtx = null;
4596
- }
4597
- };
4598
3956
  // src/input.ts
4599
3957
  var Pointer = class {
4600
3958
  id;
@@ -4639,9 +3997,10 @@ var Pointer = class {
4639
3997
  }
4640
3998
  };
4641
3999
  function normalizeKeyToken(codeOrKey) {
4642
- const raw = String(codeOrKey ?? "").trim();
4000
+ const str = String(codeOrKey ?? "");
4001
+ if (str === " ") return "SPACE";
4002
+ const raw = str.trim();
4643
4003
  if (!raw) return "";
4644
- if (raw === " ") return "SPACE";
4645
4004
  const up = raw.toUpperCase();
4646
4005
  if (up.length === 1 && up >= "A" && up <= "Z") return `KEY${up}`;
4647
4006
  if (up.length === 1 && up >= "0" && up <= "9") return `DIGIT${up}`;
@@ -4650,22 +4009,24 @@ function normalizeKeyToken(codeOrKey) {
4650
4009
  var KeyboardPlugin = class extends EventEmitter {
4651
4010
  win;
4652
4011
  getFrameSeq;
4012
+ getWriteFrameSeq;
4653
4013
  downCodes = /* @__PURE__ */ new Set();
4654
- pressedFrame = /* @__PURE__ */ new Map();
4655
- releasedFrame = /* @__PURE__ */ new Map();
4014
+ pressedUntil = /* @__PURE__ */ new Map();
4015
+ releasedUntil = /* @__PURE__ */ new Map();
4656
4016
  onKeyDown;
4657
4017
  onKeyUp;
4658
4018
  onBlur;
4659
- constructor(win, getFrameSeq = () => 0) {
4019
+ constructor(win, getFrameSeq = () => 0, getWriteFrameSeq = getFrameSeq) {
4660
4020
  super();
4661
4021
  this.win = win;
4662
4022
  this.getFrameSeq = getFrameSeq;
4023
+ this.getWriteFrameSeq = getWriteFrameSeq;
4663
4024
  this.onKeyDown = (e) => {
4664
4025
  const code = normalizeKeyToken(e.code || e.key);
4665
4026
  if (code) {
4666
4027
  if (!this.downCodes.has(code)) {
4667
4028
  this.downCodes.add(code);
4668
- this.pressedFrame.set(code, this.getFrameSeq());
4029
+ this.pressedUntil.set(code, this.getWriteFrameSeq());
4669
4030
  }
4670
4031
  }
4671
4032
  this.emit("keydown", e);
@@ -4673,7 +4034,7 @@ var KeyboardPlugin = class extends EventEmitter {
4673
4034
  };
4674
4035
  this.onKeyUp = (e) => {
4675
4036
  const code = normalizeKeyToken(e.code || e.key);
4676
- if (code && this.downCodes.delete(code)) this.releasedFrame.set(code, this.getFrameSeq());
4037
+ if (code && this.downCodes.delete(code)) this.releasedUntil.set(code, this.getWriteFrameSeq());
4677
4038
  this.emit("keyup", e);
4678
4039
  if (code) this.emit(`keyup-${code}`, e);
4679
4040
  };
@@ -4684,22 +4045,24 @@ var KeyboardPlugin = class extends EventEmitter {
4684
4045
  win.addEventListener("keyup", this.onKeyUp);
4685
4046
  win.addEventListener("blur", this.onBlur);
4686
4047
  }
4687
- /** Returns `true` while `codeOrKey` (e.g. `'KeyW'`, `'W'`, `'ArrowLeft'`, `'Space'`) is held down. */
4048
+ /** Returns `true` while `codeOrKey` (e.g. `'KeyW'`, `'W'`, `'ArrowLeft'`, `'Space'`, `' '`) is held down. */
4688
4049
  isDown(codeOrKey) {
4689
4050
  const code = normalizeKeyToken(codeOrKey);
4690
4051
  return code ? this.downCodes.has(code) : false;
4691
4052
  }
4692
- /** Returns `true` only on the simulation frame during which `codeOrKey` transitioned up -> down. */
4053
+ /** Returns `true` only through the simulation frame during which `codeOrKey` transitioned up -> down. */
4693
4054
  justPressed(codeOrKey) {
4694
4055
  const code = normalizeKeyToken(codeOrKey);
4695
4056
  if (!code) return false;
4696
- return this.pressedFrame.get(code) === this.getFrameSeq();
4057
+ const until = this.pressedUntil.get(code);
4058
+ return until !== void 0 && this.getFrameSeq() <= until;
4697
4059
  }
4698
- /** Returns `true` only on the simulation frame during which `codeOrKey` transitioned down -> up. */
4060
+ /** Returns `true` only through the simulation frame during which `codeOrKey` transitioned down -> up. */
4699
4061
  justReleased(codeOrKey) {
4700
4062
  const code = normalizeKeyToken(codeOrKey);
4701
4063
  if (!code) return false;
4702
- return this.releasedFrame.get(code) === this.getFrameSeq();
4064
+ const until = this.releasedUntil.get(code);
4065
+ return until !== void 0 && this.getFrameSeq() <= until;
4703
4066
  }
4704
4067
  /** Synthesize a deterministic key event from an `InputProvider` (headless timeline, replay, or Mod controller). */
4705
4068
  injectKey(type, codeOrKey, key) {
@@ -4717,8 +4080,8 @@ var KeyboardPlugin = class extends EventEmitter {
4717
4080
  }
4718
4081
  /** Release all currently held keys (called automatically on window blur). */
4719
4082
  resetKeys() {
4720
- const f = this.getFrameSeq();
4721
- for (const c of this.downCodes) this.releasedFrame.set(c, f);
4083
+ const f = this.getWriteFrameSeq();
4084
+ for (const c of this.downCodes) this.releasedUntil.set(c, f);
4722
4085
  this.downCodes.clear();
4723
4086
  }
4724
4087
  destroy() {
@@ -4726,8 +4089,8 @@ var KeyboardPlugin = class extends EventEmitter {
4726
4089
  this.win.removeEventListener("keyup", this.onKeyUp);
4727
4090
  this.win.removeEventListener("blur", this.onBlur);
4728
4091
  this.downCodes.clear();
4729
- this.pressedFrame.clear();
4730
- this.releasedFrame.clear();
4092
+ this.pressedUntil.clear();
4093
+ this.releasedUntil.clear();
4731
4094
  this.removeAllListeners();
4732
4095
  }
4733
4096
  };
@@ -4746,7 +4109,7 @@ var InputPlugin = class extends EventEmitter {
4746
4109
  coalescedScratch = [];
4747
4110
  resizeObserver = null;
4748
4111
  lastTouchMs = -1e9;
4749
- constructor(canvas, win, getCamera, config = {}, getFrameSeq = () => 0) {
4112
+ constructor(canvas, win, getCamera, config = {}, getFrameSeq = () => 0, getWriteFrameSeq = getFrameSeq) {
4750
4113
  super();
4751
4114
  this.canvas = canvas;
4752
4115
  this.win = win;
@@ -4764,7 +4127,7 @@ var InputPlugin = class extends EventEmitter {
4764
4127
  pointers: [this.mousePointer, ...this.touchPointers],
4765
4128
  canvas
4766
4129
  };
4767
- this.keyboard = config.keyboard === false ? null : new KeyboardPlugin(win, getFrameSeq);
4130
+ this.keyboard = config.keyboard === false ? null : new KeyboardPlugin(win, getFrameSeq, getWriteFrameSeq);
4768
4131
  const preventDown = config.mouse?.preventDefaultDown ?? false;
4769
4132
  const preventMove = config.mouse?.preventDefaultMove ?? false;
4770
4133
  const preventUp = config.mouse?.preventDefaultUp ?? false;
@@ -5783,6 +5146,7 @@ var Game = class {
5783
5146
  pendingDestroy = false;
5784
5147
  destroyed = false;
5785
5148
  ownedCanvas = false;
5149
+ inStep = false;
5786
5150
  constructor(config = {}) {
5787
5151
  this.config = config;
5788
5152
  const fallbackDoc = typeof document !== "undefined" ? document : void 0;
@@ -5842,7 +5206,7 @@ var Game = class {
5842
5206
  this.scale = new ScaleManager(this.canvas, this.renderer, width, height, zoom);
5843
5207
  this.cache = { json: new JsonCache() };
5844
5208
  this.scene = new SceneManager(this);
5845
- this.input = new InputPlugin(this.canvas, this.win, () => this.scene.getPrimaryCamera(), config.input, () => this.clock.frameSeq);
5209
+ 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);
5846
5210
  this.kernel = new StageKernel(this);
5847
5211
  if (config.plugins) for (let i = 0; i < config.plugins.length; i++) this.kernel.use(config.plugins[i]);
5848
5212
  this.loop = new GameLoop(this, this.win);
@@ -5862,34 +5226,46 @@ var Game = class {
5862
5226
  /** Advance simulation, tweens, kernel plugins, and camera follow by one deterministic tick WITHOUT rendering pixels. */
5863
5227
  stepSimOnly(time, delta) {
5864
5228
  if (this.pendingDestroy || this.destroyed || this.isPaused) return false;
5865
- this.clock.advance(time, delta);
5866
- this.kernel._prestep(time, delta);
5867
- this.events.emit("prestep", time, delta);
5868
- const scenes = this.scene.scenes;
5869
- for (let i = 0; i < scenes.length; i++) {
5870
- const sc = scenes[i];
5871
- if (sc.sys.settings.status === Scenes.RUNNING) {
5872
- sc.events.emit("preupdate", time, delta);
5873
- if (sc.tweens.hasActive) sc.tweens.step(this.clock.lastDeltaMs);
5874
- sc.update?.(time, delta);
5875
- this.kernel._updateScene(sc, time, delta);
5876
- sc.cameras.main.stepFollow(this.clock.lastDeltaMs);
5877
- sc.events.emit("update", time, delta);
5878
- sc.events.emit("postupdate", time, delta);
5229
+ const prevInStep = this.inStep;
5230
+ this.inStep = true;
5231
+ try {
5232
+ this.clock.advance(time, delta);
5233
+ this.kernel._prestep(time, delta);
5234
+ this.events.emit("prestep", time, delta);
5235
+ const scenes = this.scene.scenes;
5236
+ for (let i = 0; i < scenes.length; i++) {
5237
+ const sc = scenes[i];
5238
+ if (sc.sys.settings.status === Scenes.RUNNING) {
5239
+ sc.events.emit("preupdate", time, delta);
5240
+ if (sc.tweens.hasActive) sc.tweens.step(this.clock.lastDeltaMs);
5241
+ sc.update?.(time, delta);
5242
+ this.kernel._updateScene(sc, time, delta);
5243
+ sc.cameras.main.stepFollow(this.clock.lastDeltaMs);
5244
+ sc.events.emit("update", time, delta);
5245
+ sc.events.emit("postupdate", time, delta);
5246
+ }
5879
5247
  }
5248
+ return true;
5249
+ } finally {
5250
+ this.inStep = prevInStep;
5880
5251
  }
5881
- return true;
5882
5252
  }
5883
5253
  step(time, delta) {
5884
- if (!this.stepSimOnly(time, delta)) return;
5885
- const scenes = this.scene.scenes;
5886
- this.events.emit("prerender", this.renderer);
5887
- for (let i = 0; i < scenes.length; i++) {
5888
- const sc = scenes[i];
5889
- if (sc.sys.settings.status === Scenes.RUNNING) this.renderer.render(sc._displayList, sc.cameras.main);
5254
+ const prevInStep = this.inStep;
5255
+ this.inStep = true;
5256
+ try {
5257
+ if (!this.stepSimOnly(time, delta)) return;
5258
+ const scenes = this.scene.scenes;
5259
+ this.events.emit("prerender", this.renderer);
5260
+ for (let i = 0; i < scenes.length; i++) {
5261
+ const sc = scenes[i];
5262
+ if (sc.sys.settings.status === Scenes.RUNNING) this.renderer.render(sc._displayList, sc.cameras.main);
5263
+ }
5264
+ this.kernel._postrender(this.renderer);
5265
+ this.events.emit("postrender", this.renderer);
5266
+ } finally {
5267
+ this.inStep = prevInStep;
5890
5268
  }
5891
- this.kernel._postrender(this.renderer);
5892
- this.events.emit("postrender", this.renderer);
5893
5269
  }
5894
5270
  destroy(removeCanvas = false) {
5895
5271
  if (this.destroyed) return;
@@ -5909,4 +5285,4 @@ var Game = class {
5909
5285
  else if (removeCanvas) this.canvas.remove();
5910
5286
  }
5911
5287
  };
5912
- export { TextureSource as C, TextureManager as S, SdfPanelBatcher as _, JsonCache as a, Frame as b, Scene as c, KeyboardPlugin as d, Pointer as f, MsdfIconBatcher as g, PixelAaBatcher as h, GameObjectFactory as i, SceneManager as l, RendererGl as m, Game as n, LoaderPlugin as o, Renderer2D as p, GameLoop as r, ScaleManager as s, EngineClock as t, InputPlugin as u, Camera as v, Texture as x, CanvasTexture as y };
5288
+ 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 };