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

  • there is no support for user-interface features (drawFocusIfNeeded, and scrollPathIntoView)
  • its canvas attribute refers to an OffscreenCanvas object rather than a <canvas> element
  • it has a commit() method for pushing rendered images to the context's OffscreenCanvas object's placeholder <canvas> element


The following code snippet creates a Worker object using the Worker() constructor. The transferControlToOffscreen() method is used to transfer the OffscreenCanvas object 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 =;
  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:


Pushes the rendered image to the context's OffscreenCanvas object's placeholder <canvas> element.

Unsupported features

The following user interface methods are not supported by the OffscreenCanvasRenderingContext2D interface:


If a given element is focused, this method draws a focus ring around the current path.

CanvasRenderingContext2D.scrollPathIntoView() Experimental

Scrolls the current path or a given path into the view.

Inherited properties and methods

The following properties and methods are inherited from CanvasRenderingContext2D. They have the same usage as in CanvasRenderingContext2D


CanvasRenderingContext2D.isContextLost() Experimental

Returns true if the rendering context was lost.

Drawing rectangles


Sets all pixels in the rectangle defined by starting point (x, y) and size (width, height) to transparent black, erasing any previously drawn content.


Draws a filled rectangle at (x, y) position whose size is determined by width and height.


Paints a rectangle which has a starting point at (x, y) and has a w width and an h height onto the canvas, using the current stroke style.

Drawing text

The following methods and properties control drawing text. See also the TextMetrics object for text properties.


Draws (fills) a given text at the given (x, y) position.


Draws (strokes) a given text at the given (x, y) position.


Returns a TextMetrics object.

CanvasRenderingContext2D.textRendering Experimental

Text rendering. Possible values: auto (default), optimizeSpeed, optimizeLegibility,

Line styles

The following methods and properties control how lines are drawn.


Width of lines. Default 1.0.


Type of endings on the end of lines. Possible values: butt (default), round, square.


Defines the type of corners where two lines meet. Possible values: round, bevel, miter (default).


Miter limit ratio. Default 10.


Returns the current line dash pattern array containing an even number of non-negative numbers.


Sets the current line dash pattern.


Specifies where to start a dash array on a line.

Text styles

The following properties control how text is laid out.


Font setting. Default value 10px sans-serif.


Text alignment setting. Possible values: start (default), end, left, right, center.


Baseline alignment setting. Possible values: top, hanging, middle, alphabetic (default), ideographic, bottom.


Directionality. Possible values: ltr, rtl, inherit (default).

CanvasRenderingContext2D.letterSpacing Experimental

Letter spacing. Default: 0px.


Font kerning. Possible values: auto (default), normal, none.

CanvasRenderingContext2D.fontStretch Experimental

Font stretch. Possible values: ultra-condensed, extra-condensed, condensed, semi-condensed, normal (default), semi-expanded, expanded, extra-expanded, ultra-expanded.

CanvasRenderingContext2D.fontVariantCaps Experimental

Font variant caps. Possible values: normal (default), small-caps, all-small-caps, petite-caps, all-petite-caps, unicase, titling-caps.

CanvasRenderingContext2D.textRendering Experimental

Text rendering. Possible values: auto (default), optimizeSpeed, optimizeLegibility, geometricPrecision.

CanvasRenderingContext2D.wordSpacing Experimental

Word spacing. Default value: 0px

Fill and stroke styles

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


Color or style to use inside shapes. Default #000 (black).


Color or style to use for the lines around shapes. Default #000 (black).

Gradients and patterns


Creates a conic gradient around a point given by coordinates represented by the parameters.


Creates a linear gradient along the line given by the coordinates represented by the parameters.


Creates a radial gradient given by the coordinates of the two circles represented by the parameters.


Creates a pattern using the specified image. It repeats the source in the directions specified by the repetition argument. This method returns a CanvasPattern.



Specifies the blurring effect. Default: 0.


Color of the shadow. Default: fully-transparent black.


Horizontal distance the shadow will be offset. Default: 0.


Vertical distance the shadow will be offset. Default: 0.


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


Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.


Causes the point of the pen to move back to the start of the current sub-path. It tries to draw a straight line from the current point to the start. If the shape has already been closed or has only one point, this function does nothing.


Moves the starting point of a new sub-path to the (x, y) coordinates.


Connects the last point in the current sub-path to the specified (x, y) coordinates with a straight line.


Adds a cubic Bézier curve to the current path.


Adds a quadratic Bézier curve to the current path.


Adds a circular arc to the current path.


Adds an arc to the current path with the given control points and radius, connected to the previous point by a straight line.


Adds an elliptical arc to the current path.


Creates a path for a rectangle at position (x, y) with a size that is determined by width and height.

Drawing paths


Fills the current sub-paths with the current fill style.


Strokes the current sub-paths with the current stroke style.


Creates a clipping path from the current sub-paths. Everything drawn after clip() is called appears inside the clipping path only. For an example, see Clipping paths in the Canvas tutorial.


Reports whether or not the specified point is contained in the current path.


Reports whether or not the specified point is inside the area contained by the stroking of a path.


Addition to CanvasPath that allows users to render rectangles with rounded corners.


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 Path2D objects. The methods listed below remain for historical and compatibility reasons as DOMMatrix objects are used in most parts of the API nowadays and will be used in the future instead.


Retrieves the current transformation matrix being applied to the context.


Adds a rotation to the transformation matrix. The angle argument represents a clockwise rotation angle and is expressed in radians.


Adds a scaling transformation to the canvas units by x horizontally and by y vertically.


Adds a translation transformation by moving the canvas and its origin x horizontally and y vertically on the grid.


Multiplies the current transformation matrix with the matrix described by its arguments.


Resets the current transform to the identity matrix, and then invokes the transform() method with the same arguments.


Resets the current transform by the identity matrix.



Alpha value that is applied to shapes and images before they are composited onto the canvas. Default 1.0 (opaque).


With globalAlpha applied this sets how shapes and images are drawn onto the existing bitmap.

Drawing images


Draws the specified image. This method is available in multiple formats, providing a great deal of flexibility in its use.

Pixel manipulation

See also the ImageData object.


Creates a new, blank ImageData object with the specified dimensions. All of the pixels in the new object are transparent black.


Returns an ImageData object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has an sw width and sh height.


Paints data from the given ImageData object onto the bitmap. If a dirty rectangle is provided, only the pixels from that rectangle are painted.

Image smoothing


Image smoothing mode; if disabled, images will not be smoothed if scaled.


Allows you to set the quality of 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:

Saves the current drawing style state using a stack so you can revert any change you make to it using restore().


Restores the drawing style state to the last element on the 'state stack' saved by save().


A read-only reference to an OffscreenCanvas object.


Returns an object containing the actual context attributes. Context attributes can be requested with HTMLCanvasElement.getContext().


Resets the current drawing style state to the default values.



Applies a CSS or SVG filter to the canvas; e.g., to change its brightness or blurriness.

Unsupported properties and methods

The following methods are not supported in the OffscreenCanvasRenderingContext2D interface:


If a given element is focused, this method draws a focus ring around the current path.

CanvasRenderingContext2D.scrollPathIntoView() Experimental

Scrolls the current path or a given path into the view.


HTML Standard
# the-offscreen-2d-rendering-context

Browser compatibility

BCD tables only load in the browser

See also