Skip to content

Instantly share code, notes, and snippets.

@milksense
Created June 1, 2021 02:40
Show Gist options
  • Save milksense/e9802fc1895f7e84804dd929c0b8e4bf to your computer and use it in GitHub Desktop.
Save milksense/e9802fc1895f7e84804dd929c0b8e4bf to your computer and use it in GitHub Desktop.
Is WEBP supported
let webpSupported = undefined;
/* export ES6+ */ async function isWebpSupported() {
if (webpSupported !== undefined) {
return webpSupported;
}
const promise = new Promise(resolve => {
const image = new Image();
image.onload = function () {
resolve(image.width === 2 && image.height === 1);
}
image.onerror = function () {
resolve(false);
}
image.src = 'data:image/webp;base64,UklGRjIAAABXRUJQVlA4ICYAAACyAgCdASoCAAEALmk0mk0iIiIiIgBoSygABc6zbAAA/v56QAAAAA==';
});
return webpSupported = await promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment