Description
For now, the only possible way to fetch the image data in a canvas is to call getImageData, which returns a newly created ImageData.
However, sometimes the buffer where we want to store the image data has already been made, so it's useful to fill the data into the exisitng buffer, rather than creating a new one, to avoid data copying, so we can get better performance.
This is especially useful in tandem with WebAssembly.Memory. For example, let's consider the following pesudo code:
//Step 1: Allocate memory for wasm
const memory = new WebAssembly.Memory(...);
const imageData = new ImageData(new Uint8ClampedArray(memory.buffer), ...);
//Step 2: Do some work in canvas, and get the resulted image data for future use in wasm
offscreenCanvasContext.drawImage(...);
offscreenCanvasContext.fillImageData(imageData, sx, sy, sw, sh); //If sw, sh are beyond the room of the data buffer, an exception is thrown.
Without a method like fillImageData
, we have to first call getImageData
to create a new ImageData, and then copy the data in ImageData to the wasm memory, which results in an unnecessary performance cost.
So I propose to add a new method fillImageData
in CanvasImageData.