@@ -31,22 +31,26 @@ export default async (paths, options) => {
3131 ...options ,
3232 } ;
3333
34+ // Check input path
3435 if ( ! paths || ! paths . length ) {
3536 throw new Error ( "No file given." ) ;
3637 }
3738
39+ // Check outputFormat
3840 const supportedFormat = [ "png" , "jpeg" ] ;
3941 if ( ! supportedFormat . includes ( outputFormat ) ) {
4042 const supported = JSON . stringify ( supportedFormat ) ;
4143 throw new Error ( `outputFormat should only be one of ${ supported } , but "${ outputFormat } " was given.` ) ;
4244 }
4345
46+ // Load all images
4447 const loads = paths . map ( path => loadImage ( path ) ) ;
4548 const images = await Promise . all ( loads ) ;
4649
4750 const playground = createCanvas ( ) ;
4851 const playgroundContext = playground . getContext ( "2d" ) ;
4952
53+ // Crop all image
5054 const data = await Promise . all ( images . map ( async ( source ) => {
5155 const { width, height } = source ;
5256 playground . width = width ;
@@ -67,15 +71,18 @@ export default async (paths, options) => {
6771 } ;
6872 } ) ) ;
6973
74+ // Pack images
7075 const { items, width, height } = pack ( data ) ;
7176
7277 const canvas = createCanvas ( width + margin , height + margin ) ;
7378 const context = canvas . getContext ( "2d" ) ;
7479
80+ // Draw all images on the destination canvas
7581 items . forEach ( ( { x, y, item } ) => {
7682 context . drawImage ( item . source , x - item . cropped . left + margin , y - item . cropped . top + margin ) ;
7783 } ) ;
7884
85+ // Write JSON
7986 const json = {
8087 // Global data about the generated file
8188 meta : {
@@ -117,6 +124,7 @@ export default async (paths, options) => {
117124 } , { } ) ,
118125 } ;
119126
127+ // Write image
120128 const image = canvas . toBuffer ( `image/${ outputFormat } ` ) ;
121129
122130 return {
0 commit comments