[whatwg] Proposal: ImageData constructor or factory method with preexisting data

Kenneth Russell kbr at google.com
Tue Aug 13 14:25:45 PDT 2013

On Tue, Aug 13, 2013 at 11:57 AM, Ian Hickson <ian at hixie.ch> wrote:
> On Mon, 12 Aug 2013, Kenneth Russell wrote:
>> The use case is the passing of ImageData objects back and forth to
>> workers to fill and refill with data.
>> An ImageData is essentially a wrapper for the underlying
>> Uint8ClampedArray, providing an associated width and height. However,
>> the only way to draw raw pixels into a Canvas is using an ImageData.
>> Currently a Unit8ClampedArray can be constructed, but the only way to
>> create an ImageData is to ask a Canvas for one, and there's no way to
>> associate it with a pre-allocated Uint8ClampedArray.
> Why can't you just send an ImageData over?
>> This means that if you want to pass an ImageData to a worker for
>> filling, transferring the underlying Uint8ClampedArray, you need to be
>> very careful about bookkeeping, and to not lose the reference to the
>> ImageData object.
> Sure. Just send the ImageData over. That seems relatively
> straight-forward. What am I missing?
>> IMO there ought to be a factory method for ImageData taking a
>> Uint8ClampedArray, width, height, and possibly resolution (or a
>> dictionary?), which validates the width, height and resolution against
>> the size of the Uint8ClampedArray, and makes a new ImageData object.
>> This would ease management of ImageData instances.
> We could have a constructor for ImageData objects, sure. That would be
> relatively easy to add, if it's really needed. I don't understand why it's
> hard to keep track of ImageData objects, though. Can you elaborate?

I have in mind new APIs for typed arrays which allow sharding of typed
arrays to workers and re-assembly of the component pieces when the
work is complete. This would involve multiple manipulations of the
ArrayBuffer and its views. It would be most convenient if the result
could be wrapped in an ImageData if it's destined to be drawn to a
Canvas. Otherwise it's likely that a data copy will need to be


More information about the whatwg mailing list