Last active
April 24, 2024 18:42
-
-
Save noamr/b6e77e2c8a04e4362983477c9a8f727c to your computer and use it in GitHub Desktop.
Paint timestamp
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
const next_frame = () => Promise(resolve => requestAnimationFrame(ts => resolve(ts)); | |
async function get_img_presentation_time(img) { | |
// Wait until image is fully loaded | |
await new Promise(resolve => img.complete ? resolve() : img.addEventListener("load", resolve)) | |
let frame_timestamp = await next_frame(); | |
// Wait until we have the frame timestamp soonest after the image was decoded. | |
// Note that because of main-thread scheduling the implementation might be slightly different than the polyfill | |
let decoded = false; | |
img.decode().then(() => { | |
decoded = true; | |
}); | |
while (!decoded) { | |
frame_timestamp = await next_frame(); | |
} | |
return frame_timestamp; | |
} |
Updated a bit and added comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
correct, it wouldn't be a 100% polyfill, but it demonstrates something very close.
correct, it wouldn't be a 100% polyfill, but it demonstrates something very close, and the fact that this polyfill exposes just as much about the image as a browser implementation would.