@@ -27,37 +27,21 @@ function dispatchEvent(el, event, type) {
27
27
el . dispatchEvent ( event ) ;
28
28
}
29
29
30
- const getPixelRatio = ( ) => my . getSystemInfoSync ( ) . pixelRatio ;
31
-
32
30
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
- } ,
50
31
didMount ( ) {
51
- // 为了兼容未配置 "component2": true 的情况
52
- if ( ! this . data . id ) {
53
- this . setCanvasId ( ) ;
54
- }
55
- this . onCanvasReady ( ) ;
32
+ this . createChart ( ) ;
56
33
} ,
57
34
didUpdate ( ) {
58
35
const { canvas, props } = this ;
59
36
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
+
61
45
const children = props . onRender ( props ) ;
62
46
const updateProps = {
63
47
theme,
@@ -74,52 +58,32 @@ Component({
74
58
canvas . destroy ( ) ;
75
59
} ,
76
60
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 ( ) ;
99
65
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
+ } ) ;
123
87
} ,
124
88
125
89
catchError ( error ) {
@@ -156,7 +120,17 @@ Component({
156
120
createImage,
157
121
requestAnimationFrame,
158
122
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
+ } ,
160
134
// @ts -ignore
161
135
offscreenCanvas : my . createOffscreenCanvas ? my . createOffscreenCanvas ( ) : null ,
162
136
// useNativeClickEvent: false,
0 commit comments