files/en-us/web/api/offscreencanvasrenderingcontext2d/index.md
{{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:
drawFocusIfNeeded)canvas attribute refers to an OffscreenCanvas object rather than a {{HtmlElement("canvas")}} elementOffscreenCanvas object is updated during the rendering update of the Window or Worker that owns the OffscreenCanvasThe 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).
The following method is new to the OffscreenCanvasRenderingContext2D interface and does not exist in the CanvasRenderingContext2D interface:
OffscreenCanvas object's placeholder {{HtmlElement("canvas")}} element.The following user interface method is not supported by the OffscreenCanvasRenderingContext2D interface:
The following properties and methods are inherited from {{domxref("CanvasRenderingContext2D")}}. They have the same usage as in CanvasRenderingContext2D
true if the rendering context was lost.The following methods and properties control drawing text. See also the {{domxref("TextMetrics")}} object for text properties.
auto (default), optimizeSpeed, optimizeLegibility,The following methods and properties control how lines are drawn.
1.0.butt (default), round, square.round, bevel, miter (default).10.The following properties control how text is laid out.
10px sans-serif.start (default), end, left, right, center.top, hanging, middle, alphabetic (default), ideographic, bottom.ltr, rtl, inherit (default).0px.auto (default), normal, none.ultra-condensed, extra-condensed, condensed, semi-condensed, normal (default), semi-expanded, expanded, extra-expanded, ultra-expanded.normal (default), small-caps, all-small-caps, petite-caps, all-petite-caps, unicase, titling-caps.auto (default), optimizeSpeed, optimizeLegibility, geometricPrecision.0pxFill styling is used for colors and styles inside shapes and stroke styling is used for the lines around shapes.
black.black.0.0.0.The following methods can be used to manipulate paths of objects.
clip() is called appears inside the clipping path only. For an example, see Clipping paths in the Canvas tutorial.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.
transform() method with the same arguments.1.0 (opaque).globalAlpha applied this sets how shapes and images are drawn onto the existing bitmap.See also the {{domxref("ImageData")}} object.
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:
restore().save().OffscreenCanvas object.The following method is not supported in the OffscreenCanvasRenderingContext2D interface:
{{Specifications}}
{{Compat}}