最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reattach the canvas context after using transferControlToOffscreen? - Stack Overflow

programmeradmin0浏览0评论

I'm playing about with a project at the moment where I have multiple simulations running in multiple web workers.

When I need to view the current state of the simulation I use transferControlToOffscreen on a canvas element in the main thread and pass the handle into a webworker.

The problem is the next time I try to use transferControlToOffscreen or just pass the existing handle into a different web worker the browser plains that I can't reclone the canvas.

An OffscreenCanvas could not be cloned because it was detached

Is there a function I need to call to reattach the canvas before using transferControlToOffscreen again?


Edit: It also seems like I can't pass the offscreenCanvas between the workers using a MessageChannel.

Is my only option to nullify the handle to the canvas is WorkerA, replace the current canvas (in the main thread) with a new one (Maybe cloned) and then send a new offscreenCanvas to worker B?

I'm playing about with a project at the moment where I have multiple simulations running in multiple web workers.

When I need to view the current state of the simulation I use transferControlToOffscreen on a canvas element in the main thread and pass the handle into a webworker.

The problem is the next time I try to use transferControlToOffscreen or just pass the existing handle into a different web worker the browser plains that I can't reclone the canvas.

An OffscreenCanvas could not be cloned because it was detached

Is there a function I need to call to reattach the canvas before using transferControlToOffscreen again?


Edit: It also seems like I can't pass the offscreenCanvas between the workers using a MessageChannel.

Is my only option to nullify the handle to the canvas is WorkerA, replace the current canvas (in the main thread) with a new one (Maybe cloned) and then send a new offscreenCanvas to worker B?

Share Improve this question edited Oct 3, 2017 at 21:27 michael asked Oct 3, 2017 at 13:42 michaelmichael 4,4836 gold badges41 silver badges57 bronze badges 1
  • I think you could use OffscreenCanvasRenderingContext2D#mit to replace content of the original canvas with what's in the offscreen canvas context (be it "2d" or "webgl"). – rishat Commented Oct 3, 2017 at 21:37
Add a ment  | 

2 Answers 2

Reset to default 4

So it turns out that the only solution to this was cloning the canvas, replacing it in the DOM with its own clone, and then using transferControlToOffscreen on the clone

I found that this is occurred when invoking worker.postMessage with the offscreen variable directly (worker.postMessage({canvas: offscreen}, offscreen)) and is fixed by just turning the transfer variable into an array: worker.postMessage({canvas: offscreen}, [offscreen]).

发布评论

评论列表(0)

  1. 暂无评论