A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(editor): the viewport stops stretching, and readback stops coming back black
yhhf8d538f978cb
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.tsindex 632ae2d94..4ac78bf0b 100644--- a/desktop/src/engine/EditorControlSurface.ts+++ b/desktop/src/engine/EditorControlSurface.ts@@ -513,17 +513,24 @@ export class EditorControlSurfaceImpl {* frame, and with no rAF loop the headless host's buffer persists.*//**- * Resize the render canvas (headless drivers): the engine follows on the- * next stepped frame — GL tracks the canvas drawing buffer implicitly,- * WebGPU reconfigures its swapchain through the same per-frame size source.+ * Resize the render canvas's DRAWING BUFFER (headless drivers): the engine+ * follows on the next stepped frame — GL tracks the canvas drawing buffer+ * implicitly, WebGPU reconfigures its swapchain through the same per-frame+ * size source. This is also the resolution `captureViewport` reads.+ *+ * Deliberately does NOT touch `canvas.style`: the canvas is laid out at+ * `width/height: 100%` of its container, and pinning a pixel CSS size here+ * permanently detaches it from that container — in the live editor the panel+ * then keeps growing while the canvas does not, and the frame is drawn+ * stretched into a stale box. A driver that wants the LIVE viewport to be a+ * given size must size its PANEL (`setPanelSize`) and let layout drive the+ * canvas; the drawing buffer follows via EngineHost's ResizeObserver.*/resizeViewport(width: number, height: number): void {const canvas = EngineHost.canvas;if (!canvas) throw new Error('resizeViewport requires a render host (no canvas)');canvas.width = width;canvas.height = height;- canvas.style.width = `${width}px`;- canvas.style.height = `${height}px`;}/**diff --git a/desktop/src/engine/EngineHost.ts b/desktop/src/engine/EngineHost.tsindex a53548298..6062e7e6f 100644--- a/desktop/src/engine/EngineHost.ts+++ b/desktop/src/engine/EngineHost.ts@@ -615,6 +615,17 @@ class EngineHostImpl {depth: true,stencil: true,premultipliedAlpha: false,+ // The editor must be able to READ BACK what it drew — `captureViewport`+ // (screenshots, visual verification, the MCP automation surface) does a+ // `readPixels` on the default framebuffer from a task of its own. Without+ // this the browser is free to discard the drawing buffer as soon as it+ // has composited, so every readback that isn't in the same task as the+ // draw comes back BLACK — which is exactly what happened in the live+ // editor while the headless host (no rAF loop, nothing composites) kept+ // working. This is the editor's own context, created here rather than by+ // the engine, so the cost lands on the editor alone and no shipping+ // build pays for it.+ preserveDrawingBuffer: true,}) as WebGL2RenderingContext | null;if (!gl) throw new Error('WebGL2 is not available in this renderer.');diff --git a/desktop/tests/editor-control-surface.test.ts b/desktop/tests/editor-control-surface.test.tsindex 44580ed77..916d92e15 100644--- a/desktop/tests/editor-control-surface.test.ts+++ b/desktop/tests/editor-control-surface.test.ts@@ -25,6 +25,8 @@ const host = vi.hoisted(() => ({app: null as unknown as App,ticks: 0,runMode: null as null | [boolean, boolean],+ // null by default (no render host); a test that needs one substitutes a stub.+ canvas: null as null | { width: number; height: number; style: Record<string, string> },}));vi.mock('@/engine/EngineHost', () => ({@@ -35,7 +37,7 @@ vi.mock('@/engine/EngineHost', () => ({},getResource: () => undefined,get canvas() {- return null; // no render host in the node harness → captureViewport must fail clearly+ return host.canvas; // null by default → captureViewport must fail clearly},tick: async (dt: number) => {host.ticks++;@@ -69,6 +71,7 @@ describe.skipIf(!HAS_WASM)('EditorControlSurface (headless World)', () => {host.app = app;host.ticks = 0;host.runMode = null;+ host.canvas = null;S = EditorSession.create();S.model.adopt(emptyScene(), new Map());});@@ -188,6 +191,23 @@ describe.skipIf(!HAS_WASM)('EditorControlSurface (headless World)', () => {expect(S.surface.getSelection()).toBeNull();});+ it('resizeViewport sets the drawing buffer and leaves CSS sizing to layout', () => {+ // The canvas is laid out at width/height:100% of its panel. Writing a pixel+ // CSS size here detaches it from that container for good: the panel keeps+ // growing, the canvas does not, and every later frame is drawn stretched+ // into a stale box. Only the drawing buffer is this door's business.+ host.canvas = { width: 300, height: 150, style: { width: '100%', height: '100%' } };+ S.surface.resizeViewport(1280, 720);+ expect(host.canvas.width).toBe(1280);+ expect(host.canvas.height).toBe(720);+ expect(host.canvas.style.width).toBe('100%');+ expect(host.canvas.style.height).toBe('100%');+ });++ it('resizeViewport fails clearly with no render host', () => {+ expect(() => S.surface.resizeViewport(64, 64)).toThrow(/render host/);+ });+it('subscribeSelection fires on change and stops after unsubscribe', () => {const a = S.surface.addEntity()!;let n = 0;