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(minigame): the display canvas follows the window
esengine55184776d820
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b0cad0b46..fa0fdbfcd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -15,6 +15,12 @@ published separately; it ships inside the editor.## [Unreleased]### Fixed+- **A mini-game's canvas follows its window.** It was sized once at boot from+ `getSystemInfoSync` and never again, so any later change — a rotation, a foldable,+ split screen, or a host that opens in the other orientation from the one `game.json`+ asked for — left the canvas in the old coordinate space while touches kept arriving+ in the window's. The game still rendered, and stopped answering taps entirely.+- **A remote group with no CDN root loads from the package.** Its bytes ship insidethe build (editor Play, a mini-game with no CDN), but the load path fell through todiff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 1a5966d29..862d7b17a 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -5177,6 +5177,7 @@ onTouchEnd: (cb: (res: MiniGameTouchEvent) => void) => voidonTouchMove: (cb: (res: MiniGameTouchEvent) => void) => voidonTouchStart: (cb: (res: MiniGameTouchEvent) => void) => voidonUnhandledRejection: ((cb: (res: { reason?: unknown; }) => void) => void) | undefined+onWindowResize: ((cb: (res: { windowWidth: number; windowHeight: number; }) => void) => void) | undefinedremoveStorageSync: (key: string) => voidrequest: (opts: MiniGameRequestOptions) => voidrequestMidasPayment: ((opts: { mode: string; offerId: string; buyQuantity: number; zoneId?: string; currencyType?: string; platform?: string; env?: number; success?: () => void; fail?: (err: { errMsg?: string; errCode?: number; }) => void; }) => void) | undefineddiff --git a/sdk/src/platform/minigame/adapter.ts b/sdk/src/platform/minigame/adapter.tsindex 0a93814a8..ff4ab3c86 100644--- a/sdk/src/platform/minigame/adapter.ts+++ b/sdk/src/platform/minigame/adapter.ts@@ -367,15 +367,26 @@ export class MiniGamePlatformAdapter implements PlatformAdapter {*/createScreenCanvas(): MiniGameCanvas {const canvas = this.g_.createCanvas();- const info = this.g_.getSystemInfoSync();- const dpr = info.pixelRatio ?? 1;- // Left at the host's own default when the info is incomplete — a 0×0- // surface would be worse than the host's guess.- if (info.windowWidth) canvas.width = info.windowWidth * dpr;- if (info.windowHeight) canvas.height = info.windowHeight * dpr;+ this.sizeScreenCanvas_(canvas, this.g_.getSystemInfoSync());+ // The window is not fixed — a rotation, a foldable, a host that opens in+ // the other orientation. Sized once, the canvas keeps the old space while+ // touches keep arriving in the window's, and taps stop landing at all.+ this.g_.onWindowResize?.((res) => this.sizeScreenCanvas_(canvas, res));return canvas;}+ /** Size the display canvas to a window measurement. Left at the host's own+ * default when the measurement is incomplete — a 0×0 surface would be worse+ * than the host's guess. */+ private sizeScreenCanvas_(+ canvas: MiniGameCanvas,+ size: { windowWidth?: number; windowHeight?: number },+ ): void {+ const dpr = this.g_.getSystemInfoSync().pixelRatio ?? 1;+ if (size.windowWidth) canvas.width = size.windowWidth * dpr;+ if (size.windowHeight) canvas.height = size.windowHeight * dpr;+ }+now(): number {return Date.now();}diff --git a/sdk/src/platform/minigame/api.ts b/sdk/src/platform/minigame/api.tsindex f300fa94d..6c1fb2a7d 100644--- a/sdk/src/platform/minigame/api.ts+++ b/sdk/src/platform/minigame/api.ts@@ -248,6 +248,10 @@ export interface MiniGameGlobal {onShow?(cb: () => void): void;onHide?(cb: () => void): void;+ /** The window changed size — a rotation, a foldable, split screen. Carries+ * the new size, which is authoritative: a `getSystemInfoSync` beside it can+ * still answer with the old one. */+ onWindowResize?(cb: (res: { windowWidth: number; windowHeight: number }) => void): void;/** Monetization + share — optional like every capability a vendor may lack* (or a game may not have configured): the adapter probes at use time. */diff --git a/sdk/tests/minigame-window-resize.test.ts b/sdk/tests/minigame-window-resize.test.tsnew file mode 100644index 000000000..9837e4b10--- /dev/null+++ b/sdk/tests/minigame-window-resize.test.ts@@ -0,0 +1,70 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file minigame-window-resize.test.ts+ * @brief The display canvas follows the window. Sized once at boot it keeps+ * the old space while touches keep arriving in the window's, so what is+ * drawn and what is hit come apart — a game that renders and answers no+ * tap at all, which is what a host opening in the other orientation+ * looks like.+ */+import { describe, it, expect, vi } from 'vitest';+import { MiniGamePlatformAdapter } from '../src/platform/minigame/adapter';+import type { MiniGameCanvas } from '../src/platform/minigame/api';++function host(info: { windowWidth: number; windowHeight: number; pixelRatio: number }) {+ const canvas = { width: 0, height: 0, getContext: () => null } as unknown as MiniGameCanvas;+ let resized: ((res: { windowWidth: number; windowHeight: number }) => void) | null = null;+ const g = {+ createCanvas: () => canvas,+ getSystemInfoSync: () => info,+ onWindowResize: (cb: (res: { windowWidth: number; windowHeight: number }) => void) => { resized = cb; },+ };+ const adapter = new MiniGamePlatformAdapter({ id: 'wechat', hostLabel: 'WeChat', global: g } as never);+ return { adapter, canvas, g, resize: (w: number, h: number) => resized?.({ windowWidth: w, windowHeight: h }) };+}++describe('the mini-game display canvas', () => {+ it('is sized to the window at boot, in device pixels', () => {+ const { adapter, canvas } = host({ windowWidth: 812, windowHeight: 375, pixelRatio: 3 });+ expect(adapter.createScreenCanvas()).toBe(canvas);+ expect([canvas.width, canvas.height]).toEqual([2436, 1125]);+ });++ it('follows the window when it changes — a rotation is not a new game', () => {+ const { adapter, canvas, resize } = host({ windowWidth: 375, windowHeight: 812, pixelRatio: 3 });+ adapter.createScreenCanvas();+ expect([canvas.width, canvas.height]).toEqual([1125, 2436]);++ resize(812, 375);++ expect([canvas.width, canvas.height]).toEqual([2436, 1125]);+ });++ it('takes the size the resize event carries, not a re-read of system info', () => {+ // `getSystemInfoSync` beside the event can still answer with the old+ // window; the event is the one that knows what just happened.+ const { adapter, canvas, resize } = host({ windowWidth: 375, windowHeight: 812, pixelRatio: 2 });+ adapter.createScreenCanvas();+ resize(812, 375);+ expect([canvas.width, canvas.height]).toEqual([1624, 750]);+ });++ it('keeps the host default when a measurement is missing', () => {+ const { adapter, canvas, resize } = host({ windowWidth: 375, windowHeight: 812, pixelRatio: 1 });+ adapter.createScreenCanvas();+ resize(0, 0);+ expect([canvas.width, canvas.height]).toEqual([375, 812]);+ });++ it('does not require the host to offer onWindowResize', () => {+ const canvas = { width: 0, height: 0, getContext: () => null } as unknown as MiniGameCanvas;+ const g = {+ createCanvas: () => canvas,+ getSystemInfoSync: () => ({ windowWidth: 100, windowHeight: 200, pixelRatio: 1 }),+ };+ const adapter = new MiniGamePlatformAdapter({ id: 'wechat', hostLabel: 'WeChat', global: g } as never);+ expect(() => adapter.createScreenCanvas()).not.toThrow();+ expect([canvas.width, canvas.height]).toEqual([100, 200]);+ });+});