Created
June 18, 2021 09:41
-
-
Save b-aleksei/1a10de79f11a5b08796634bf6fbe7ef9 to your computer and use it in GitHub Desktop.
check-Format-Img
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function supportsImgType(type) { | |
const img = document.createElement('img'); | |
await document.createElement('picture').append( | |
Object.assign(document.createElement('source'), { | |
srcset: 'data:,x', // валидный урл не дергающий сеть | |
type, | |
}), | |
img | |
); | |
return !!img.currentSrc; // если браузер умеет, заполнит значение currentSrc | |
} | |
for (const type of ['image/png', 'image/jpeg', 'image/webp', 'image/avif']) { | |
supportsImgType(type).then(supported => console.log(`${type}: ${supported}`)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment