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.