From 97c54254cf2dd48939da7eefbe327cb09699ee29 Mon Sep 17 00:00:00 2001 From: huaxiabuluo Date: Fri, 8 May 2026 20:04:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E5=BB=B6=E8=BF=9F=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E5=AE=9D=20canvas=20=E9=A6=96=E5=B8=A7=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/f-my/src/index.tsx | 40 ++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/f-my/src/index.tsx b/packages/f-my/src/index.tsx index c0fe90e5..0a770f91 100644 --- a/packages/f-my/src/index.tsx +++ b/packages/f-my/src/index.tsx @@ -52,6 +52,7 @@ Component({ this.setCanvasId(); }, didMount() { + this.destroyed = false; if (!isAppX2CanvasEnv()) { console.error('当前基础库版本过低,请升级基础库版本到 2.7.0 或以上。'); } @@ -75,6 +76,11 @@ Component({ }); }, didUnmount() { + this.destroyed = true; + if (this.renderFrameId != null) { + this.nativeCanvas?.cancelAnimationFrame(this.renderFrameId); + this.renderFrameId = null; + } const { canvas } = this; if (!canvas) return; canvas.destroy(); @@ -105,6 +111,7 @@ Component({ } const { width, height } = canvas; + this.nativeCanvas = canvas; const pixelRatio = Math.ceil(getPixelRatio()); @@ -115,18 +122,27 @@ Component({ height: height * pixelRatio, }, () => { - const context = canvas.getContext('2d'); - const fCanvas = this.createCanvas({ - width, - height, - pixelRatio, - context, - createImage: canvas.createImage.bind(canvas), - requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas), - cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas), - }); - fCanvas.render().catch((error) => { - this.catchError(error); + if (this.destroyed) { + return; + } + this.renderFrameId = canvas.requestAnimationFrame(() => { + this.renderFrameId = null; + if (this.destroyed) { + return; + } + const context = canvas.getContext('2d'); + const fCanvas = this.createCanvas({ + width, + height, + pixelRatio, + context, + createImage: canvas.createImage.bind(canvas), + requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas), + cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas), + }); + fCanvas.render().catch((error) => { + this.catchError(error); + }); }); }, ); From 1d9f25281dabf280f3c601a4342ea08d388a3dbc Mon Sep 17 00:00:00 2001 From: Bruce Date: Sat, 9 May 2026 18:50:52 +0800 Subject: [PATCH 2/2] Update packages/f-my/src/index.tsx Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- packages/f-my/src/index.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/f-my/src/index.tsx b/packages/f-my/src/index.tsx index 0a770f91..8dc4f858 100644 --- a/packages/f-my/src/index.tsx +++ b/packages/f-my/src/index.tsx @@ -81,6 +81,7 @@ Component({ this.nativeCanvas?.cancelAnimationFrame(this.renderFrameId); this.renderFrameId = null; } + this.nativeCanvas = null; const { canvas } = this; if (!canvas) return; canvas.destroy();