const WebpImage = `data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=` ; function isCanvasWebpSupported ( ) { const elem = document . createElement ( 'canvas' ) ; if ( ! ! ( elem . getContext && elem . getContext ( '2d' ) ) ) { return elem . toDataURL ( 'image/webp' ) . indexOf ( 'data:image/webp' ) === 0 ; } return false ; } /**
 * Is this runtime able to support webp
 */ function isWebpImageSupported ( ) { const img = new Image ( ) ; const promise = new Promise < boolean > ( ( resolve ) => { img . onload = ( ) : void => resolve ( true ) ; img . onerror = ( ) : void => resolve ( false ) ; } ) ; img . src = WebpImage ; return promise ; } /** Is WebP able to be rendered in this environment */ export function isWebpSupported ( ) { return new Promise ( ( resolve ) => { if ( isCanvasWebpSupported ( ) ) return resolve ( true ) ; isWebpImageSupported ( ) . then ( resolve ) ; } ) ; }