Skip to content

Commit

Permalink
feat: my增加canvasReady&reRender
Browse files Browse the repository at this point in the history
  • Loading branch information
xuying.xu committed Sep 26, 2024
1 parent 672c288 commit 4bc0ed8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packages/f-engine/src/canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class Canvas<P extends CanvasProps = CanvasProps> {
isTouchEvent,
isMouseEvent,
useNativeClickEvent = true,
onRender,
} = props;

const px2hd = isFunction(customPx2hd) ? batch2hd(customPx2hd) : defaultPx2hd;
Expand Down Expand Up @@ -164,8 +163,6 @@ class Canvas<P extends CanvasProps = CanvasProps> {
isMouseEvent,
});

onRender && canvas.addEventListener('rerender', () => onRender(canvas), { once: true });

const container = canvas.getRoot();
const { width: canvasWidth, height: canvasHeight } = canvas.getConfig();

Expand Down Expand Up @@ -238,8 +235,10 @@ class Canvas<P extends CanvasProps = CanvasProps> {
}

async render() {
const { canvas, vNode } = this;
const { canvas, vNode, props } = this;
const { onRender } = props;
await canvas.ready;
onRender && canvas.addEventListener('rerender', () => onRender(canvas), { once: true });
render(vNode);
}

Expand Down
7 changes: 6 additions & 1 deletion packages/f-my/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Component({
width: null,
height: null,
onError: () => {},
onCanvasReady: () => {},
onCanvasRender: () => {},
type: '2d', // canvas 2d, 基础库 2.7 以上支持
},
/**
Expand Down Expand Up @@ -83,6 +85,8 @@ Component({
this.setData({ id });
},
onCanvasReady() {
const { onCanvasReady } = this.props;
onCanvasReady && onCanvasReady();
const { id } = this.data;
const query = my.createSelectorQuery();
query
Expand Down Expand Up @@ -149,7 +153,7 @@ Component({
if (!width || !height) {
return;
}
const { theme, px2hd } = this.props;
const { theme, px2hd, onCanvasRender } = this.props;
const children = this.props.onRender(this.props);
const canvas = new Canvas({
pixelRatio,
Expand All @@ -162,6 +166,7 @@ Component({
createImage,
requestAnimationFrame,
cancelAnimationFrame,
onRender: onCanvasRender,
// @ts-ignore
offscreenCanvas: my.createOffscreenCanvas(),
useNativeClickEvent: false,
Expand Down

0 comments on commit 4bc0ed8

Please sign in to comment.