@holtjs/stage 0.5.0 → 0.5.1

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-BE6fTBzo.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
  }
@@ -2626,7 +2629,19 @@ var RendererGl = class extends EventEmitter {
2626
2629
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, src.premultiplyAlpha);
2627
2630
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
2628
2631
  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);
2632
+ if (src.isRenderTarget) {
2633
+ const wrap = canRepeat ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2634
+ const filt = src.filterMode === FilterMode.NEAREST ? gl.NEAREST : gl.LINEAR;
2635
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
2636
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
2637
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filt);
2638
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filt);
2639
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, src.width, src.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
2640
+ src.glWidth = src.width;
2641
+ src.glHeight = src.height;
2642
+ src.glRepeat = canRepeat;
2643
+ src.glPremultiplyAlpha = src.premultiplyAlpha;
2644
+ } 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
2645
  else {
2631
2646
  const wrap = canRepeat ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2632
2647
  const filt = src.filterMode === FilterMode.NEAREST ? gl.NEAREST : gl.LINEAR;
@@ -3625,12 +3640,15 @@ var RendererGl = class extends EventEmitter {
3625
3640
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
3626
3641
  gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3627
3642
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glTex, 0);
3628
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
3643
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.activeFbo);
3629
3644
  const dummyStub = {
3630
3645
  width: w,
3631
3646
  height: h
3632
3647
  };
3633
- const tex = textures.addImage(key, dummyStub, { filterMode });
3648
+ const tex = textures.addImage(key, dummyStub, {
3649
+ filterMode,
3650
+ isRenderTarget: true
3651
+ });
3634
3652
  if (!tex) throw new Error("Stage: failed to register WebGL RenderTarget texture");
3635
3653
  const src = tex.source[0];
3636
3654
  src.glTexture = glTex;
@@ -3914,687 +3932,6 @@ var RendererGl = class extends EventEmitter {
3914
3932
  this.boundArrayBuf = null;
3915
3933
  }
3916
3934
  };
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
3935
  // src/input.ts
4599
3936
  var Pointer = class {
4600
3937
  id;
@@ -5909,4 +5246,4 @@ var Game = class {
5909
5246
  else if (removeCanvas) this.canvas.remove();
5910
5247
  }
5911
5248
  };
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 };
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 };