@holtjs/stage 0.4.1 → 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.
@@ -1430,6 +1430,687 @@ 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
+ constructor(canvas, width, height, existingCtx) {
1492
+ super();
1493
+ this.canvas = canvas;
1494
+ this.width = Math.max(1, Math.round(width));
1495
+ this.height = Math.max(1, Math.round(height));
1496
+ const ctx = existingCtx ?? canvas.getContext("2d");
1497
+ if (!ctx) throw new Error("Stage2D: 2D canvas context unavailable");
1498
+ this.ctx = ctx;
1499
+ }
1500
+ stats() {
1501
+ return {
1502
+ drawCalls: 0,
1503
+ flushes: 0,
1504
+ quads: 0,
1505
+ tris: 0,
1506
+ culled: 0,
1507
+ texBinds: 0,
1508
+ batchMaxUnits: 0,
1509
+ sdfPanels: 0,
1510
+ sdfDraws: 0,
1511
+ msdfIcons: 0,
1512
+ msdfDraws: 0,
1513
+ pixelAaIcons: 0,
1514
+ pixelAaDraws: 0
1515
+ };
1516
+ }
1517
+ resize(width, height) {
1518
+ const w = Math.max(1, Math.round(width));
1519
+ const h = Math.max(1, Math.round(height));
1520
+ if (this.width === w && this.height === h && this.canvas.width === w && this.canvas.height === h) return;
1521
+ this.width = w;
1522
+ this.height = h;
1523
+ this.canvas.width = w;
1524
+ this.canvas.height = h;
1525
+ }
1526
+ getTintedSource(img, sx, sy, sw, sh, rgb, fill) {
1527
+ const iw = Math.max(1, Math.ceil(sw));
1528
+ const ih = Math.max(1, Math.ceil(sh));
1529
+ if (!this.tintCanvas) {
1530
+ const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
1531
+ if (!doc) return null;
1532
+ this.tintCanvas = doc.createElement("canvas");
1533
+ this.tintCtx = this.tintCanvas.getContext("2d");
1534
+ }
1535
+ const tc = this.tintCanvas;
1536
+ const tg = this.tintCtx;
1537
+ if (!tc || !tg) return null;
1538
+ if (tc.width < iw || tc.height < ih) {
1539
+ tc.width = Math.max(tc.width, iw);
1540
+ tc.height = Math.max(tc.height, ih);
1541
+ }
1542
+ tg.save();
1543
+ tg.globalCompositeOperation = "source-over";
1544
+ tg.clearRect(0, 0, iw, ih);
1545
+ tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1546
+ tg.globalCompositeOperation = fill ? "source-in" : "multiply";
1547
+ tg.fillStyle = rgbToCss(rgb, 1);
1548
+ tg.fillRect(0, 0, iw, ih);
1549
+ if (!fill) {
1550
+ tg.globalCompositeOperation = "destination-in";
1551
+ tg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1552
+ }
1553
+ tg.restore();
1554
+ return tc;
1555
+ }
1556
+ renderImage(img, ctx) {
1557
+ const frame = img.frame;
1558
+ if (!frame || frame.width <= 0 || frame.height <= 0) return;
1559
+ const srcImg = frame.source.image;
1560
+ const ox = img.displayOriginX, oy = img.displayOriginY;
1561
+ let sx = frame.cutX, sy = frame.cutY, sw = frame.cutWidth, sh = frame.cutHeight;
1562
+ let dx = -ox, dy = -oy, dw = img.width, dh = img.height;
1563
+ if (img.isCropped) {
1564
+ const rx = Math.max(0, Math.min(frame.width, img.cropX));
1565
+ const ry = Math.max(0, Math.min(frame.height, img.cropY));
1566
+ const rw = Math.max(0, Math.min(frame.width - rx, img.cropW));
1567
+ const rh = Math.max(0, Math.min(frame.height - ry, img.cropH));
1568
+ if (rw <= 0 || rh <= 0) return;
1569
+ sx += rx;
1570
+ sy += ry;
1571
+ sw = rw;
1572
+ sh = rh;
1573
+ dx = -ox + rx;
1574
+ dy = -oy + ry;
1575
+ dw = rw;
1576
+ dh = rh;
1577
+ }
1578
+ if (img.pixelAA) {
1579
+ const m = ctx.getTransform();
1580
+ const dispW = Math.hypot(m.a * dw, m.b * dw);
1581
+ const dispH = Math.hypot(m.c * dh, m.d * dh);
1582
+ const sxRatio = dispW / Math.max(1, sw);
1583
+ const syRatio = dispH / Math.max(1, sh);
1584
+ ctx.imageSmoothingEnabled = !(sxRatio >= .98 && syRatio >= .98 && Math.abs(sxRatio - Math.round(sxRatio)) <= .02 && Math.abs(syRatio - Math.round(syRatio)) <= .02);
1585
+ } else ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1586
+ if (img.flipX || img.flipY) {
1587
+ ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
1588
+ ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
1589
+ }
1590
+ if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
1591
+ const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL);
1592
+ if (tinted) {
1593
+ ctx.drawImage(tinted, 0, 0, Math.ceil(sw), Math.ceil(sh), dx, dy, dw, dh);
1594
+ return;
1595
+ }
1596
+ }
1597
+ ctx.drawImage(srcImg, sx, sy, sw, sh, dx, dy, dw, dh);
1598
+ }
1599
+ renderNineSlice(ns, ctx) {
1600
+ const frame = ns.frame;
1601
+ if (!frame || frame.width <= 0 || frame.height <= 0 || ns.width <= 0 || ns.height <= 0) return;
1602
+ const srcImg = frame.source.image;
1603
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1604
+ const w = ns.width, h = ns.height;
1605
+ const ox = ns.displayOriginX, oy = ns.displayOriginY;
1606
+ let L = ns.leftWidth, R = ns.rightWidth, T = ns.topHeight, B = ns.bottomHeight;
1607
+ if (L + R > w && L + R > 0) {
1608
+ const s = w / (L + R);
1609
+ L *= s;
1610
+ R *= s;
1611
+ }
1612
+ if (T + B > h && T + B > 0) {
1613
+ const s = h / (T + B);
1614
+ T *= s;
1615
+ B *= s;
1616
+ }
1617
+ const sxs = [
1618
+ frame.cutX,
1619
+ frame.cutX + ns.leftWidth,
1620
+ frame.cutX + frame.cutWidth - ns.rightWidth,
1621
+ frame.cutX + frame.cutWidth
1622
+ ];
1623
+ const sys = [
1624
+ frame.cutY,
1625
+ frame.cutY + ns.topHeight,
1626
+ frame.cutY + frame.cutHeight - ns.bottomHeight,
1627
+ frame.cutY + frame.cutHeight
1628
+ ];
1629
+ const dxs = [
1630
+ -ox,
1631
+ -ox + L,
1632
+ -ox + w - R,
1633
+ -ox + w
1634
+ ];
1635
+ const dys = [
1636
+ -oy,
1637
+ -oy + T,
1638
+ -oy + h - B,
1639
+ -oy + h
1640
+ ];
1641
+ for (let r = 0; r < 3; r++) {
1642
+ const sh = sys[r + 1] - sys[r], dh = dys[r + 1] - dys[r];
1643
+ if (sh <= 0 || dh <= 0) continue;
1644
+ for (let c = 0; c < 3; c++) {
1645
+ const sw = sxs[c + 1] - sxs[c], dw = dxs[c + 1] - dxs[c];
1646
+ if (sw <= 0 || dw <= 0) continue;
1647
+ ctx.drawImage(srcImg, sxs[c], sys[r], sw, sh, dxs[c], dys[r], dw, dh);
1648
+ }
1649
+ }
1650
+ }
1651
+ renderTileSprite(ts, ctx) {
1652
+ const frame = ts.frame;
1653
+ if (!frame || frame.width <= 0 || frame.height <= 0 || ts.width <= 0 || ts.height <= 0) return;
1654
+ const srcImg = frame.source.image;
1655
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1656
+ const ox = ts.displayOriginX, oy = ts.displayOriginY;
1657
+ const fw = frame.width, fh = frame.height;
1658
+ const offX = (ts.tilePositionX % fw + fw) % fw;
1659
+ const offY = (ts.tilePositionY % fh + fh) % fh;
1660
+ let drawSrc = srcImg;
1661
+ let baseSx = frame.cutX, baseSy = frame.cutY;
1662
+ if (ts.isTinted && ts.tint !== 0xffffff) {
1663
+ const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false);
1664
+ if (tinted) {
1665
+ drawSrc = tinted;
1666
+ baseSx = 0;
1667
+ baseSy = 0;
1668
+ }
1669
+ }
1670
+ for (let y = 0; y < ts.height;) {
1671
+ const ty = (y + offY) % fh;
1672
+ const dh = Math.min(fh - ty, ts.height - y);
1673
+ for (let x = 0; x < ts.width;) {
1674
+ const tx = (x + offX) % fw;
1675
+ const dw = Math.min(fw - tx, ts.width - x);
1676
+ ctx.drawImage(drawSrc, baseSx + tx, baseSy + ty, dw, dh, -ox + x, -oy + y, dw, dh);
1677
+ x += dw;
1678
+ }
1679
+ y += dh;
1680
+ }
1681
+ }
1682
+ renderRectangle(r, ctx) {
1683
+ if (r.fillAlpha <= 0 || r.width <= 0 || r.height <= 0) return;
1684
+ ctx.fillStyle = rgbToCss(r.fillColor, r.fillAlpha);
1685
+ ctx.fillRect(-r.displayOriginX, -r.displayOriginY, r.width, r.height);
1686
+ }
1687
+ renderPanel(p, ctx) {
1688
+ const w = p.width, h = p.height;
1689
+ if (w <= 0 || h <= 0) return;
1690
+ const x = -p.displayOriginX, y = -p.displayOriginY;
1691
+ const bw = Math.max(0, Math.min(p.borderWidth, Math.min(w, h) * .5));
1692
+ if (bw > 0 && p.openSide !== 0) {
1693
+ if (p.borderAlpha > 0) {
1694
+ perCornerRoundedRectPath(ctx, x, y, w, h, p.radiusTL, p.radiusTR, p.radiusBR, p.radiusBL);
1695
+ ctx.fillStyle = rgbToCss(p.borderColor, p.borderAlpha);
1696
+ ctx.fill();
1697
+ }
1698
+ if (p.fillAlpha > 0) {
1699
+ const ix = p.openSide > 0 ? x : x + bw;
1700
+ const iw = Math.max(0, w - bw);
1701
+ const ih = Math.max(0, h - bw * 2);
1702
+ if (iw > 0 && ih > 0) {
1703
+ const irTL = p.openSide > 0 ? 0 : Math.max(0, p.radiusTL - bw);
1704
+ const irTR = p.openSide < 0 ? 0 : Math.max(0, p.radiusTR - bw);
1705
+ const irBR = p.openSide < 0 ? 0 : Math.max(0, p.radiusBR - bw);
1706
+ const irBL = p.openSide > 0 ? 0 : Math.max(0, p.radiusBL - bw);
1707
+ perCornerRoundedRectPath(ctx, ix, y + bw, iw, ih, irTL, irTR, irBR, irBL);
1708
+ ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
1709
+ ctx.fill();
1710
+ paintInnerTopGlint2D(ctx, ix, y + bw, iw, ih, p);
1711
+ }
1712
+ }
1713
+ return;
1714
+ }
1715
+ if (p.fillAlpha > 0) {
1716
+ const iw = w - bw * 2, ih = h - bw * 2;
1717
+ if (iw > 0 && ih > 0) {
1718
+ const irTL = Math.max(0, p.radiusTL - bw);
1719
+ const irTR = Math.max(0, p.radiusTR - bw);
1720
+ const irBR = Math.max(0, p.radiusBR - bw);
1721
+ const irBL = Math.max(0, p.radiusBL - bw);
1722
+ perCornerRoundedRectPath(ctx, x + bw, y + bw, iw, ih, irTL, irTR, irBR, irBL);
1723
+ ctx.fillStyle = rgbToCss(p.fillColor, p.fillAlpha);
1724
+ ctx.fill();
1725
+ paintInnerTopGlint2D(ctx, x + bw, y + bw, iw, ih, p);
1726
+ }
1727
+ }
1728
+ if (bw > 0 && p.borderAlpha > 0) {
1729
+ const half = bw * .5;
1730
+ 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));
1731
+ ctx.lineWidth = bw;
1732
+ ctx.strokeStyle = rgbToCss(p.borderColor, p.borderAlpha);
1733
+ ctx.stroke();
1734
+ }
1735
+ }
1736
+ renderMsdfIcon(ic, ctx) {
1737
+ const w = ic.width, h = ic.height;
1738
+ if (w <= 0 || h <= 0 || !ic.svgPath || typeof Path2D !== "function") return;
1739
+ let p2d = this.pathCache.get(ic.svgPath);
1740
+ if (!p2d) {
1741
+ p2d = new Path2D(ic.svgPath);
1742
+ this.pathCache.set(ic.svgPath, p2d);
1743
+ }
1744
+ const view = Math.max(1, ic.viewSize);
1745
+ ctx.save();
1746
+ ctx.translate(-ic.displayOriginX, -ic.displayOriginY);
1747
+ ctx.scale(w / view, h / view);
1748
+ if (ic.outlineWidth > 0 && ic.outlineAlpha > 0) {
1749
+ ctx.lineJoin = "round";
1750
+ ctx.lineCap = "round";
1751
+ ctx.lineWidth = ic.outlineWidth * (view / Math.max(1, Math.min(w, h))) * 2;
1752
+ ctx.strokeStyle = rgbToCss(ic.outlineColor, ic.outlineAlpha);
1753
+ ctx.stroke(p2d);
1754
+ }
1755
+ if (ic.fillAlpha > 0) {
1756
+ ctx.fillStyle = rgbToCss(ic.fillColor, ic.fillAlpha);
1757
+ ctx.fill(p2d);
1758
+ }
1759
+ ctx.restore();
1760
+ }
1761
+ renderText(txt, ctx) {
1762
+ const cv = txt.ensureCanvas();
1763
+ if (!cv || txt.width <= 0 || txt.height <= 0) return;
1764
+ ctx.imageSmoothingEnabled = true;
1765
+ const ox = txt.displayOriginX, oy = txt.displayOriginY;
1766
+ if (!txt.isCropped) {
1767
+ ctx.drawImage(cv, 0, 0, cv.width, cv.height, -ox, -oy, txt.width, txt.height);
1768
+ return;
1769
+ }
1770
+ const fw = txt.frame.cutWidth || cv.width || 1;
1771
+ const fh = txt.frame.cutHeight || cv.height || 1;
1772
+ const rx = Math.max(0, Math.min(fw, txt.cropX));
1773
+ const ry = Math.max(0, Math.min(fh, txt.cropY));
1774
+ const rw = Math.max(0, Math.min(fw - rx, txt.cropW));
1775
+ const rh = Math.max(0, Math.min(fh - ry, txt.cropH));
1776
+ if (rw <= 0 || rh <= 0) return;
1777
+ const resX = fw / txt.width;
1778
+ const resY = fh / txt.height;
1779
+ ctx.drawImage(cv, rx, ry, rw, rh, -ox + rx / resX, -oy + ry / resY, rw / resX, rh / resY);
1780
+ }
1781
+ renderGraphics(g, ctx) {
1782
+ if (g.opLen === 0) return;
1783
+ let fillRgb = WHITE_RGB, fillA = 1;
1784
+ let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
1785
+ const ops = g.ops;
1786
+ const nums = g.nums;
1787
+ let ni = 0;
1788
+ for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
1789
+ case 22:
1790
+ ctx.save();
1791
+ break;
1792
+ case 23:
1793
+ ctx.restore();
1794
+ break;
1795
+ case 1:
1796
+ fillRgb = nums[ni++];
1797
+ fillA = nums[ni++];
1798
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
1799
+ break;
1800
+ case 2:
1801
+ lineW = nums[ni++];
1802
+ lineRgb = nums[ni++];
1803
+ lineA = nums[ni++];
1804
+ ctx.lineWidth = lineW;
1805
+ ctx.strokeStyle = rgbToCss(lineRgb, lineA);
1806
+ break;
1807
+ case 3: {
1808
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1809
+ if (fillA > 0) ctx.fillRect(x, y, w, h);
1810
+ break;
1811
+ }
1812
+ case 4: {
1813
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1814
+ if (lineA > 0 && lineW > 0) ctx.strokeRect(x, y, w, h);
1815
+ break;
1816
+ }
1817
+ case 5: {
1818
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1819
+ if (fillA > 0 && w > 0 && h > 0) {
1820
+ roundedRectPath(ctx, x, y, w, h, r);
1821
+ ctx.fill();
1822
+ }
1823
+ break;
1824
+ }
1825
+ case 6: {
1826
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1827
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
1828
+ roundedRectPath(ctx, x, y, w, h, r);
1829
+ ctx.stroke();
1830
+ }
1831
+ break;
1832
+ }
1833
+ case 7: {
1834
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1835
+ if (fillA > 0 && r > 0) {
1836
+ ctx.beginPath();
1837
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
1838
+ ctx.fill();
1839
+ }
1840
+ break;
1841
+ }
1842
+ case 8: {
1843
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1844
+ if (lineA > 0 && lineW > 0 && r > 0) {
1845
+ ctx.beginPath();
1846
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
1847
+ ctx.stroke();
1848
+ }
1849
+ break;
1850
+ }
1851
+ case 9: {
1852
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1853
+ if (fillA > 0 && w > 0 && h > 0) {
1854
+ ctx.beginPath();
1855
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1856
+ ctx.fill();
1857
+ }
1858
+ break;
1859
+ }
1860
+ case 10: {
1861
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1862
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
1863
+ ctx.beginPath();
1864
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1865
+ ctx.stroke();
1866
+ }
1867
+ break;
1868
+ }
1869
+ case 11: {
1870
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1871
+ if (fillA > 0) {
1872
+ ctx.beginPath();
1873
+ ctx.moveTo(x0, y0);
1874
+ ctx.lineTo(x1, y1);
1875
+ ctx.lineTo(x2, y2);
1876
+ ctx.closePath();
1877
+ ctx.fill();
1878
+ }
1879
+ break;
1880
+ }
1881
+ case 12: {
1882
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1883
+ if (lineA > 0 && lineW > 0) {
1884
+ ctx.beginPath();
1885
+ ctx.moveTo(x0, y0);
1886
+ ctx.lineTo(x1, y1);
1887
+ ctx.lineTo(x2, y2);
1888
+ ctx.closePath();
1889
+ ctx.stroke();
1890
+ }
1891
+ break;
1892
+ }
1893
+ case 13: {
1894
+ const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++];
1895
+ if (lineA > 0 && lineW > 0) {
1896
+ ctx.beginPath();
1897
+ ctx.moveTo(x0, y0);
1898
+ ctx.lineTo(x1, y1);
1899
+ ctx.stroke();
1900
+ }
1901
+ break;
1902
+ }
1903
+ case 14:
1904
+ ctx.beginPath();
1905
+ break;
1906
+ case 15:
1907
+ ctx.moveTo(nums[ni++], nums[ni++]);
1908
+ break;
1909
+ case 16:
1910
+ ctx.lineTo(nums[ni++], nums[ni++]);
1911
+ break;
1912
+ case 17: {
1913
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
1914
+ ctx.arc(cx, cy, r, a0, a1, ccw);
1915
+ break;
1916
+ }
1917
+ case 18: {
1918
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++], a0 = nums[ni++], a1 = nums[ni++], ccw = nums[ni++] > .5;
1919
+ ctx.moveTo(cx, cy);
1920
+ ctx.arc(cx, cy, r, a0, a1, ccw);
1921
+ ctx.closePath();
1922
+ break;
1923
+ }
1924
+ case 19:
1925
+ ctx.closePath();
1926
+ break;
1927
+ case 20:
1928
+ if (fillA > 0) ctx.fill();
1929
+ break;
1930
+ case 21: if (lineA > 0 && lineW > 0) ctx.stroke();
1931
+ }
1932
+ }
1933
+ renderInstancedBatch(batch, ctx) {
1934
+ const cols = batch.columns;
1935
+ if (!cols || cols.count <= 0) return;
1936
+ const baseAlpha = ctx.globalAlpha;
1937
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
1938
+ for (let i = 0; i < n; i++) {
1939
+ const s = cols.order ? cols.order[i] : i;
1940
+ const a = baseAlpha * (cols.alpha ? cols.alpha[s] : 1);
1941
+ if (a <= 0) continue;
1942
+ const fr = cols.frameIndex ? batch.texture.get(cols.frameIndex[s]) : batch.defaultFrame;
1943
+ const sx = cols.scaleX ? cols.scaleX[s] : 1;
1944
+ const sy = cols.scaleY ? cols.scaleY[s] : sx;
1945
+ const rot = cols.rotation ? cols.rotation[s] : 0;
1946
+ const px = cols.x[s];
1947
+ const py = cols.y[s];
1948
+ ctx.save();
1949
+ ctx.globalAlpha = a;
1950
+ ctx.imageSmoothingEnabled = fr.source.filterMode !== FilterMode.NEAREST;
1951
+ ctx.translate(px, py);
1952
+ if (rot !== 0) ctx.rotate(rot);
1953
+ if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
1954
+ 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);
1955
+ ctx.restore();
1956
+ }
1957
+ ctx.globalAlpha = baseAlpha;
1958
+ }
1959
+ /** Create an offscreen Canvas2D render target backed by a registered Stage `Texture` (`key`). */
1960
+ createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
1961
+ const w = Math.max(1, Math.round(width));
1962
+ const h = Math.max(1, Math.round(height));
1963
+ const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
1964
+ if (!doc) throw new Error("Stage: ownerDocument unavailable for Canvas2D RenderTarget");
1965
+ const rtCanvas = doc.createElement("canvas");
1966
+ rtCanvas.width = w;
1967
+ rtCanvas.height = h;
1968
+ const tex = textures.addCanvas(key, rtCanvas);
1969
+ if (!tex) throw new Error("Stage: failed to register Canvas2D RenderTarget texture");
1970
+ tex.source[0]?.setFilter(filterMode);
1971
+ const rtRenderer = new Renderer2D(rtCanvas, w, h);
1972
+ return {
1973
+ key,
1974
+ width: w,
1975
+ height: h,
1976
+ texture: tex,
1977
+ _rtRenderer: rtRenderer,
1978
+ destroy: () => {
1979
+ rtRenderer.destroy();
1980
+ textures.remove(key);
1981
+ }
1982
+ };
1983
+ }
1984
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node. */
1985
+ renderToTarget(target, content, camera) {
1986
+ const sub = target._rtRenderer;
1987
+ if (!sub) return;
1988
+ if (typeof content === "function") {
1989
+ camera.syncMetrics();
1990
+ content(sub, camera);
1991
+ } else sub.render(content, camera);
1992
+ const src = target.texture.source[0];
1993
+ if (src) src.dirty = true;
1994
+ }
1995
+ renderBlitter(blitter, ctx) {
1996
+ const list = blitter.children.list;
1997
+ const baseAlpha = ctx.globalAlpha;
1998
+ for (let i = 0; i < list.length; i++) {
1999
+ const bob = list[i];
2000
+ if (!bob.visible || bob.alpha <= 0) continue;
2001
+ const frame = bob.frame;
2002
+ ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2003
+ ctx.globalAlpha = baseAlpha * bob.alpha;
2004
+ ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
2005
+ }
2006
+ ctx.globalAlpha = baseAlpha;
2007
+ }
2008
+ renderNode(obj, camera, ctx, isRoot) {
2009
+ if (!obj.visible || obj.alpha <= 0) return;
2010
+ ctx.save();
2011
+ if (isRoot) {
2012
+ if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) {
2013
+ const cm = camera.matrixCombined;
2014
+ ctx.setTransform(cm.a, cm.b, cm.c, cm.d, cm.tx, cm.ty);
2015
+ } else {
2016
+ const z = camera.zoom || 1;
2017
+ const ox = camera.width * camera.originX;
2018
+ const oy = camera.height * camera.originY;
2019
+ const sx = camera.roundPixels ? Math.round(camera.scrollX) : camera.scrollX;
2020
+ const sy = camera.roundPixels ? Math.round(camera.scrollY) : camera.scrollY;
2021
+ ctx.setTransform(z, 0, 0, z, (-sx * obj.scrollFactorX - ox) * z + ox + camera.x, (-sy * obj.scrollFactorY - oy) * z + oy + camera.y);
2022
+ }
2023
+ }
2024
+ if (obj.type === "Container") {
2025
+ const mask = obj.clipMask;
2026
+ if (mask && mask.active && mask.graphics?.maskRect) {
2027
+ const mr = mask.graphics.maskRect;
2028
+ const mw = mask.graphics.getWorldTransformMatrix(this.maskWorldMat);
2029
+ const cm = camera.matrixCombined;
2030
+ const wx0 = mw.a * mr.x + mw.c * mr.y + mw.tx;
2031
+ const wy0 = mw.b * mr.x + mw.d * mr.y + mw.ty;
2032
+ const wx1 = mw.a * (mr.x + mr.w) + mw.c * (mr.y + mr.h) + mw.tx;
2033
+ const wy1 = mw.b * (mr.x + mr.w) + mw.d * (mr.y + mr.h) + mw.ty;
2034
+ const sx0 = cm.a * wx0 + cm.c * wy0 + cm.tx;
2035
+ const sy0 = cm.b * wx0 + cm.d * wy0 + cm.ty;
2036
+ const sx1 = cm.a * wx1 + cm.c * wy1 + cm.tx;
2037
+ const sy1 = cm.b * wx1 + cm.d * wy1 + cm.ty;
2038
+ ctx.save();
2039
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
2040
+ ctx.beginPath();
2041
+ ctx.rect(Math.min(sx0, sx1), Math.min(sy0, sy1), Math.abs(sx1 - sx0), Math.abs(sy1 - sy0));
2042
+ ctx.restore();
2043
+ ctx.clip();
2044
+ }
2045
+ }
2046
+ ctx.globalAlpha *= obj.alpha;
2047
+ if (obj.blendMode === BlendModes.ADD) ctx.globalCompositeOperation = "lighter";
2048
+ else if (obj.blendMode === BlendModes.MULTIPLY) ctx.globalCompositeOperation = "multiply";
2049
+ else if (obj.blendMode === BlendModes.SCREEN) ctx.globalCompositeOperation = "screen";
2050
+ if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
2051
+ if (obj.rotation !== 0) ctx.rotate(obj.rotation);
2052
+ if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
2053
+ switch (obj.type) {
2054
+ case "Image":
2055
+ this.renderImage(obj, ctx);
2056
+ break;
2057
+ case "NineSlice":
2058
+ this.renderNineSlice(obj, ctx);
2059
+ break;
2060
+ case "TileSprite":
2061
+ this.renderTileSprite(obj, ctx);
2062
+ break;
2063
+ case "Rectangle":
2064
+ this.renderRectangle(obj, ctx);
2065
+ break;
2066
+ case "Panel":
2067
+ this.renderPanel(obj, ctx);
2068
+ break;
2069
+ case "MsdfIcon":
2070
+ this.renderMsdfIcon(obj, ctx);
2071
+ break;
2072
+ case "Text":
2073
+ this.renderText(obj, ctx);
2074
+ break;
2075
+ case "Graphics":
2076
+ this.renderGraphics(obj, ctx);
2077
+ break;
2078
+ case "Blitter":
2079
+ this.renderBlitter(obj, ctx);
2080
+ break;
2081
+ case "InstancedBatch":
2082
+ this.renderInstancedBatch(obj, ctx);
2083
+ break;
2084
+ case "Container": {
2085
+ const list = obj.sortedChildren();
2086
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
2087
+ break;
2088
+ }
2089
+ }
2090
+ ctx.restore();
2091
+ }
2092
+ render(displayList, camera) {
2093
+ camera.syncMetrics();
2094
+ const ctx = this.ctx;
2095
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
2096
+ ctx.globalAlpha = 1;
2097
+ ctx.globalCompositeOperation = "source-over";
2098
+ if (camera.transparent) ctx.clearRect(0, 0, this.width, this.height);
2099
+ else {
2100
+ const bg = camera.backgroundColor;
2101
+ ctx.fillStyle = `rgba(${Math.round(bg.r * 255)},${Math.round(bg.g * 255)},${Math.round(bg.b * 255)},${bg.a})`;
2102
+ ctx.fillRect(0, 0, this.width, this.height);
2103
+ }
2104
+ ctx.globalAlpha = camera.alpha;
2105
+ const list = displayList.sorted();
2106
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
2107
+ }
2108
+ destroy() {
2109
+ this.removeAllListeners();
2110
+ this.tintCanvas = null;
2111
+ this.tintCtx = null;
2112
+ }
2113
+ };
1433
2114
  // src/renderer-webgpu.ts
1434
2115
  const GPUShaderStage_VERTEX = 1;
1435
2116
  const GPUShaderStage_FRAGMENT = 2;
@@ -2603,6 +3284,42 @@ var RendererWebGpu = class extends EventEmitter {
2603
3284
  this.lastStats.sdfPanels = this.sdfCount;
2604
3285
  this.lastStats.sdfDraws = sdfDraws;
2605
3286
  }
3287
+ /** Create an offscreen render target backed by a registered Stage `Texture` (`key`) for WebGPU passes (e.g. `Lighting2DSystem`). */
3288
+ createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
3289
+ const w = Math.max(1, Math.round(width));
3290
+ const h = Math.max(1, Math.round(height));
3291
+ const doc = this.canvas?.ownerDocument ?? (typeof document !== "undefined" ? document : void 0);
3292
+ if (!doc) throw new Error("Stage: ownerDocument unavailable for WebGPU RenderTarget");
3293
+ const rtCanvas = doc.createElement("canvas");
3294
+ rtCanvas.width = w;
3295
+ rtCanvas.height = h;
3296
+ const tex = textures.addCanvas(key, rtCanvas);
3297
+ if (!tex) throw new Error("Stage: failed to register WebGPU RenderTarget texture");
3298
+ tex.source[0]?.setFilter(filterMode);
3299
+ const rtRenderer = new Renderer2D(rtCanvas, w, h);
3300
+ return {
3301
+ key,
3302
+ width: w,
3303
+ height: h,
3304
+ texture: tex,
3305
+ _rtRenderer: rtRenderer,
3306
+ destroy: () => {
3307
+ rtRenderer.destroy();
3308
+ textures.remove(key);
3309
+ }
3310
+ };
3311
+ }
3312
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload. */
3313
+ renderToTarget(target, content, camera) {
3314
+ const sub = target._rtRenderer;
3315
+ if (!sub) return;
3316
+ if (typeof content === "function") {
3317
+ camera.syncMetrics();
3318
+ content(sub, camera);
3319
+ } else sub.render(content, camera);
3320
+ const src = target.texture.source[0];
3321
+ if (src) src.dirty = true;
3322
+ }
2606
3323
  destroy() {
2607
3324
  this.removeAllListeners();
2608
3325
  try {
@@ -2616,4 +3333,4 @@ var RendererWebGpu = class extends EventEmitter {
2616
3333
  } catch {}
2617
3334
  }
2618
3335
  };
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 };
3336
+ 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 };