@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.
@@ -1430,6 +1430,786 @@ var InstancedBatchObject = class extends DisplayObject {
1430
1430
  super.destroy(fromScene);
1431
1431
  }
1432
1432
  };
1433
+ // src/renderer-2d.ts
1434
+ function addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl) {
1435
+ const maxR = Math.min(w, h) * .5;
1436
+ const rTL = Math.max(0, Math.min(tl, maxR));
1437
+ const rTR = Math.max(0, Math.min(tr, maxR));
1438
+ const rBR = Math.max(0, Math.min(br, maxR));
1439
+ const rBL = Math.max(0, Math.min(bl, maxR));
1440
+ ctx.moveTo(x + rTL, y);
1441
+ ctx.lineTo(x + w - rTR, y);
1442
+ if (rTR > 0) ctx.quadraticCurveTo(x + w, y, x + w, y + rTR);
1443
+ ctx.lineTo(x + w, y + h - rBR);
1444
+ if (rBR > 0) ctx.quadraticCurveTo(x + w, y + h, x + w - rBR, y + h);
1445
+ ctx.lineTo(x + rBL, y + h);
1446
+ if (rBL > 0) ctx.quadraticCurveTo(x, y + h, x, y + h - rBL);
1447
+ ctx.lineTo(x, y + rTL);
1448
+ if (rTL > 0) ctx.quadraticCurveTo(x, y, x + rTL, y);
1449
+ ctx.closePath();
1450
+ }
1451
+ function perCornerRoundedRectPath(ctx, x, y, w, h, tl, tr, br, bl) {
1452
+ ctx.beginPath();
1453
+ addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl);
1454
+ }
1455
+ function paintInnerTopGlint2D(ctx, ix, iy, iw, ih, p) {
1456
+ if (p.topGlintHeight <= 0 || p.topGlintAlpha <= 0 || p.fillAlpha <= 0 || iw <= 0 || ih <= 0) return;
1457
+ const gh = Math.min(p.topGlintHeight, ih);
1458
+ ctx.save();
1459
+ ctx.clip();
1460
+ ctx.fillStyle = rgbToCss(p.topGlintColor, p.topGlintAlpha * p.fillAlpha);
1461
+ ctx.fillRect(ix, iy, iw, gh);
1462
+ ctx.restore();
1463
+ }
1464
+ function roundedRectPath(ctx, x, y, w, h, radius) {
1465
+ const r = Math.max(0, Math.min(radius, w * .5, h * .5));
1466
+ ctx.beginPath();
1467
+ ctx.moveTo(x + r, y);
1468
+ ctx.lineTo(x + w - r, y);
1469
+ ctx.quadraticCurveTo(x + w, y, x + w, y + r);
1470
+ ctx.lineTo(x + w, y + h - r);
1471
+ ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
1472
+ ctx.lineTo(x + r, y + h);
1473
+ ctx.quadraticCurveTo(x, y + h, x, y + h - r);
1474
+ ctx.lineTo(x, y + r);
1475
+ ctx.quadraticCurveTo(x, y, x + r, y);
1476
+ ctx.closePath();
1477
+ }
1478
+ var Renderer2D = class Renderer2D extends EventEmitter {
1479
+ type = 1;
1480
+ canvas;
1481
+ ctx;
1482
+ gl = null;
1483
+ contextLost = false;
1484
+ maxTextures = 0;
1485
+ width;
1486
+ height;
1487
+ tintCanvas = null;
1488
+ tintCtx = null;
1489
+ maskWorldMat = createMatrix2D();
1490
+ pathCache = /* @__PURE__ */ new Map();
1491
+ curDrawCalls = 0;
1492
+ curFlushes = 0;
1493
+ curQuads = 0;
1494
+ curTris = 0;
1495
+ curSdfPanels = 0;
1496
+ curMsdfIcons = 0;
1497
+ lastStats = {
1498
+ drawCalls: 0,
1499
+ flushes: 0,
1500
+ quads: 0,
1501
+ tris: 0,
1502
+ culled: 0,
1503
+ texBinds: 0,
1504
+ batchMaxUnits: 0,
1505
+ sdfPanels: 0,
1506
+ sdfDraws: 0,
1507
+ msdfIcons: 0,
1508
+ msdfDraws: 0,
1509
+ pixelAaIcons: 0,
1510
+ pixelAaDraws: 0
1511
+ };
1512
+ constructor(canvas, width, height, existingCtx) {
1513
+ super();
1514
+ this.canvas = canvas;
1515
+ this.width = Math.max(1, Math.round(width));
1516
+ this.height = Math.max(1, Math.round(height));
1517
+ const ctx = existingCtx ?? canvas.getContext("2d");
1518
+ if (!ctx) throw new Error("Stage2D: 2D canvas context unavailable");
1519
+ this.ctx = ctx;
1520
+ }
1521
+ stats() {
1522
+ return this.lastStats;
1523
+ }
1524
+ resize(width, height) {
1525
+ const w = Math.max(1, Math.round(width));
1526
+ const h = Math.max(1, Math.round(height));
1527
+ if (this.width === w && this.height === h && this.canvas.width === w && this.canvas.height === h) return;
1528
+ this.width = w;
1529
+ this.height = h;
1530
+ this.canvas.width = w;
1531
+ this.canvas.height = h;
1532
+ }
1533
+ getTintedSource(img, sx, sy, sw, sh, rgb, fill) {
1534
+ const iw = Math.max(1, Math.ceil(sw));
1535
+ const ih = Math.max(1, Math.ceil(sh));
1536
+ if (!this.tintCanvas) {
1537
+ const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
1538
+ if (!doc) return null;
1539
+ this.tintCanvas = doc.createElement("canvas");
1540
+ this.tintCtx = this.tintCanvas.getContext("2d");
1541
+ }
1542
+ const tc = this.tintCanvas;
1543
+ const tg = this.tintCtx;
1544
+ if (!tc || !tg) return null;
1545
+ if (tc.width < iw || tc.height < ih) {
1546
+ tc.width = Math.max(tc.width, iw);
1547
+ tc.height = Math.max(tc.height, ih);
1548
+ }
1549
+ tg.save();
1550
+ tg.globalCompositeOperation = "source-over";
1551
+ tg.clearRect(0, 0, iw, ih);
1552
+ tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1553
+ tg.globalCompositeOperation = fill ? "source-in" : "multiply";
1554
+ tg.fillStyle = rgbToCss(rgb, 1);
1555
+ tg.fillRect(0, 0, iw, ih);
1556
+ if (!fill) {
1557
+ tg.globalCompositeOperation = "destination-in";
1558
+ tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1559
+ }
1560
+ tg.restore();
1561
+ return tc;
1562
+ }
1563
+ renderImage(img, ctx) {
1564
+ const frame = img.frame;
1565
+ if (!frame || frame.width <= 0 || frame.height <= 0) return;
1566
+ const srcImg = frame.source.image;
1567
+ const ox = img.displayOriginX, oy = img.displayOriginY;
1568
+ let sx = frame.cutX, sy = frame.cutY, sw = frame.cutWidth, sh = frame.cutHeight;
1569
+ let dx = -ox, dy = -oy, dw = img.width, dh = img.height;
1570
+ if (img.isCropped) {
1571
+ const rx = Math.max(0, Math.min(frame.width, img.cropX));
1572
+ const ry = Math.max(0, Math.min(frame.height, img.cropY));
1573
+ const rw = Math.max(0, Math.min(frame.width - rx, img.cropW));
1574
+ const rh = Math.max(0, Math.min(frame.height - ry, img.cropH));
1575
+ if (rw <= 0 || rh <= 0) return;
1576
+ sx += rx;
1577
+ sy += ry;
1578
+ sw = rw;
1579
+ sh = rh;
1580
+ dx = -ox + rx;
1581
+ dy = -oy + ry;
1582
+ dw = rw;
1583
+ dh = rh;
1584
+ }
1585
+ if (img.pixelAA) {
1586
+ const m = ctx.getTransform();
1587
+ const dispW = Math.hypot(m.a * dw, m.b * dw);
1588
+ const dispH = Math.hypot(m.c * dh, m.d * dh);
1589
+ const sxRatio = dispW / Math.max(1, sw);
1590
+ const syRatio = dispH / Math.max(1, sh);
1591
+ ctx.imageSmoothingEnabled = !(sxRatio >= .98 && syRatio >= .98 && Math.abs(sxRatio - Math.round(sxRatio)) <= .02 && Math.abs(syRatio - Math.round(syRatio)) <= .02);
1592
+ } else ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1593
+ if (img.flipX || img.flipY) {
1594
+ ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
1595
+ ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
1596
+ }
1597
+ if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
1598
+ const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL);
1599
+ if (tinted) {
1600
+ ctx.drawImage(tinted, 0, 0, Math.ceil(sw), Math.ceil(sh), dx, dy, dw, dh);
1601
+ this.curDrawCalls++;
1602
+ this.curQuads++;
1603
+ this.curTris += 2;
1604
+ return;
1605
+ }
1606
+ }
1607
+ ctx.drawImage(srcImg, sx, sy, sw, sh, dx, dy, dw, dh);
1608
+ this.curDrawCalls++;
1609
+ this.curQuads++;
1610
+ this.curTris += 2;
1611
+ }
1612
+ renderNineSlice(ns, ctx) {
1613
+ const frame = ns.frame;
1614
+ if (!frame || frame.width <= 0 || frame.height <= 0 || ns.width <= 0 || ns.height <= 0) return;
1615
+ const srcImg = frame.source.image;
1616
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1617
+ const w = ns.width, h = ns.height;
1618
+ const ox = ns.displayOriginX, oy = ns.displayOriginY;
1619
+ let L = ns.leftWidth, R = ns.rightWidth, T = ns.topHeight, B = ns.bottomHeight;
1620
+ if (L + R > w && L + R > 0) {
1621
+ const s = w / (L + R);
1622
+ L *= s;
1623
+ R *= s;
1624
+ }
1625
+ if (T + B > h && T + B > 0) {
1626
+ const s = h / (T + B);
1627
+ T *= s;
1628
+ B *= s;
1629
+ }
1630
+ const sxs = [
1631
+ frame.cutX,
1632
+ frame.cutX + ns.leftWidth,
1633
+ frame.cutX + frame.cutWidth - ns.rightWidth,
1634
+ frame.cutX + frame.cutWidth
1635
+ ];
1636
+ const sys = [
1637
+ frame.cutY,
1638
+ frame.cutY + ns.topHeight,
1639
+ frame.cutY + frame.cutHeight - ns.bottomHeight,
1640
+ frame.cutY + frame.cutHeight
1641
+ ];
1642
+ const dxs = [
1643
+ -ox,
1644
+ -ox + L,
1645
+ -ox + w - R,
1646
+ -ox + w
1647
+ ];
1648
+ const dys = [
1649
+ -oy,
1650
+ -oy + T,
1651
+ -oy + h - B,
1652
+ -oy + h
1653
+ ];
1654
+ let patches = 0;
1655
+ for (let r = 0; r < 3; r++) {
1656
+ const sh = sys[r + 1] - sys[r], dh = dys[r + 1] - dys[r];
1657
+ if (sh <= 0 || dh <= 0) continue;
1658
+ for (let c = 0; c < 3; c++) {
1659
+ const sw = sxs[c + 1] - sxs[c], dw = dxs[c + 1] - dxs[c];
1660
+ if (sw <= 0 || dw <= 0) continue;
1661
+ ctx.drawImage(srcImg, sxs[c], sys[r], sw, sh, dxs[c], dys[r], dw, dh);
1662
+ patches++;
1663
+ }
1664
+ }
1665
+ if (patches > 0) {
1666
+ this.curDrawCalls++;
1667
+ this.curQuads += patches;
1668
+ this.curTris += patches * 2;
1669
+ }
1670
+ }
1671
+ renderTileSprite(ts, ctx) {
1672
+ const frame = ts.frame;
1673
+ if (!frame || frame.width <= 0 || frame.height <= 0 || ts.width <= 0 || ts.height <= 0) return;
1674
+ const srcImg = frame.source.image;
1675
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1676
+ const ox = ts.displayOriginX, oy = ts.displayOriginY;
1677
+ const fw = frame.width, fh = frame.height;
1678
+ const offX = (ts.tilePositionX % fw + fw) % fw;
1679
+ const offY = (ts.tilePositionY % fh + fh) % fh;
1680
+ let drawSrc = srcImg;
1681
+ let baseSx = frame.cutX, baseSy = frame.cutY;
1682
+ if (ts.isTinted && ts.tint !== 0xffffff) {
1683
+ const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false);
1684
+ if (tinted) {
1685
+ drawSrc = tinted;
1686
+ baseSx = 0;
1687
+ baseSy = 0;
1688
+ }
1689
+ }
1690
+ let tiles = 0;
1691
+ for (let y = 0; y < ts.height;) {
1692
+ const ty = (y + offY) % fh;
1693
+ const dh = Math.min(fh - ty, ts.height - y);
1694
+ for (let x = 0; x < ts.width;) {
1695
+ const tx = (x + offX) % fw;
1696
+ const dw = Math.min(fw - tx, ts.width - x);
1697
+ ctx.drawImage(drawSrc, baseSx + tx, baseSy + ty, dw, dh, -ox + x, -oy + y, dw, dh);
1698
+ tiles++;
1699
+ x += dw;
1700
+ }
1701
+ y += dh;
1702
+ }
1703
+ if (tiles > 0) {
1704
+ this.curDrawCalls++;
1705
+ this.curQuads += tiles;
1706
+ this.curTris += tiles * 2;
1707
+ }
1708
+ }
1709
+ renderRectangle(r, ctx) {
1710
+ if (r.fillAlpha <= 0 || r.width <= 0 || r.height <= 0) return;
1711
+ ctx.fillStyle = rgbToCss(r.fillColor, r.fillAlpha);
1712
+ ctx.fillRect(-r.displayOriginX, -r.displayOriginY, r.width, r.height);
1713
+ this.curDrawCalls++;
1714
+ this.curQuads++;
1715
+ this.curTris += 2;
1716
+ }
1717
+ renderPanel(p, ctx) {
1718
+ const w = p.width, h = p.height;
1719
+ if (w <= 0 || h <= 0) return;
1720
+ this.curDrawCalls++;
1721
+ this.curQuads++;
1722
+ this.curTris += 2;
1723
+ this.curSdfPanels++;
1724
+ const x = -p.displayOriginX, y = -p.displayOriginY;
1725
+ const bw = Math.max(0, Math.min(p.borderWidth, Math.min(w, h) * .5));
1726
+ if (bw > 0 && p.openSide !== 0) {
1727
+ if (p.borderAlpha > 0) {
1728
+ perCornerRoundedRectPath(ctx, x, y, w, h, p.radiusTL, p.radiusTR, p.radiusBR, p.radiusBL);
1729
+ ctx.fillStyle = rgbToCss(p.borderColor, p.borderAlpha);
1730
+ ctx.fill();
1731
+ }
1732
+ if (p.fillAlpha > 0) {
1733
+ const ix = p.openSide > 0 ? x : x + bw;
1734
+ const iw = Math.max(0, w - bw);
1735
+ const ih = Math.max(0, h - bw * 2);
1736
+ if (iw > 0 && ih > 0) {
1737
+ const irTL = p.openSide > 0 ? 0 : Math.max(0, p.radiusTL - bw);
1738
+ const irTR = p.openSide < 0 ? 0 : Math.max(0, p.radiusTR - bw);
1739
+ const irBR = p.openSide < 0 ? 0 : Math.max(0, p.radiusBR - bw);
1740
+ const irBL = p.openSide > 0 ? 0 : Math.max(0, p.radiusBL - bw);
1741
+ perCornerRoundedRectPath(ctx, ix, y + bw, iw, ih, irTL, irTR, irBR, irBL);
1742
+ ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
1743
+ ctx.fill();
1744
+ paintInnerTopGlint2D(ctx, ix, y + bw, iw, ih, p);
1745
+ }
1746
+ }
1747
+ return;
1748
+ }
1749
+ if (p.fillAlpha > 0) {
1750
+ const iw = w - bw * 2, ih = h - bw * 2;
1751
+ if (iw > 0 && ih > 0) {
1752
+ const irTL = Math.max(0, p.radiusTL - bw);
1753
+ const irTR = Math.max(0, p.radiusTR - bw);
1754
+ const irBR = Math.max(0, p.radiusBR - bw);
1755
+ const irBL = Math.max(0, p.radiusBL - bw);
1756
+ perCornerRoundedRectPath(ctx, x + bw, y + bw, iw, ih, irTL, irTR, irBR, irBL);
1757
+ ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
1758
+ ctx.fill();
1759
+ paintInnerTopGlint2D(ctx, x + bw, y + bw, iw, ih, p);
1760
+ }
1761
+ }
1762
+ if (bw > 0 && p.borderAlpha > 0) {
1763
+ const half = bw * .5;
1764
+ 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));
1765
+ ctx.lineWidth = bw;
1766
+ ctx.strokeStyle = rgbToCss(p.borderColor, p.borderAlpha);
1767
+ ctx.stroke();
1768
+ }
1769
+ }
1770
+ renderMsdfIcon(ic, ctx) {
1771
+ const w = ic.width, h = ic.height;
1772
+ if (w <= 0 || h <= 0 || !ic.svgPath || typeof Path2D !== "function") return;
1773
+ let p2d = this.pathCache.get(ic.svgPath);
1774
+ if (!p2d) {
1775
+ p2d = new Path2D(ic.svgPath);
1776
+ this.pathCache.set(ic.svgPath, p2d);
1777
+ }
1778
+ const view = Math.max(1, ic.viewSize);
1779
+ ctx.save();
1780
+ ctx.translate(-ic.displayOriginX, -ic.displayOriginY);
1781
+ ctx.scale(w / view, h / view);
1782
+ if (ic.outlineWidth > 0 && ic.outlineAlpha > 0) {
1783
+ ctx.lineJoin = "round";
1784
+ ctx.lineCap = "round";
1785
+ ctx.lineWidth = ic.outlineWidth * (view / Math.max(1, Math.min(w, h))) * 2;
1786
+ ctx.strokeStyle = rgbToCss(ic.outlineColor, ic.outlineAlpha);
1787
+ ctx.stroke(p2d);
1788
+ }
1789
+ if (ic.fillAlpha > 0) {
1790
+ ctx.fillStyle = rgbToCss(ic.fillColor, ic.fillAlpha);
1791
+ ctx.fill(p2d);
1792
+ }
1793
+ ctx.restore();
1794
+ this.curDrawCalls++;
1795
+ this.curQuads++;
1796
+ this.curTris += 2;
1797
+ this.curMsdfIcons++;
1798
+ }
1799
+ renderText(txt, ctx) {
1800
+ const cv = txt.ensureCanvas();
1801
+ if (!cv || txt.width <= 0 || txt.height <= 0) return;
1802
+ ctx.imageSmoothingEnabled = true;
1803
+ const ox = txt.displayOriginX, oy = txt.displayOriginY;
1804
+ if (!txt.isCropped) {
1805
+ ctx.drawImage(cv, 0, 0, cv.width, cv.height, -ox, -oy, txt.width, txt.height);
1806
+ this.curDrawCalls++;
1807
+ this.curQuads++;
1808
+ this.curTris += 2;
1809
+ return;
1810
+ }
1811
+ const fw = txt.frame.cutWidth || cv.width || 1;
1812
+ const fh = txt.frame.cutHeight || cv.height || 1;
1813
+ const rx = Math.max(0, Math.min(fw, txt.cropX));
1814
+ const ry = Math.max(0, Math.min(fh, txt.cropY));
1815
+ const rw = Math.max(0, Math.min(fw - rx, txt.cropW));
1816
+ const rh = Math.max(0, Math.min(fh - ry, txt.cropH));
1817
+ if (rw <= 0 || rh <= 0) return;
1818
+ const resX = fw / txt.width;
1819
+ const resY = fh / txt.height;
1820
+ ctx.drawImage(cv, rx, ry, rw, rh, -ox + rx / resX, -oy + ry / resY, rw / resX, rh / resY);
1821
+ this.curDrawCalls++;
1822
+ this.curQuads++;
1823
+ this.curTris += 2;
1824
+ }
1825
+ renderGraphics(g, ctx) {
1826
+ if (g.opLen === 0) return;
1827
+ this.curDrawCalls++;
1828
+ let fillRgb = WHITE_RGB, fillA = 1;
1829
+ let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
1830
+ const ops = g.ops;
1831
+ const nums = g.nums;
1832
+ let ni = 0;
1833
+ for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
1834
+ case 22:
1835
+ ctx.save();
1836
+ break;
1837
+ case 23:
1838
+ ctx.restore();
1839
+ break;
1840
+ case 1:
1841
+ fillRgb = nums[ni++];
1842
+ fillA = nums[ni++];
1843
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
1844
+ break;
1845
+ case 2:
1846
+ lineW = nums[ni++];
1847
+ lineRgb = nums[ni++];
1848
+ lineA = nums[ni++];
1849
+ ctx.lineWidth = lineW;
1850
+ ctx.strokeStyle = rgbToCss(lineRgb, lineA);
1851
+ break;
1852
+ case 3: {
1853
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1854
+ if (fillA > 0) ctx.fillRect(x, y, w, h);
1855
+ break;
1856
+ }
1857
+ case 4: {
1858
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1859
+ if (lineA > 0 && lineW > 0) ctx.strokeRect(x, y, w, h);
1860
+ break;
1861
+ }
1862
+ case 5: {
1863
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1864
+ if (fillA > 0 && w > 0 && h > 0) {
1865
+ roundedRectPath(ctx, x, y, w, h, r);
1866
+ ctx.fill();
1867
+ }
1868
+ break;
1869
+ }
1870
+ case 6: {
1871
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1872
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
1873
+ roundedRectPath(ctx, x, y, w, h, r);
1874
+ ctx.stroke();
1875
+ }
1876
+ break;
1877
+ }
1878
+ case 7: {
1879
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1880
+ if (fillA > 0 && r > 0) {
1881
+ ctx.beginPath();
1882
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
1883
+ ctx.fill();
1884
+ }
1885
+ break;
1886
+ }
1887
+ case 8: {
1888
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1889
+ if (lineA > 0 && lineW > 0 && r > 0) {
1890
+ ctx.beginPath();
1891
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
1892
+ ctx.stroke();
1893
+ }
1894
+ break;
1895
+ }
1896
+ case 9: {
1897
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1898
+ if (fillA > 0 && w > 0 && h > 0) {
1899
+ ctx.beginPath();
1900
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1901
+ ctx.fill();
1902
+ }
1903
+ break;
1904
+ }
1905
+ case 10: {
1906
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1907
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
1908
+ ctx.beginPath();
1909
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1910
+ ctx.stroke();
1911
+ }
1912
+ break;
1913
+ }
1914
+ case 11: {
1915
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1916
+ if (fillA > 0) {
1917
+ ctx.beginPath();
1918
+ ctx.moveTo(x0, y0);
1919
+ ctx.lineTo(x1, y1);
1920
+ ctx.lineTo(x2, y2);
1921
+ ctx.closePath();
1922
+ ctx.fill();
1923
+ }
1924
+ break;
1925
+ }
1926
+ case 12: {
1927
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1928
+ if (lineA > 0 && lineW > 0) {
1929
+ ctx.beginPath();
1930
+ ctx.moveTo(x0, y0);
1931
+ ctx.lineTo(x1, y1);
1932
+ ctx.lineTo(x2, y2);
1933
+ ctx.closePath();
1934
+ ctx.stroke();
1935
+ }
1936
+ break;
1937
+ }
1938
+ case 13: {
1939
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++];
1940
+ if (lineA > 0 && lineW > 0) {
1941
+ ctx.beginPath();
1942
+ ctx.moveTo(x0, y0);
1943
+ ctx.lineTo(x1, y1);
1944
+ ctx.stroke();
1945
+ }
1946
+ break;
1947
+ }
1948
+ case 14:
1949
+ ctx.beginPath();
1950
+ break;
1951
+ case 15:
1952
+ ctx.moveTo(nums[ni++], nums[ni++]);
1953
+ break;
1954
+ case 16:
1955
+ ctx.lineTo(nums[ni++], nums[ni++]);
1956
+ break;
1957
+ case 17: {
1958
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
1959
+ ctx.arc(cx, cy, r, a0, a1, ccw);
1960
+ break;
1961
+ }
1962
+ case 18: {
1963
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
1964
+ ctx.moveTo(cx, cy);
1965
+ ctx.arc(cx, cy, r, a0, a1, ccw);
1966
+ ctx.closePath();
1967
+ break;
1968
+ }
1969
+ case 19:
1970
+ ctx.closePath();
1971
+ break;
1972
+ case 20:
1973
+ if (fillA > 0) ctx.fill();
1974
+ break;
1975
+ case 21: if (lineA > 0 && lineW > 0) ctx.stroke();
1976
+ }
1977
+ }
1978
+ renderInstancedBatch(batch, ctx) {
1979
+ const cols = batch.columns;
1980
+ if (!cols || cols.count <= 0) return;
1981
+ const baseAlpha = ctx.globalAlpha;
1982
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
1983
+ let vis = 0;
1984
+ let lastTintImg = null;
1985
+ let lastCutX = -1, lastCutY = -1, lastCutW = -1, lastCutH = -1, lastRgb = -1;
1986
+ let cachedTinted = null;
1987
+ for (let i = 0; i < n; i++) {
1988
+ const s = cols.order ? cols.order[i] : i;
1989
+ const a = baseAlpha * (cols.alpha ? cols.alpha[s] : 1);
1990
+ if (a <= 0) continue;
1991
+ const fr = cols.frameIndex ? batch.texture.get(cols.frameIndex[s]) : batch.defaultFrame;
1992
+ if (!fr || fr.width <= 0 || fr.height <= 0) continue;
1993
+ const sx = cols.scaleX ? cols.scaleX[s] : 1;
1994
+ const sy = cols.scaleY ? cols.scaleY[s] : sx;
1995
+ const rot = cols.rotation ? cols.rotation[s] : 0;
1996
+ const rgb = cols.tint ? cols.tint[s] ?? 0xffffff : WHITE_RGB;
1997
+ const px = cols.x[s];
1998
+ const py = cols.y[s];
1999
+ const dx = -fr.width * batch.originX;
2000
+ const dy = -fr.height * batch.originY;
2001
+ const srcImg = fr.source.image;
2002
+ ctx.save();
2003
+ ctx.globalAlpha = a;
2004
+ ctx.imageSmoothingEnabled = fr.source.filterMode !== FilterMode.NEAREST;
2005
+ ctx.translate(px, py);
2006
+ if (rot !== 0) ctx.rotate(rot);
2007
+ if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
2008
+ if (rgb !== 0xffffff && fr.cutWidth > 0 && fr.cutHeight > 0) {
2009
+ if (srcImg !== lastTintImg || fr.cutX !== lastCutX || fr.cutY !== lastCutY || fr.cutWidth !== lastCutW || fr.cutHeight !== lastCutH || rgb !== lastRgb) {
2010
+ cachedTinted = this.getTintedSource(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, rgb, false);
2011
+ lastTintImg = srcImg;
2012
+ lastCutX = fr.cutX;
2013
+ lastCutY = fr.cutY;
2014
+ lastCutW = fr.cutWidth;
2015
+ lastCutH = fr.cutHeight;
2016
+ lastRgb = rgb;
2017
+ }
2018
+ if (cachedTinted) {
2019
+ ctx.drawImage(cachedTinted, 0, 0, Math.ceil(fr.cutWidth), Math.ceil(fr.cutHeight), dx, dy, fr.width, fr.height);
2020
+ ctx.restore();
2021
+ vis++;
2022
+ continue;
2023
+ }
2024
+ }
2025
+ ctx.drawImage(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, dx, dy, fr.width, fr.height);
2026
+ ctx.restore();
2027
+ vis++;
2028
+ }
2029
+ ctx.globalAlpha = baseAlpha;
2030
+ if (vis > 0) {
2031
+ this.curDrawCalls++;
2032
+ this.curFlushes++;
2033
+ this.curQuads += vis;
2034
+ this.curTris += vis * 2;
2035
+ }
2036
+ }
2037
+ /** Create an offscreen Canvas2D render target backed by a registered Stage `Texture` (`key`). */
2038
+ createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
2039
+ const w = Math.max(1, Math.round(width));
2040
+ const h = Math.max(1, Math.round(height));
2041
+ const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
2042
+ if (!doc) throw new Error("Stage: ownerDocument unavailable for Canvas2D RenderTarget");
2043
+ const rtCanvas = doc.createElement("canvas");
2044
+ rtCanvas.width = w;
2045
+ rtCanvas.height = h;
2046
+ const tex = textures.addCanvas(key, rtCanvas);
2047
+ if (!tex) throw new Error("Stage: failed to register Canvas2D RenderTarget texture");
2048
+ tex.source[0]?.setFilter(filterMode);
2049
+ const rtRenderer = new Renderer2D(rtCanvas, w, h);
2050
+ return {
2051
+ key,
2052
+ width: w,
2053
+ height: h,
2054
+ texture: tex,
2055
+ _rtRenderer: rtRenderer,
2056
+ destroy: () => {
2057
+ rtRenderer.destroy();
2058
+ textures.remove(key);
2059
+ }
2060
+ };
2061
+ }
2062
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node. */
2063
+ renderToTarget(target, content, camera) {
2064
+ const sub = target._rtRenderer;
2065
+ if (!sub) return;
2066
+ if (typeof content === "function") {
2067
+ camera.syncMetrics();
2068
+ content(sub, camera);
2069
+ } else sub.render(content, camera);
2070
+ const src = target.texture.source[0];
2071
+ if (src) src.dirty = true;
2072
+ }
2073
+ renderBlitter(blitter, ctx) {
2074
+ const list = blitter.children.list;
2075
+ const baseAlpha = ctx.globalAlpha;
2076
+ let vis = 0;
2077
+ for (let i = 0; i < list.length; i++) {
2078
+ const bob = list[i];
2079
+ if (!bob.visible || bob.alpha <= 0) continue;
2080
+ const frame = bob.frame;
2081
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2082
+ ctx.globalAlpha = baseAlpha * bob.alpha;
2083
+ ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
2084
+ vis++;
2085
+ }
2086
+ ctx.globalAlpha = baseAlpha;
2087
+ if (vis > 0) {
2088
+ this.curDrawCalls++;
2089
+ this.curQuads += vis;
2090
+ this.curTris += vis * 2;
2091
+ }
2092
+ }
2093
+ renderNode(obj, camera, ctx, isRoot) {
2094
+ if (!obj.visible || obj.alpha <= 0) return;
2095
+ ctx.save();
2096
+ if (isRoot) {
2097
+ if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) {
2098
+ const cm = camera.matrixCombined;
2099
+ ctx.setTransform(cm.a, cm.b, cm.c, cm.d, cm.tx, cm.ty);
2100
+ } else {
2101
+ const z = camera.zoom || 1;
2102
+ const ox = camera.width * camera.originX;
2103
+ const oy = camera.height * camera.originY;
2104
+ const sx = camera.roundPixels ? Math.round(camera.scrollX) : camera.scrollX;
2105
+ const sy = camera.roundPixels ? Math.round(camera.scrollY) : camera.scrollY;
2106
+ ctx.setTransform(z, 0, 0, z, (-sx * obj.scrollFactorX - ox) * z + ox + camera.x, (-sy * obj.scrollFactorY - oy) * z + oy + camera.y);
2107
+ }
2108
+ }
2109
+ if (obj.type === "Container") {
2110
+ const mask = obj.clipMask;
2111
+ if (mask && mask.active && mask.graphics?.maskRect) {
2112
+ const mr = mask.graphics.maskRect;
2113
+ const mw = mask.graphics.getWorldTransformMatrix(this.maskWorldMat);
2114
+ const cm = camera.matrixCombined;
2115
+ const wx0 = mw.a * mr.x + mw.c * mr.y + mw.tx;
2116
+ const wy0 = mw.b * mr.x + mw.d * mr.y + mw.ty;
2117
+ const wx1 = mw.a * (mr.x + mr.w) + mw.c * (mr.y + mr.h) + mw.tx;
2118
+ const wy1 = mw.b * (mr.x + mr.w) + mw.d * (mr.y + mr.h) + mw.ty;
2119
+ const sx0 = cm.a * wx0 + cm.c * wy0 + cm.tx;
2120
+ const sy0 = cm.b * wx0 + cm.d * wy0 + cm.ty;
2121
+ const sx1 = cm.a * wx1 + cm.c * wy1 + cm.tx;
2122
+ const sy1 = cm.b * wx1 + cm.d * wy1 + cm.ty;
2123
+ ctx.save();
2124
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
2125
+ ctx.beginPath();
2126
+ ctx.rect(Math.min(sx0, sx1), Math.min(sy0, sy1), Math.abs(sx1 - sx0), Math.abs(sy1 - sy0));
2127
+ ctx.restore();
2128
+ ctx.clip();
2129
+ }
2130
+ }
2131
+ ctx.globalAlpha *= obj.alpha;
2132
+ if (obj.blendMode === BlendModes.ADD) ctx.globalCompositeOperation = "lighter";
2133
+ else if (obj.blendMode === BlendModes.MULTIPLY) ctx.globalCompositeOperation = "multiply";
2134
+ else if (obj.blendMode === BlendModes.SCREEN) ctx.globalCompositeOperation = "screen";
2135
+ if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
2136
+ if (obj.rotation !== 0) ctx.rotate(obj.rotation);
2137
+ if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
2138
+ switch (obj.type) {
2139
+ case "Image":
2140
+ this.renderImage(obj, ctx);
2141
+ break;
2142
+ case "NineSlice":
2143
+ this.renderNineSlice(obj, ctx);
2144
+ break;
2145
+ case "TileSprite":
2146
+ this.renderTileSprite(obj, ctx);
2147
+ break;
2148
+ case "Rectangle":
2149
+ this.renderRectangle(obj, ctx);
2150
+ break;
2151
+ case "Panel":
2152
+ this.renderPanel(obj, ctx);
2153
+ break;
2154
+ case "MsdfIcon":
2155
+ this.renderMsdfIcon(obj, ctx);
2156
+ break;
2157
+ case "Text":
2158
+ this.renderText(obj, ctx);
2159
+ break;
2160
+ case "Graphics":
2161
+ this.renderGraphics(obj, ctx);
2162
+ break;
2163
+ case "Blitter":
2164
+ this.renderBlitter(obj, ctx);
2165
+ break;
2166
+ case "InstancedBatch":
2167
+ this.renderInstancedBatch(obj, ctx);
2168
+ break;
2169
+ case "Container": {
2170
+ const list = obj.sortedChildren();
2171
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
2172
+ break;
2173
+ }
2174
+ }
2175
+ ctx.restore();
2176
+ }
2177
+ render(displayList, camera) {
2178
+ this.curDrawCalls = 0;
2179
+ this.curFlushes = 0;
2180
+ this.curQuads = 0;
2181
+ this.curTris = 0;
2182
+ this.curSdfPanels = 0;
2183
+ this.curMsdfIcons = 0;
2184
+ camera.syncMetrics();
2185
+ const ctx = this.ctx;
2186
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
2187
+ ctx.globalAlpha = 1;
2188
+ ctx.globalCompositeOperation = "source-over";
2189
+ if (camera.transparent) ctx.clearRect(0, 0, this.width, this.height);
2190
+ else {
2191
+ const bg = camera.backgroundColor;
2192
+ ctx.fillStyle = `rgba(${Math.round(bg.r * 255)},${Math.round(bg.g * 255)},${Math.round(bg.b * 255)},${bg.a})`;
2193
+ ctx.fillRect(0, 0, this.width, this.height);
2194
+ }
2195
+ ctx.globalAlpha = camera.alpha;
2196
+ const list = displayList.sorted();
2197
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
2198
+ this.lastStats.drawCalls = this.curDrawCalls;
2199
+ this.lastStats.flushes = this.curFlushes;
2200
+ this.lastStats.quads = this.curQuads;
2201
+ this.lastStats.tris = this.curTris;
2202
+ this.lastStats.sdfPanels = this.curSdfPanels;
2203
+ this.lastStats.sdfDraws = this.curSdfPanels > 0 ? 1 : 0;
2204
+ this.lastStats.msdfIcons = this.curMsdfIcons;
2205
+ this.lastStats.msdfDraws = this.curMsdfIcons > 0 ? 1 : 0;
2206
+ }
2207
+ destroy() {
2208
+ this.removeAllListeners();
2209
+ this.tintCanvas = null;
2210
+ this.tintCtx = null;
2211
+ }
2212
+ };
1433
2213
  // src/renderer-webgpu.ts
1434
2214
  const GPUShaderStage_VERTEX = 1;
1435
2215
  const GPUShaderStage_FRAGMENT = 2;
@@ -2603,6 +3383,42 @@ var RendererWebGpu = class extends EventEmitter {
2603
3383
  this.lastStats.sdfPanels = this.sdfCount;
2604
3384
  this.lastStats.sdfDraws = sdfDraws;
2605
3385
  }
3386
+ /** Create an offscreen render target backed by a registered Stage `Texture` (`key`) for WebGPU passes (e.g. `Lighting2DSystem`). */
3387
+ createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
3388
+ const w = Math.max(1, Math.round(width));
3389
+ const h = Math.max(1, Math.round(height));
3390
+ const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
3391
+ if (!doc) throw new Error("Stage: ownerDocument unavailable for WebGPU RenderTarget");
3392
+ const rtCanvas = doc.createElement("canvas");
3393
+ rtCanvas.width = w;
3394
+ rtCanvas.height = h;
3395
+ const tex = textures.addCanvas(key, rtCanvas);
3396
+ if (!tex) throw new Error("Stage: failed to register WebGPU RenderTarget texture");
3397
+ tex.source[0]?.setFilter(filterMode);
3398
+ const rtRenderer = new Renderer2D(rtCanvas, w, h);
3399
+ return {
3400
+ key,
3401
+ width: w,
3402
+ height: h,
3403
+ texture: tex,
3404
+ _rtRenderer: rtRenderer,
3405
+ destroy: () => {
3406
+ rtRenderer.destroy();
3407
+ textures.remove(key);
3408
+ }
3409
+ };
3410
+ }
3411
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload. */
3412
+ renderToTarget(target, content, camera) {
3413
+ const sub = target._rtRenderer;
3414
+ if (!sub) return;
3415
+ if (typeof content === "function") {
3416
+ camera.syncMetrics();
3417
+ content(sub, camera);
3418
+ } else sub.render(content, camera);
3419
+ const src = target.texture.source[0];
3420
+ if (src) src.dirty = true;
3421
+ }
2606
3422
  destroy() {
2607
3423
  this.removeAllListeners();
2608
3424
  try {
@@ -2616,4 +3432,4 @@ var RendererWebGpu = class extends EventEmitter {
2616
3432
  } catch {}
2617
3433
  }
2618
3434
  };
2619
- export { CustomPassObject as a, Graphics as c, MsdfIconObject as d, NineSliceObject as f, TileSpriteObject as g, TextObject as h, Container as i, ImageObject as l, RectangleObject as m, requestWebGpuDevice as n, DisplayList as o, PanelObject as p, BlitterObject as r, DisplayObject as s, RendererWebGpu as t, InstancedBatchObject as u };
3435
+ export { TileSpriteObject as _, Container as a, DisplayObject as c, InstancedBatchObject as d, MsdfIconObject as f, TextObject as g, RectangleObject as h, BlitterObject as i, Graphics as l, PanelObject as m, requestWebGpuDevice as n, CustomPassObject as o, NineSliceObject as p, Renderer2D as r, DisplayList as s, RendererWebGpu as t, ImageObject as u };