DEV Community

Meghan (she/her)
Meghan (she/her)

Posted on


createImageBitmap polyfill for Safari and Edge

Chris Coyier, the developer behind recently tweeted:

And so I'm about to do exactly that. I've been working on a web app game recently and I was bringing in some images dynamically using the Fetch API, and then drawing them on to a <canvas>. My simplified version of what I was doing was this:

async function getPicture(url) {
    return fetch(url)
    .then(x => x.blob())
    .then(x => createImageBitmap(x));
Enter fullscreen mode Exit fullscreen mode

This worked great on Chrome. And not anywhere else for some reason. Even though ImageBitmap is in the HTML spec.

And so after a really long time, I came up with the following polyfill:

Feel free to use it anywhere you like, and save yourself the headache that I've had for so long.

Edit: ImageBitmap works on Firefox, but recently I've been using ES6 Modules as well, which aren't enabled in Firefox by default at the time of this writing.

Top comments (1)

aeisenberg profile image
Andrew Eisenberg

Thanks for this it's useful for me. However, every time you are finished with a url created by URL.createObjectURL, you need to call URL.revokeObjectURL.

Not exactly sure the best way around this. And the memory leak is only important if you are creating many image bitmaps. Though it is an important thing to know.