@@ -61,9 +61,11 @@ export function drawCanvas(
61
61
iconImg . crossOrigin = 'anonymous' ;
62
62
iconImg . width = iconSize * ( canvas . width / size ) ;
63
63
iconImg . height = iconSize * ( canvas . width / size ) ;
64
- iconImg . onload = ( ) => {
64
+
65
+ const onLoad = ( ) : void => {
66
+ cleanup ( ) ;
65
67
drawCanvasBackground ( ctx , canvas . width , canvas . height , scale , backgroundColor ) ;
66
- drawCanvasColor ( ctx , value , scale , formattedPadding , backgroundColor , color ) ;
68
+ drawCanvasColor ( ctx , value ! , scale , formattedPadding , backgroundColor , color ) ;
67
69
const iconCoordinate = canvas . width / 2 - ( iconSize * ( canvas . width / size ) ) / 2 ;
68
70
69
71
ctx . fillRect ( iconCoordinate , iconCoordinate , iconSize * ( canvas . width / size ) , iconSize * ( canvas . width / size ) ) ;
@@ -75,10 +77,20 @@ export function drawCanvas(
75
77
iconSize * ( canvas . width / size )
76
78
) ;
77
79
} ;
78
- iconImg . onerror = ( ) => {
80
+
81
+ const onError = ( ) : void => {
82
+ cleanup ( ) ;
79
83
drawCanvasBackground ( ctx , canvas . width , canvas . height , scale , backgroundColor ) ;
80
84
drawCanvasColor ( ctx , value , scale , formattedPadding , backgroundColor , color ) ;
81
85
} ;
86
+
87
+ const cleanup = ( ) : void => {
88
+ iconImg . removeEventListener ( 'load' , onLoad ) ;
89
+ iconImg . removeEventListener ( 'error' , onError ) ;
90
+ } ;
91
+
92
+ iconImg . addEventListener ( 'load' , onLoad ) ;
93
+ iconImg . addEventListener ( 'error' , onError ) ;
82
94
}
83
95
}
84
96
0 commit comments