--- title: OffscreenCanvas.getContext() slug: Web/API/OffscreenCanvas/getContext translation_of: Web/API/OffscreenCanvas/getContext ---
{{APIRef("Canvas API")}} {{SeeCompatTable}}

The OffscreenCanvas.getContext() method returns a drawing context for an offscreen canvas, or {{jsxref("null")}} if the context identifier is not supported.

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

構文

offscreen.getContext(contextType, contextAttributes);

引数

contextType
Is a {{domxref("DOMString")}} containing the context identifier defining the drawing context associated to the canvas. Possible values are:

Note: The identifiers "experimental-webgl" or "experimental-webgl2" are also used in implementations of WebGL. These implementations have not reached test suite conformance, or the graphic drivers situation on the platform is not yet stable. The Khronos Group certifies WebGL implementations under certain conformance rules.

contextAttributes

You can use several context attributes when creating your rendering context, for example:

offscreen.getContext("webgl",
                 { antialias: false,
                   depth: false });
2d context attributes: WebGL context attributes:

 返値

A {{domxref("RenderingContext")}} which is either a

If the contextType doesn't match a possible drawing context, null is returned.

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

gl; // WebGLRenderingContext
gl.canvas; // OffscreenCanvas

仕様書

Specification Status Comment
{{SpecName('HTML WHATWG', "#dom-offscreencanvas-getcontext", "OffscreenCanvas.getContext()")}} {{Spec2('HTML WHATWG')}}

ブラウザの互換性

{{Compat("api.OffscreenCanvas.getContext")}}

See also