export function dataUrlToArrayBuffer ( dataURI ) { const byteString = atob ( dataURI . split ( ',' ) [ 1 ] ) ; const ia = new Uint8Array ( byteString . length ) ; for ( let i = 0 ; i < byteString . length ; i ++ ) { ia [ i ] = byteString . charCodeAt ( i ) ; } return ia . buffer as ArrayBuffer ; } export async function canvasToBlob ( canvas , type ) { if ( canvas . toBlob ) { const result = new Promise ( ( resolve ) => { canvas . toBlob ( ( blob ) => resolve ( blob ) , type ) ; } ) ; return result ; } else { const dataURL = canvas . toDataURL ( type ) ; const buffer = dataUrlToArrayBuffer ( dataURL ) ; return new Blob ( [ buffer ] , { type } ) ; } } export function blobToArrayBuffer ( blob ) { return new Promise ( ( resolve , reject ) => { const reader = new FileReader ( ) ; reader . addEventListener ( 'loadend' , async ( e ) => { resolve ( reader . result ) ; } ) ; reader . addEventListener ( 'error' , reject ) ; reader . readAsArrayBuffer ( blob ) ; } ) ; }