@@ -21,6 +21,7 @@ Component({
21
21
width : null ,
22
22
height : null ,
23
23
type : '2d' , // canvas 2d, 基础库 2.7 以上支持
24
+ onError : ( error ) => { } ,
24
25
} ,
25
26
/**
26
27
* 组件创建时触发
@@ -35,27 +36,33 @@ Component({
35
36
return ;
36
37
}
37
38
const { id } = this . data ;
38
- const query = my . createSelectorQuery ( { page : this . $page } ) ;
39
- query
40
- . select ( `#${ id } ` )
41
- . boundingClientRect ( )
42
- . exec ( ( res ) => {
43
- // 获取画布实际宽高, 用props的宽高做失败兜底
44
- const { width, height } = res && res [ 0 ] ? res [ 0 ] : this . props ;
45
- const pixelRatio = getPixelRatio ( ) ;
46
- // 高清解决方案
47
- this . setData (
48
- {
49
- width : width * pixelRatio ,
50
- height : height * pixelRatio ,
51
- } ,
52
- ( ) => {
53
- const myCtx = my . createCanvasContext ( id ) ;
54
- const context = F2Context ( myCtx ) ;
55
- this . canvasRender ( { width, height, context, pixelRatio } ) ;
56
- }
57
- ) ;
58
- } ) ;
39
+ const { onError } = this . props ;
40
+ try {
41
+ const query = my . createSelectorQuery ( { page : this . $page } ) ;
42
+ query
43
+ . select ( `#${ id } ` )
44
+ . boundingClientRect ( )
45
+ . exec ( ( res ) => {
46
+ // 获取画布实际宽高, 用props的宽高做失败兜底
47
+ const { width, height } = res && res [ 0 ] ? res [ 0 ] : this . props ;
48
+ const pixelRatio = getPixelRatio ( ) ;
49
+ // 高清解决方案
50
+ this . setData (
51
+ {
52
+ width : width * pixelRatio ,
53
+ height : height * pixelRatio ,
54
+ } ,
55
+ ( ) => {
56
+ const myCtx = my . createCanvasContext ( id ) ;
57
+ const context = F2Context ( myCtx ) ;
58
+ this . canvasRender ( { width, height, context, pixelRatio } ) ;
59
+ }
60
+ ) ;
61
+ } ) ;
62
+ } catch ( error ) {
63
+ onError && onError ( error ) ;
64
+ throw error ;
65
+ }
59
66
} ,
60
67
didUpdate ( ) {
61
68
const { canvas, props } = this ;
@@ -78,23 +85,29 @@ Component({
78
85
} ,
79
86
onCanvasReady ( ) {
80
87
const { id } = this . data ;
81
- const query = my . createSelectorQuery ( ) ;
82
- query
83
- . select ( `#${ id } ` )
84
- // @ts -ignore
85
- . node ( )
86
- . exec ( ( res ) => {
87
- if ( ! res [ 0 ] ) {
88
- return ;
89
- }
90
- const canvas = res [ 0 ] . node ;
91
- const { width, height } = canvas ;
92
- const pixelRatio = getPixelRatio ( ) ;
93
- canvas . width = width * pixelRatio ;
94
- canvas . height = height * pixelRatio ;
95
- const context = canvas . getContext ( '2d' ) ;
96
- this . canvasRender ( { width, height, pixelRatio, context } ) ;
97
- } ) ;
88
+ const { onError } = this . props ;
89
+ try {
90
+ const query = my . createSelectorQuery ( ) ;
91
+ query
92
+ . select ( `#${ id } ` )
93
+ // @ts -ignore
94
+ . node ( )
95
+ . exec ( ( res ) => {
96
+ if ( ! res [ 0 ] ) {
97
+ return ;
98
+ }
99
+ const canvas = res [ 0 ] . node ;
100
+ const { width, height } = canvas ;
101
+ const pixelRatio = getPixelRatio ( ) ;
102
+ canvas . width = width * pixelRatio ;
103
+ canvas . height = height * pixelRatio ;
104
+ const context = canvas . getContext ( '2d' ) ;
105
+ this . canvasRender ( { width, height, pixelRatio, context } ) ;
106
+ } ) ;
107
+ } catch ( error ) {
108
+ onError && onError ( error ) ;
109
+ throw error ;
110
+ }
98
111
} ,
99
112
canvasRender ( { width, height, pixelRatio, context } ) {
100
113
if ( ! width || ! height ) {
0 commit comments