Skip to content

Commit 6debe33

Browse files
author
xuying.xu
committed
fix: web canvas宽高外部传入
1 parent 55ef607 commit 6debe33

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
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.tsx

+24-35
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function dispatchEvent(el, event, type) {
2929

3030
Component({
3131
didMount() {
32-
this.onCanvasReady();
32+
this.createChart();
3333
},
3434
didUpdate() {
3535
const { canvas, props } = this;
@@ -59,43 +59,31 @@ Component({
5959
},
6060
methods: {
6161
createChart() {
62-
const query = my.createSelectorQuery();
62+
const { width, height } = this.props;
6363
const id = `f-web-canvas-${this.$id}`;
64-
query
65-
.select(`#${id}`)
66-
.boundingClientRect()
67-
.exec((res) => {
68-
if (!res[0]) {
69-
return;
70-
}
71-
const { width, height } = res[0] as {
72-
width: number;
73-
height: number;
74-
};
64+
const { pixelRatio: drp = 2, windowWidth = 375 } = my?.getSystemInfoSync();
7565

76-
const { pixelRatio: drp = 2, windowWidth = 375 } = my?.getSystemInfoSync();
77-
const rpx2px = windowWidth / 750;
78-
const pixelRatio = Math.ceil(drp);
66+
const pixelRatio = Math.ceil(drp);
67+
const rpx2px = windowWidth / 750;
7968

80-
this.setData({
81-
width: width * rpx2px * pixelRatio,
82-
height: height * rpx2px * pixelRatio,
83-
});
69+
this.setData({
70+
width: width * rpx2px * pixelRatio,
71+
height: height * rpx2px * pixelRatio,
72+
});
8473

85-
const { element: canvas, context } = createCanvasAdapter(my.createCanvasContext(id));
86-
const fCanvas = this.createCanvas({
87-
width: width * rpx2px,
88-
height: height * rpx2px,
89-
pixelRatio,
90-
context,
91-
createImage: canvas.createImage.bind(canvas),
92-
requestAnimationFrame: canvas.requestAnimationFrame.bind(canvas),
93-
cancelAnimationFrame: canvas.cancelAnimationFrame.bind(canvas),
94-
});
95-
fCanvas.render().catch((error) => {
96-
this.catchError(error);
97-
});
98-
});
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+
});
9987
},
10088

10189
catchError(error) {
@@ -136,10 +124,11 @@ Component({
136124
const query = my.createSelectorQuery();
137125
query
138126
.select(`f-web-canvas-${this.$id}`)
127+
//@ts-ignore
139128
.node()
140129
.exec((time) => {
141130
// api 执行结束后的下一个通信才会上屏
142-
onCanvasRender(time);
131+
onCanvasRender && onCanvasRender(time);
143132
});
144133
},
145134
// @ts-ignore

0 commit comments

Comments
 (0)