Skip to content

Commit 19ad229

Browse files
author
xuying.xu
committed
feat: 增加webcanvas勾子函数
1 parent c64aaff commit 19ad229

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

packages/f-my/src/web.tsx

+26-5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ function dispatchEvent(el, event, type) {
2929

3030
Component({
3131
didMount() {
32+
const { onUpdate, onClear } = this.props;
33+
onUpdate && onUpdate(this.createChart.bind(this));
34+
onClear && onClear(this.clear.bind(this));
3235
this.createChart();
3336
},
3437
didUpdate() {
3538
const { canvas, props } = this;
3639
if (!canvas) return;
3740
const { theme, px2hd, reCreateOnUpdate } = props;
41+
3842
if (reCreateOnUpdate) {
3943
canvas.destroy();
4044
this.canvas = null;
@@ -53,9 +57,7 @@ Component({
5357
});
5458
},
5559
didUnmount() {
56-
const { canvas } = this;
57-
if (!canvas) return;
58-
canvas.destroy();
60+
this.clear();
5961
},
6062
methods: {
6163
createChart() {
@@ -86,6 +88,25 @@ Component({
8688
});
8789
},
8890

91+
clear() {
92+
const canvas = this.canvas;
93+
if (!canvas) return;
94+
const { width, height } = this.props;
95+
const { rpx2px, pixelRatio } = this.data;
96+
try {
97+
canvas.context.ctx.clearRect(
98+
0,
99+
0,
100+
width * rpx2px * pixelRatio,
101+
height * rpx2px * pixelRatio,
102+
);
103+
canvas.destroy();
104+
this.canvas = null;
105+
} catch (error) {
106+
this.catchError(error);
107+
}
108+
},
109+
89110
catchError(error) {
90111
console.error('图表渲染失败: ', error);
91112
const { onError } = this.props;
@@ -126,9 +147,9 @@ Component({
126147
.select(`f-web-canvas-${this.$id}`)
127148
//@ts-ignore
128149
.node()
129-
.exec((time) => {
150+
.exec(() => {
130151
// api 执行结束后的下一个通信才会上屏
131-
onCanvasRender && onCanvasRender(time);
152+
onCanvasRender && onCanvasRender();
132153
});
133154
},
134155
// @ts-ignore

0 commit comments

Comments
 (0)