W3C home > Mailing lists > Public > public-webapps@w3.org > April to June 2015

Async Image -> ImageData conversion

From: Ashley Gullen <ashley@scirra.com>
Date: Wed, 17 Jun 2015 17:59:35 +0100
Message-ID: <CAABs73gH9QRKgy2soB9Ki=pHZehsLmYHdXubwgE559vMXAs5Qg@mail.gmail.com>
To: "public-webapps@w3.org" <public-webapps@w3.org>
I was wondering if there is anything on the standards track to
asynchronously get an ImageData object from an Image?

We have a web app dealing with large sprite sheets (up to 2048x2048), and
at some point we need to get an ImageData for this. Currently the only way
I know of doing this is via a canvas 2d context like so:

function ImageToImageData(img)
let w = img.width;
let h = img.height;
 let canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
return ctx.getImageData(0, 0, w, h);

This whole function is synchronous. With large images, in Chrome the
getImageData call can jank for well over 100ms on a high-end desktop
machine. Maybe this can be faster but I really doubt this whole function is
likely to be done in <16ms on low-end devices, so I believe there ought to
be an async alternative. As far as I am aware there is none.

Ideally we could have an async ImageData.fromImage(img) method or similar.
It would also be useful if we could decode directly from Blob to ImageData
without having to go via an Image as well (perhaps ImageData.fromBlob(blob)

More broadly, it would be good to have a comprehensive set of async
conversion functions between Blob, Image, and ImageData. Blob -> Image can
already be done by setting the image src to a blob URL, but basically
everything else requires going synchronously through a 2D canvas, which
janks apps which have to process lots of large images. Running a canvas in
a worker could possibly help, but that would require both Blob and Image to
be transferable to be useful.

Ashley Gullen
Received on Wednesday, 17 June 2015 17:00:09 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:27:31 UTC