Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1057210 of OffscreenCanvas

  • Revision slug: Web/API/OffscreenCanvas
  • Revision title: OffscreenCanvas
  • Revision id: 1057210
  • Created:
  • Creator: dtipson
  • Is current revision? Yes
  • Comment Minor syntax edit.

Revision Content

{{APIRef("Canvas API")}} {{SeeCompatTable}}

The OffscreenCanvas interface provides a canvas that can be rendered off screen. It is available in both the window and worker contexts.

Note: This API is currently implemented for WebGL1 and WebGL2 contexts only. See {{bug(801176)}} for Canvas 2D API support from workers.

Constructors

{{domxref("OffscreenCanvas.OffscreenCanvas", "OffscreenCanvas()")}}
OffscreenCanvas constructor. Creates a new OffscreenCanvas object.

Properties

{{domxref("OffscreenCanvas.height")}}
The height of the offscreen canvas.
{{domxref("OffscreenCanvas.width")}}
The width of the offscreen canvas.

Methods

{{domxref("OffscreenCanvas.getContext()")}}
Returns a rendering context for the offscreen canvas.
{{domxref("OffscreenCanvas.toBlob()")}}
Creates a {{domxref("Blob")}} object representing the image contained in the canvas.
{{domxref("OffscreenCanvas.transferToImageBitmap()")}}
Creates an {{domxref("ImageBitmap")}} object from the most recently rendered image of the OffscreenCanvas.

Examples

Synchronous display of frames produced by an OffscreenCanvas

One way to use the OffscreenCanvas API, is to use a {{domxref("RenderingContext")}} that has been obtained from an OffscreenCanvas object to generate new frames. Once a new frame has finished rendering in this context,  the {{domxref("OffscreenCanvas.transferToImageBitmap", "transferToImageBitmap()")}} method can be called to save the most recent rendered image. This method returns an {{domxref("ImageBitmap")}} object, which can be used in a variety of Web APIs and also in a second canvas without creating a transfer copy.

To display the ImageBitmap, you can use a {{domxref("ImageBitmapRenderingContext")}} context, which can be created by calling canvas.getContext("bitmaprenderer") on a (visible) canvas element. This context only provides functionality to replace the canvas's contents with the given ImageBitmap. A call to {{domxref("ImageBitmapRenderingContext.transferImageBitmap()")}} with the previously rendered and saved ImageBitmap from the OffscreenCanvas, will display the ImageBitmap on the canvas and transfer its ownership to the canvas. A single OffscreenCanvas may transfer frames into an arbitrary number of other ImageBitmapRenderingContext objects.

Given these two {{HTMLElement("canvas")}} elements

<canvas id="one"></canvas>
<canvas id="two"></canvas>

the following code will provide the rendering using an OffscreenCanvas as described above.

var one = document.getElementById("one").getContext("bitmaprenderer"); 
var two = document.getElementById("two").getContext("bitmaprenderer");

var offscreen = new OffscreenCanvas(256, 256);
var gl = offscreen.getContext('webgl');

// ... some drawing for the first canvas using the gl context ...

// Commit rendering to the first canvas
var bitmapOne = offscreen.transferToImageBitmap();
one.transferImageBitmap(bitmapOne);

// ... some more drawing for the second canvas using the gl context ...

// Commit rendering to the second canvas 
var bitmapTwo = offscreen.transferToImageBitmap();
two.transferImageBitmap(bitmapTwo);

Asynchronous display of frames produced by an OffscreenCanvas

Another way to use the OffscreenCanvas API, is to call {{domxref("HTMLCanvasElement.transferControlToOffscreen", "transferControlToOffscreen()")}} on a {{HTMLElement("canvas")}} element, either on a worker or the main thread, which will return an OffscreenCanvas object from an {{domxref("HTMLCanvasElement")}} object from the main thread. Calling {{domxref("OffscreenCanvas.getContext", "getContext()")}} will then obtain a RenderingContext from that OffscreenCanvas.

In order to make frames visible, you can call commit() on that RenderingContext, so that frames will be pushed back to the original {{HTMLElement("canvas")}} element.

Note that, in Firefox, this API is currently only implemented for the WebGL context ({{domxref("WebGLRenderingContext.commit()")}}). For Canvas 2D API support from workers, see {{bug(801176)}}.

main.js (main thread code):

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

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

offscreencanvas.js (worker code):

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

  // ... some drawing using the gl context ...

  // Push frames back to the original HTMLCanvasElement
  gl.commit();
};

Specifications

{{WhyNoSpecStart}} Currently drafted as a proposal: OffscreenCanvas.{{WhyNoSpecEnd}}

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}} {{CompatGeckoDesktop(44)}} [1] {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoMobile(44)}} [1] {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] This feature is behind a feature preference setting. In about:config, set gfx.offscreencanvas.enabled to true.

See also

  • WebGL Off the Main Thread – Mozilla Hacks
  • {{domxref("CanvasRenderingContext2D")}}
  • {{domxref("ImageBitmap")}}
  • {{domxref("ImageBitmapRenderingContext")}}
  • {{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}
  • {{domxref("WebGLRenderingContext.commit()")}}

Revision Source

<div>{{APIRef("Canvas API")}} {{SeeCompatTable}}</div>

<p>The <strong><code>OffscreenCanvas</code></strong> interface provides a canvas that can be rendered off screen. It is available in both&nbsp;the window and&nbsp;<a href="/en-US/docs/Web/API/Web_Workers_API">worker</a> contexts.</p>

<div class="note">
<p><strong>Note</strong>: This API is currently implemented for <a href="/en-US/docs/Web/API/WebGLRenderingContext">WebGL1</a> and <a href="/en-US/docs/Web/API/WebGL2RenderingContext">WebGL2</a> contexts only. See {{bug(801176)}} for <a href="/en-US/docs/Web/API/Canvas_API">Canvas 2D API</a> support from workers.</p>
</div>

<h2 id="Constructors">Constructors</h2>

<dl>
 <dt>{{domxref("OffscreenCanvas.OffscreenCanvas", "OffscreenCanvas()")}}</dt>
 <dd><code>OffscreenCanvas</code> constructor. Creates a new <code>OffscreenCanvas</code> object.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("OffscreenCanvas.height")}}</dt>
 <dd>The height of the offscreen canvas.</dd>
 <dt>{{domxref("OffscreenCanvas.width")}}</dt>
 <dd>The width of the offscreen canvas.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("OffscreenCanvas.getContext()")}}</dt>
 <dd>Returns a rendering context for the offscreen canvas.</dd>
</dl>

<dl>
 <dt>{{domxref("OffscreenCanvas.toBlob()")}}</dt>
 <dd>Creates a {{domxref("Blob")}} object representing the image contained in the canvas.</dd>
</dl>

<dl>
 <dt>{{domxref("OffscreenCanvas.transferToImageBitmap()")}}</dt>
 <dd>Creates an {{domxref("ImageBitmap")}} object from the most recently rendered image of the <code>OffscreenCanvas</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Synchronous_display_of_frames_produced_by_an_OffscreenCanvas">Synchronous display of frames produced by an <code>OffscreenCanvas</code></h3>

<p>One way to use the <code>OffscreenCanvas</code> API, is to use a {{domxref("RenderingContext")}} that has been obtained from an <code>OffscreenCanvas</code> object to generate new frames. Once a new frame has finished rendering in this context,&nbsp; the {{domxref("OffscreenCanvas.transferToImageBitmap", "transferToImageBitmap()")}} method can be called to save the most recent rendered image. This method returns an {{domxref("ImageBitmap")}} object, which can be used in a variety of Web APIs and also in a second canvas without creating a transfer copy.</p>

<p>To display the <code>ImageBitmap</code>, you can use a {{domxref("ImageBitmapRenderingContext")}} context, which can be created by calling <code>canvas.getContext("bitmaprenderer")</code> on a (visible) canvas element. This context only provides functionality to replace the canvas's contents with the given <code>ImageBitmap</code>. A call to {{domxref("ImageBitmapRenderingContext.transferImageBitmap()")}} with the previously rendered and saved <code>ImageBitmap</code> from the OffscreenCanvas, will display the <code>ImageBitmap</code> on the canvas and transfer its ownership to the canvas. A single <code>OffscreenCanvas</code> may transfer frames into an arbitrary number of other <code>ImageBitmapRenderingContext</code> objects.</p>

<p>Given these two {{HTMLElement("canvas")}} elements</p>

<pre class="brush: html">
&lt;canvas id="one"&gt;&lt;/canvas&gt;
&lt;canvas id="two"&gt;&lt;/canvas&gt;</pre>

<p>the following code will provide the rendering using an <code>OffscreenCanvas</code> as described above.</p>

<pre class="brush: js">
var one = document.getElementById("one").getContext("bitmaprenderer"); 
var two = document.getElementById("two").getContext("bitmaprenderer");

var offscreen = new OffscreenCanvas(256, 256);
var gl = offscreen.getContext('webgl');

// ... some drawing for the first canvas using the gl context ...

// Commit rendering to the first canvas
var bitmapOne = offscreen.transferToImageBitmap();
one.transferImageBitmap(bitmapOne);

// ... some more drawing for the second canvas using the gl context ...

// Commit rendering to the second canvas 
var bitmapTwo = offscreen.transferToImageBitmap();
two.transferImageBitmap(bitmapTwo);
</pre>

<h3 id="Asynchronous_display_of_frames_produced_by_an_OffscreenCanvas">Asynchronous display of frames produced by an <code>OffscreenCanvas</code></h3>

<p>Another way to use the <code>OffscreenCanvas</code> API, is to call {{domxref("HTMLCanvasElement.transferControlToOffscreen", "transferControlToOffscreen()")}} on a {{HTMLElement("canvas")}} element, either on a <a href="/en-US/docs/Web/API/Web_Workers_API">worker</a> or the main thread, which will return an <code>OffscreenCanvas</code> object from an {{domxref("HTMLCanvasElement")}} object from the main thread. Calling {{domxref("OffscreenCanvas.getContext", "getContext()")}} will then obtain a <code>RenderingContext</code> from that <code>OffscreenCanvas</code>.</p>

<p>In order to make frames visible, you can call <code>commit()</code> on that <code>RenderingContext</code>, so that frames will be pushed back to the original {{HTMLElement("canvas")}} element.</p>

<p>Note that, in Firefox, this API is currently only implemented for the <a href="/en-US/docs/Web/API/WebGL_API">WebGL context</a> ({{domxref("WebGLRenderingContext.commit()")}}). For <a href="/en-US/docs/Web/API/Canvas_API">Canvas 2D API</a> support from workers, see {{bug(801176)}}.</p>

<p>main.js (main thread code):</p>

<pre class="brush: js">
var htmlCanvas = document.getElementById("canvas");
var offscreen = htmlCanvas.transferControlToOffscreen();

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

<p>offscreencanvas.js (worker code):</p>

<pre class="brush: js">
onmessage = function(evt) {
  var canvas = evt.data.canvas.
  var gl = canvas.getContext("webgl");

  // ... some drawing using the gl context ...

  // Push frames back to the original HTMLCanvasElement
  gl.commit();
};
</pre>

<h2 id="Specifications">Specifications</h2>

<p>{{WhyNoSpecStart}} Currently drafted as a proposal: <a href="https://wiki.whatwg.org/wiki/OffscreenCanvas">OffscreenCanvas</a>.{{WhyNoSpecEnd}}</p>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop(44)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(44)}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] This feature is behind a feature preference setting. In about:config, set <code>gfx.offscreencanvas.enabled</code> to <code>true</code>.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://hacks.mozilla.org/2016/01/webgl-off-the-main-thread/">WebGL Off the Main Thread – Mozilla Hacks</a></li>
 <li>{{domxref("CanvasRenderingContext2D")}}</li>
 <li>{{domxref("ImageBitmap")}}</li>
 <li>{{domxref("ImageBitmapRenderingContext")}}</li>
 <li>{{domxref("HTMLCanvasElement.transferControlToOffscreen()")}}</li>
 <li>{{domxref("WebGLRenderingContext.commit()")}}</li>
</ul>
Revert to this revision