docs.rodeo

MDN Web Docs mirror

OffscreenCanvasRenderingContext2D

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

The OffscreenCanvasRenderingContext2D interface is a {{domxref("CanvasRenderingContext2D")}}  rendering context for drawing to the bitmap of an OffscreenCanvas object. It is similar to the CanvasRenderingContext2D object, with the following differences:

Example

The following code snippet creates a {{domxref("Worker")}}  object using the {{domxref("Worker.Worker", "Worker()")}}  constructor. The transferControlToOffscreen() method is used to get an OffscreenCanvas object from the {{HtmlElement("canvas")}}  element so it can be transferred to the worker:

const canvas = document.getElementById("canvas");
const offscreen = canvas.transferControlToOffscreen();
const worker = new Worker("worker.js");
worker.postMessage({ canvas: offscreen }, [offscreen]);

In the worker thread, we can use the OffscreenCanvasRenderingContext2D to draw to the bitmap of the OffscreenCanvas object:

onmessage = (event) => {
  const canvas = event.data.canvas;
  const offCtx = canvas.getContext("2d");
  // draw to the offscreen canvas context
  offCtx.fillStyle = "red";
  offCtx.fillRect(0, 0, 100, 100);
};

For a full example, see our OffscreenCanvas worker example (run OffscreenCanvas worker).

Additional methods

The following method is new to the OffscreenCanvasRenderingContext2D interface and does not exist in the CanvasRenderingContext2D interface:

Unsupported features

The following user interface method is not supported by the OffscreenCanvasRenderingContext2D interface:

Inherited properties and methods

The following properties and methods are inherited from {{domxref("CanvasRenderingContext2D")}} . They have the same usage as in CanvasRenderingContext2D

Context

Drawing rectangles

Drawing text

The following methods and properties control drawing text. See also the {{domxref("TextMetrics")}}  object for text properties.

Line styles

The following methods and properties control how lines are drawn.

Text styles

The following properties control how text is laid out.

Fill and stroke styles

Fill styling is used for colors and styles inside shapes and stroke styling is used for the lines around shapes.

Gradients and patterns

Shadows

Paths

The following methods can be used to manipulate paths of objects.

Drawing paths

Transformations

Objects in the CanvasRenderingContext2D rendering context have a current transformation matrix and methods to manipulate it. The transformation matrix is applied when creating the current default path, painting text, shapes and {{domxref("Path2D")}}  objects. The methods listed below remain for historical and compatibility reasons as {{domxref("DOMMatrix")}}  objects are used in most parts of the API nowadays and will be used in the future instead.

Compositing

Drawing images

Pixel manipulation

See also the {{domxref("ImageData")}}  object.

Image smoothing

The canvas state

The CanvasRenderingContext2D rendering context contains a variety of drawing style states (attributes for line styles, fill styles, shadow styles, text styles). The following methods help you to work with that state:

Filters

Unsupported properties and methods

The following method is not supported in the OffscreenCanvasRenderingContext2D interface:

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN