SHARE
TWEET

Untitled

a guest Mar 24th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const supportsWebp = ({ createImageBitmap, Image }) => {
  2.   // if you don't have these two methods on window you're not parsing any images:
  3.   if (!createImageBitmap || !Image) return Promise.resolve(false);
  4.  
  5.   return new Promise((resolve) => {
  6.     const image = new Image();
  7.     image.onload = () => {
  8.       createImageBitmap(image)
  9.         .then(() => {
  10.           resolve(true);
  11.         })
  12.         .catch(() => {
  13.           resolve(false);
  14.         });
  15.     };
  16.     image.onerror = () => {
  17.       resolve(false);
  18.     };
  19.     image.src = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=';
  20.   });
  21. }
  22.  
  23. export const webpIsSupported = () => {
  24.   let memo = null;
  25.   return () => {
  26.     if (!memo) {
  27.       memo = supportsWebp(window);
  28.     }
  29.     return memo;
  30.   };
  31. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top