Skip to content

Commit c64aaff

Browse files
tangying1027xuying.xu
and
xuying.xu
authored
feat:调整web canvas为输入宽高 (#319)
* fix: 将offscreenCanvas创建挪至初始化 * chore: 调整web canvas逻辑 * fix: web canvas宽高外部传入 --------- Co-authored-by: xuying.xu <[email protected]>
1 parent f4fcc19 commit c64aaff

File tree

6 files changed

+64
-82
lines changed

6 files changed

+64
-82
lines changed

Diff for: packages/f-my/examples/test/mini.project.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
2+
"format": 2,
3+
"compileOptions": {
4+
"component2": true,
5+
"enableNodeModuleBabelTransform": true
6+
},
27
"scripts": {
38
"beforeCompile": "npm run beforeCompile"
4-
},
5-
"enableNodeModuleBabelTransform": true,
6-
"component2": true,
7-
"enableAppxNg": true
9+
}
810
}

Diff for: packages/f-my/examples/test/pages/index/index.acss

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
width: 100%;
44
height: 600rpx;
55
}
6+
7+
.web-container {
8+
width: 350rpx;
9+
height: 480rpx;
10+
}

Diff for: packages/f-my/examples/test/pages/index/index.axml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
<view>native</view>
33
<f-canvas onRender="onRenderChart" data="{{data}}"></f-canvas>
44
<view>web</view>
5-
<f-canvas-web onRender="onRenderChart" data="{{data}}"></f-canvas-web>
5+
<view class="web-container">
6+
<f-canvas-web onRender="onRenderChart" data="{{data}}" width="{{350}}" height="{{480}}">
7+
</f-canvas-web>
8+
</view>
69
</view>

Diff for: packages/f-my/examples/test/pages/index/rect.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component } from '@antv/f-engine';
2-
import { jsx as _jsx } from "@antv/f-engine/jsx-runtime";
3-
import { jsxs as _jsxs } from "@antv/f-engine/jsx-runtime";
2+
import { jsx as _jsx, jsxs as _jsxs } from "@antv/f-engine/jsx-runtime";
43
class Rect extends Component {
54
render() {
65
const {

Diff for: packages/f-my/src/web.axml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<canvas
2-
a:if="{{id}}"
3-
id="{{id}}"
4-
class="f-canvas"
2+
id="{{`f-web-canvas-${$id}`}}"
3+
class="f-web-canvas"
54
width="{{width}}"
65
height="{{height}}"
76
onTap="click"
87
onTouchStart="touchStart"
98
onTouchMove="touchMove"
109
onTouchEnd="touchEnd"
11-
/>
10+
/>

Diff for: packages/f-my/src/web.tsx

+45-71
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,21 @@ function dispatchEvent(el, event, type) {
2727
el.dispatchEvent(event);
2828
}
2929

30-
const getPixelRatio = () => my.getSystemInfoSync().pixelRatio;
31-
3230
Component({
33-
props: {
34-
onRender: (_props) => {},
35-
// width height 会作为元素兜底的宽高使用
36-
width: null,
37-
height: null,
38-
onError: () => {},
39-
onCanvasReady: () => {},
40-
onCanvasRender: () => {},
41-
},
42-
/**
43-
* 组件创建时触发
44-
* 注意:
45-
* 使用该生命周期,项目配置需启用:"component2": true
46-
*/
47-
onInit() {
48-
this.setCanvasId();
49-
},
5031
didMount() {
51-
// 为了兼容未配置 "component2": true 的情况
52-
if (!this.data.id) {
53-
this.setCanvasId();
54-
}
55-
this.onCanvasReady();
32+
this.createChart();
5633
},
5734
didUpdate() {
5835
const { canvas, props } = this;
5936
if (!canvas) return;
60-
const { theme, px2hd } = props;
37+
const { theme, px2hd, reCreateOnUpdate } = props;
38+
if (reCreateOnUpdate) {
39+
canvas.destroy();
40+
this.canvas = null;
41+
this.createChart();
42+
return;
43+
}
44+
6145
const children = props.onRender(props);
6246
const updateProps = {
6347
theme,
@@ -74,52 +58,32 @@ Component({
7458
canvas.destroy();
7559
},
7660
methods: {
77-
setCanvasId() {
78-
const pageId = (this.$page && this.$page.$id) || 0;
79-
const id = `f-canvas-${pageId}-${this.$id}`;
80-
this.setData({ id });
81-
},
82-
onCanvasReady() {
83-
const { onCanvasReady } = this.props;
84-
onCanvasReady && onCanvasReady();
85-
const { id } = this.data;
86-
const query = my.createSelectorQuery();
87-
query
88-
.select(`#${id}`)
89-
.boundingClientRect()
90-
.exec((res) => {
91-
if (!res[0]) {
92-
return;
93-
}
94-
const { width, height } = res[0] as {
95-
width: number;
96-
height: number;
97-
};
98-
const pixelRatio = Math.ceil(getPixelRatio());
61+
createChart() {
62+
const { width, height } = this.props;
63+
const id = `f-web-canvas-${this.$id}`;
64+
const { pixelRatio: drp = 2, windowWidth = 375 } = my?.getSystemInfoSync();
9965

100-
// 高清解决方案
101-
this.setData(
102-
{
103-
width: width * pixelRatio,
104-
height: height * pixelRatio,
105-
},
106-
() => {
107-
const { element: canvas, context } = createCanvasAdapter(my.createCanvasContext(id));
108-
const fCanvas = this.createCanvas({
109-
width,
110-
height,
111-
pixelRatio,
112-
context,
113-
createImage: canvas.createImage.bind(canvas),
114-
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
115-
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
116-
});
117-
fCanvas.render().catch((error) => {
118-
this.catchError(error);
119-
});
120-
},
121-
);
122-
});
66+
const pixelRatio = Math.ceil(drp);
67+
const rpx2px = windowWidth / 750;
68+
69+
this.setData({
70+
width: width * rpx2px * pixelRatio,
71+
height: height * rpx2px * pixelRatio,
72+
});
73+
74+
const { element: canvas, context } = createCanvasAdapter(my.createCanvasContext(id));
75+
const fCanvas = this.createCanvas({
76+
width: width * rpx2px,
77+
height: height * rpx2px,
78+
pixelRatio,
79+
context,
80+
createImage: canvas.createImage.bind(canvas),
81+
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
82+
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
83+
});
84+
fCanvas.render().catch((error) => {
85+
this.catchError(error);
86+
});
12387
},
12488

12589
catchError(error) {
@@ -156,7 +120,17 @@ Component({
156120
createImage,
157121
requestAnimationFrame,
158122
cancelAnimationFrame,
159-
onRender: onCanvasRender,
123+
onRender: () => {
124+
const query = my.createSelectorQuery();
125+
query
126+
.select(`f-web-canvas-${this.$id}`)
127+
//@ts-ignore
128+
.node()
129+
.exec((time) => {
130+
// api 执行结束后的下一个通信才会上屏
131+
onCanvasRender && onCanvasRender(time);
132+
});
133+
},
160134
// @ts-ignore
161135
offscreenCanvas: my.createOffscreenCanvas ? my.createOffscreenCanvas() : null,
162136
// useNativeClickEvent: false,

0 commit comments

Comments
 (0)