Created
January 5, 2022 00:27
-
-
Save yjbanov/cdcdad86eb522a7a73233f6a412ffd1f to your computer and use it in GitHub Desktop.
ImageDecoder stress test
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>ImageDecoder memory usage test</title> | |
</head> | |
<body> | |
<script> | |
async function run() { | |
async function renderAnimatedImage(url) { | |
let data = await fetch(url); | |
let decoder = new ImageDecoder({ | |
type: 'image/webp', | |
data: await data.arrayBuffer(), | |
premultiplyAlpha: 'premultiply', | |
colorSpaceConversion: 'default', | |
preferAnimation: true | |
}); | |
await decoder.tracks.ready; | |
await decoder.completed; | |
let frameCount = decoder.tracks[0].frameCount; | |
let canvas = document.createElement('canvas'); | |
canvas.width = 100; | |
canvas.height = 100; | |
canvas.style.width = '100px'; | |
canvas.style.height = '100px'; | |
document.body.append(canvas); | |
let ctx = canvas.getContext('2d'); | |
let frameIndex = 0; | |
while (true) { | |
let decodeResult = await decoder.decode({frameIndex: frameIndex}); | |
let frame = decodeResult.image; | |
ctx.save(); | |
ctx.scale(100 / frame.displayWidth, 100 / frame.displayHeight); | |
ctx.drawImage(frame, 0, 0); | |
ctx.restore(); | |
frameIndex = (frameIndex + 1) % frameCount; | |
let timer = new Promise((resolve, reject) => { | |
setTimeout(resolve, frame.duration / 1000); | |
frame.close(); | |
}); | |
await timer; | |
} | |
} | |
let urls = [ | |
'https://media4.giphy.com/media/Ve1cBYXUmh6iyuYSnF/giphy.webp', | |
'https://media3.giphy.com/media/fZ31GBoN4EOUb83rIt/giphy.webp', | |
'https://media4.giphy.com/media/wP4T2q2sb9vqgz4GZ1/giphy.webp', | |
'https://media1.giphy.com/media/RgGfMHu2HLKLwGeGFb/giphy.webp', | |
'https://media0.giphy.com/media/3o7TKtpfZ3e8rwww7K/giphy.webp', | |
'https://media1.giphy.com/media/4nKnhZsY9mNX9n928r/giphy.webp', | |
'https://media4.giphy.com/media/8Rqk3cKgw799pM8U1a/giphy.webp', | |
'https://media3.giphy.com/media/3ov9jSfDqguXOoLfhu/giphy.webp', | |
'https://media4.giphy.com/media/yh4Q1Hft8zzUgzOybI/giphy.webp', | |
'https://media4.giphy.com/media/SF9v8zMYhyvLn9GTTH/giphy.webp', | |
'https://media0.giphy.com/media/BwPYESM1eTKgozrKnT/giphy.webp', | |
'https://media2.giphy.com/media/Gx9iROMAiZmFtAopIJ/giphy.webp', | |
'https://media1.giphy.com/media/wyyqRSqoWGSiP66G6a/giphy.webp', | |
'https://media2.giphy.com/media/SWUneSnAJCgy1qSfNG/giphy.webp', | |
'https://media0.giphy.com/media/l6oX5SS8vA5n7GSXG2/giphy.webp', | |
'https://media2.giphy.com/media/QBpi7TeiDCgQ59VH1O/giphy.webp' | |
]; | |
for (let url of urls) { | |
renderAnimatedImage(url); | |
} | |
} | |
run(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment