1
- module . exports = { } ;
2
-
3
- const initializeCanvas = ( { data, width, height } ) => {
1
+ const initializeCanvas = ( { width, height } ) => {
4
2
const canvas = document . createElement ( 'canvas' ) ;
5
3
canvas . width = width ;
6
4
canvas . height = height ;
7
- const ctx = canvas . getContext ( '2d' ) ;
8
-
9
- const imageData = new ImageData ( data , width , height ) ;
10
-
11
- ctx . putImageData ( imageData , 0 , 0 ) ;
12
5
13
6
return canvas ;
14
7
} ;
15
8
16
9
const convert = async ( { data, width, height } , ...blobArgs ) => {
17
- const canvas = initializeCanvas ( { data, width, height } ) ;
10
+ const canvas = initializeCanvas ( { width, height } ) ;
11
+
12
+ const ctx = canvas . getContext ( '2d' ) ;
13
+ ctx . putImageData ( new ImageData ( data , width , height ) , 0 , 0 ) ;
18
14
19
15
const blob = await new Promise ( ( resolve , reject ) => {
20
16
canvas . toBlob ( blob => {
@@ -31,10 +27,7 @@ const convert = async ({ data, width, height }, ...blobArgs) => {
31
27
return new Uint8Array ( arrayBuffer ) ;
32
28
} ;
33
29
34
- module . exports . JPEG = async ( { data, width, height, quality } ) => {
35
- return convert ( { data, width, height } , 'image/jpeg' , quality ) ;
36
- } ;
37
-
38
- module . exports . PNG = async ( { data, width, height } ) => {
39
- return convert ( { data, width, height } , 'image/png' ) ;
30
+ module . exports = {
31
+ JPEG : async ( { data, width, height, quality } ) => await convert ( { data, width, height } , 'image/jpeg' , quality ) ,
32
+ PNG : async ( { data, width, height } ) => await convert ( { data, width, height } , 'image/png' )
40
33
} ;
0 commit comments