Passing objects to a web worker

后端 未结 8 1037
心在旅途
心在旅途 2020-11-28 06:19

I\'m trying to pass an object to a web worker through the postMessage function.
This object is a square that has a couple of functions to draw himself on a canvas and so

相关标签:
8条回答
  • 2020-11-28 06:48

    if you want to pass the object with methods you can stringify it and parse it at the receiving end.

    postMessage(JSON.stringify(yourObject)
    

    In the listener

    this.worker.addEventListener('message', (event) => {
       const currentChunk = JSON.parse(event.data);   
    });
    
    0 讨论(0)
  • 2020-11-28 06:52

    Some type of objects like ArrayBuffer and ImageBitmap which have the Transferable interface implementet and can be transfered without copy the Object.

    Thats very usefull in Context of Canvas + Web worker cause you can save the time of copy the data between the threads.

    0 讨论(0)
提交回复
热议问题