Web Workers 和 Canvas

发布于 2024-08-13 08:47:21 字数 30 浏览 11 评论 0原文

是否允许 Web Worker 访问画布对象?

Are web workers allowed to access a canvas object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-08-20 08:47:21

小更新,因为问题现在已经有半年多了:

在 Chrome/Chromium 6 中,您现在可以将画布的 ImageData 对象发送给 Web Worker,让 Web Worker 对该对象进行更改,然后将其写回到使用 putImageData(..) 的画布。

Google 的 Chromabrush 就是这样做的,源代码可以在这里找到:

更新:

的最新开发快照Opera (10.70) 和 Firefox (4.0b1) 还支持将 ImageData 对象传递给 Web Worker。

更新2017:

来自Github的实际链接(更容易从Chromabrush找到所需文件):

Small update, as the question is now more than half a year old:

In Chrome/Chromium 6 you can now send a canvas' ImageData object to a web worker, let the web worker make changes to the object and then write it back to the canvas using putImageData(..).

Google's Chromabrush does it this way, the source-code can be found here:

Update:

The latest development snapshots of Opera (10.70) and Firefox (4.0b1) also support passing ImageData objects to a web worker.

Update 2017:

Actual links from Github (easier to find needed files from Chromabrush):

星星的軌跡 2024-08-20 08:47:21

较新的浏览器支持 OffscreenCanvas (请参阅该文档中的浏览器兼容性) ,这是一个在 Web Worker 中运行的画布 2D 上下文,但会自动绘制到主线程。

以下是该 MDN 文档中的基本示例。

在主线程中,创建一个 OffscreenCanvas,然后将其发送给工作线程:

var htmlCanvas = document.getElementById("canvas");
var offscreen = htmlCanvas.transferControlToOffscreen();

var worker = new Worker("offscreencanvas.js"); 
worker.postMessage({canvas: offscreen}, [offscreen]);

在工作线程中,使用离屏画布引用创建一个上下文,就像您通常在主线程上所做的那样,并执行您想要的任何绘图命令

onmessage = function(evt) {
  const canvas = evt.data.canvas;
  const gl = canvas.getContext("webgl");

  function render(time) {
    // ... some drawing using the gl context ...
    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
};

:( requestAnimationFrame API 存在于工作线程内部。)

Newer browsers support OffscreenCanvas (see the browser compatibility in that doc), which is a canvas 2d context that runs in web workers, but automatically paints to the main thread.

The following is the basic example from that MDN doc.

In the main thread, create an OffscreenCanvas, then send it to a worker:

var htmlCanvas = document.getElementById("canvas");
var offscreen = htmlCanvas.transferControlToOffscreen();

var worker = new Worker("offscreencanvas.js"); 
worker.postMessage({canvas: offscreen}, [offscreen]);

In the worker thread, use the offscreen canvas reference to create a context like you would normally do on the main thread, and perform any drawing commands that you want:

onmessage = function(evt) {
  const canvas = evt.data.canvas;
  const gl = canvas.getContext("webgl");

  function render(time) {
    // ... some drawing using the gl context ...
    requestAnimationFrame(render);
  }

  requestAnimationFrame(render);
};

(The requestAnimationFrame API exists inside the worker.)

不知在何时 2024-08-20 08:47:21

不。postMessage

规范在几个月前进行了更新,允许您发布 ImageData 对象,但到目前为止还没有人实现这一行为(我们都在实现这一目标)。 canvas 本身的问题在于它是一个 DOM 元素,因此在工作线程中不起作用(没有 DOM)。

最近在whatwg 或web-apps 邮件列表中提出了这个问题,所以我怀疑我们将开始考虑是否可以在worker 中提供类似CanvasRenderingContext2D 的api。

No.

The postMessage spec was updated a few months back to allow you to post ImageData objects but as yet no one has implemented that behaviour (we're all getting there). The problem with canvas itself is that it's a DOM element and so doesn't work in a worker (there's no DOM).

This was raised recently on either the whatwg or web-apps mailing lists so i suspect we'll start looking at whether it's possible to provide a CanvasRenderingContext2D-like api in workers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文