@@ -9,7 +9,6 @@ type fillStyle = string | CanvasGradient | CanvasPattern;
9
9
type Props = {
10
10
backgroundColour : fillStyle ,
11
11
backgroundTransparent : boolean ,
12
- height : number ,
13
12
proportion : number ,
14
13
ringBgColour : fillStyle ,
15
14
ringFgColour : fillStyle ,
@@ -18,14 +17,12 @@ type Props = {
18
17
showIntermediateProgress : boolean ,
19
18
segmented : boolean ,
20
19
steps : number ,
21
- width : number ,
22
20
} ;
23
21
24
22
export class CanvasRenderer extends React . Component < Props > {
25
23
static defaultProps = {
26
24
backgroundColour : '#fff' ,
27
25
backgroundTransparent : true ,
28
- height : 100 ,
29
26
proportion : 0 ,
30
27
ringBgColour : '#ccc' ,
31
28
ringFgColour : '#3c763d' ,
@@ -34,32 +31,38 @@ export class CanvasRenderer extends React.Component<Props> {
34
31
showIntermediateProgress : false ,
35
32
segmented : true ,
36
33
steps : 360 ,
37
- width : 100 ,
38
34
} ;
39
35
40
36
ctx : CanvasRenderingContext2D ;
41
37
42
38
canvas : HTMLCanvasElement | null ;
43
39
44
40
componentDidMount = ( ) => {
45
- if ( this . canvas ) {
46
- this . ctx = getCanvasContext ( this . canvas ) ;
47
- this . draw ( ) ;
41
+ if ( this . canvas !== null ) {
42
+ // Assign canvas to local variable to work around a flow refinement invalidation.
43
+ // https://flow.org/en/docs/lang/refinements/#toc-refinement-invalidations
44
+ const canvas = this . canvas ;
45
+ this . ctx = getCanvasContext ( canvas ) ;
46
+ this . draw ( canvas ) ;
48
47
}
49
48
} ;
50
49
51
- componentDidUpdate ( ) {
52
- if ( this . canvas ) {
53
- this . draw ( ) ;
50
+ componentDidUpdate = ( ) => {
51
+ if ( this . canvas !== null ) {
52
+ this . draw ( this . canvas ) ;
54
53
}
55
- }
54
+ } ;
55
+
56
+ draw = ( canvasElement : HTMLCanvasElement ) => {
57
+ const rect = canvasElement . getBoundingClientRect ( ) ;
58
+ const width = rect . width ;
59
+ const height = rect . height ;
56
60
57
- draw = ( ) => {
58
- const x = this . props . width * 0.5 ;
59
- const y = this . props . height * 0.5 ;
61
+ const x = width * 0.5 ;
62
+ const y = height * 0.5 ;
60
63
61
64
const radius = radiusProportion => {
62
- return ( this . props . width / 2 ) * radiusProportion ;
65
+ return ( Math . min ( width , height ) / 2 ) * radiusProportion ;
63
66
} ;
64
67
65
68
const step = Math . floor ( this . props . steps * this . props . proportion ) ;
@@ -69,7 +72,7 @@ export class CanvasRenderer extends React.Component<Props> {
69
72
const stepDegree = ( 360 / this . props . steps ) * step ;
70
73
71
74
// Clear the canvas
72
- this . ctx . clearRect ( 0 , 0 , this . props . width , this . props . height ) ;
75
+ this . ctx . clearRect ( 0 , 0 , width , height ) ;
73
76
74
77
// Draw the background circle
75
78
drawSegment ( this . ctx , x , y , radius ( 1 ) , 0 , 360 ) ;
@@ -128,8 +131,8 @@ export class CanvasRenderer extends React.Component<Props> {
128
131
< canvas
129
132
ref = { ref => ( this . canvas = ref ) }
130
133
style = { {
131
- width : this . props . width ,
132
- height : this . props . height ,
134
+ width : '100%' ,
135
+ height : '100%' ,
133
136
} }
134
137
/>
135
138
) ;
0 commit comments