Skip to content

Commit 97c5425

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

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

packages/f-my/src/index.tsx

Lines changed: 28 additions & 12 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,18 +122,27 @@ Component({
115122
height: height * pixelRatio,
116123
},
117124
() => {
118-
const context = canvas.getContext('2d');
119-
const fCanvas = this.createCanvas({
120-
width,
121-
height,
122-
pixelRatio,
123-
context,
124-
createImage: canvas.createImage.bind(canvas),
125-
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
126-
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
127-
});
128-
fCanvas.render().catch((error) => {
129-
this.catchError(error);
125+
if (this.destroyed) {
126+
return;
127+
}
128+
this.renderFrameId = canvas.requestAnimationFrame(() => {
129+
this.renderFrameId = null;
130+
if (this.destroyed) {
131+
return;
132+
}
133+
const context = canvas.getContext('2d');
134+
const fCanvas = this.createCanvas({
135+
width,
136+
height,
137+
pixelRatio,
138+
context,
139+
createImage: canvas.createImage.bind(canvas),
140+
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
141+
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
142+
});
143+
fCanvas.render().catch((error) => {
144+
this.catchError(error);
145+
});
130146
});
131147
},
132148
);

0 commit comments

Comments
 (0)