to pass single images to a worker thread I do something like the following
var image = ctx.getImageData(0, 0, data.width, data.height));
var tData = new Uint8ClampedArray(data.width*data.height*4);
tData.set(image.data);
let message = {function: "my function", buffer: tData.buffer}
worker.postMessage(message, [tData.buffer]);
This works fine for single images, but now I want to send variable number of images using a loop. The sending of multiple arrays is permitted in the specification (eg. worker.postMessage(message, [array1, array2]);) but then you cannot dynamically alter the number of images.
let imageDataArray=[];
for(let i=0;i<images.length;i++){
var tData = new Uint8ClampedArray(data.width*data.height*4);
tData.set(images[i].data);
imageDataArray.push(tData.buffer);
}
let message = {function: "my function", buffer: imageDataArray}
worker.postMessage(message, [imageDataArray]);
but it is just not working correctly since the data type is not transferable. Is there a proper way to do this?
Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': Value at index 0 does not have a transferable type.
The last line should be (note: no square brackets needed).
Then in the worker the data is accessed like
then in the calling function it needs to be accessed by array index
then you can use it like
Not all browsers support multiple images so you should check compatibility first by doing a dummy test with a array argument to the worker.