Skip to content

Commit 4f6a1ec

Browse files
committed
fix: 延迟支付宝 canvas 首帧渲染
1 parent cd4d12c commit 4f6a1ec

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

packages/f-my/src/index.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Component({
5252
this.setCanvasId();
5353
},
5454
didMount() {
55+
this.destroyed = false;
5556
if (!isAppX2CanvasEnv()) {
5657
console.error('当前基础库版本过低,请升级基础库版本到 2.7.0 或以上。');
5758
}
@@ -75,6 +76,11 @@ Component({
7576
});
7677
},
7778
didUnmount() {
79+
this.destroyed = true;
80+
if (this.renderFrameId != null) {
81+
this.nativeCanvas?.cancelAnimationFrame(this.renderFrameId);
82+
this.renderFrameId = null;
83+
}
7884
const { canvas } = this;
7985
if (!canvas) return;
8086
canvas.destroy();
@@ -105,6 +111,7 @@ Component({
105111
}
106112

107113
const { width, height } = canvas;
114+
this.nativeCanvas = canvas;
108115

109116
const pixelRatio = Math.ceil(getPixelRatio());
110117

@@ -115,6 +122,9 @@ Component({
115122
height: height * pixelRatio,
116123
},
117124
() => {
125+
if (this.destroyed) {
126+
return;
127+
}
118128
const context = canvas.getContext('2d');
119129
const fCanvas = this.createCanvas({
120130
width,
@@ -125,8 +135,14 @@ Component({
125135
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
126136
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
127137
});
128-
fCanvas.render().catch((error) => {
129-
this.catchError(error);
138+
this.renderFrameId = canvas.requestAnimationFrame(() => {
139+
this.renderFrameId = null;
140+
if (this.destroyed) {
141+
return;
142+
}
143+
fCanvas.render().catch((error) => {
144+
this.catchError(error);
145+
});
130146
});
131147
},
132148
);

0 commit comments

Comments
 (0)