diff --git a/packages/f-my/src/index.tsx b/packages/f-my/src/index.tsx index c0fe90e5..75ca6e0e 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,12 @@ Component({ }); }, didUnmount() { + this.destroyed = true; + if (this.renderFrameId != null) { + this.nativeCanvas?.cancelAnimationFrame(this.renderFrameId); + this.renderFrameId = null; + } + this.nativeCanvas = null; const { canvas } = this; if (!canvas) return; canvas.destroy(); @@ -105,6 +112,8 @@ Component({ } const { width, height } = canvas; + if (this.destroyed) return; + this.nativeCanvas = canvas; const pixelRatio = Math.ceil(getPixelRatio()); @@ -115,6 +124,9 @@ Component({ height: height * pixelRatio, }, () => { + if (this.destroyed) { + return; + } const context = canvas.getContext('2d'); const fCanvas = this.createCanvas({ width, @@ -125,8 +137,14 @@ Component({ requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas), cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas), }); - fCanvas.render().catch((error) => { - this.catchError(error); + this.renderFrameId = canvas.requestAnimationFrame(() => { + this.renderFrameId = null; + if (this.destroyed) { + return; + } + fCanvas.render().catch((error) => { + this.catchError(error); + }); }); }, );