Canvas.getContext
JS
RenderingContext Canvas.getContext(string contextType, Object contextAttribute)Gets the rendering context of a canvas object.
A single canvas can only obtain one rendering context.
- When using the webgl or webgl2 context:
- The canvas corresponding to the current rendering context can be displayed on screen.
- If multiple canvases exist, other canvases attempting to obtain a webgl rendering context will get an empty object.
- Because WebGL rendering depends on the screen width and height, the width and height properties of the canvas corresponding to the current rendering context cannot be modified.
- When using the 2d context:
- Content drawn via the current rendering context can only be stored in memory.
- The width and height properties of the canvas corresponding to the current rendering context can be modified.
Parameters
- string contextType: The type of context to obtain. Valid values are as follows.
contextType valid values:
| Value | Description |
|---|---|
| 2d | 2d rendering context |
| webgl | webgl rendering context |
| experimental-webgl | webgl rendering context |
| webgl2 | webgl2 rendering context |
| experimental-webgl2 | webgl2 rendering context |
- Object contextAttributes: Properties for the webgl or webgl2 context. Has no effect when
contextTypeis2d.
contextAttributes properties:
| Property | Type | Default | Description |
|---|---|---|---|
| alpha | boolean | true | Whether to include an alpha buffer |
| depth | boolean | true | If true, the drawing buffer includes a depth buffer of at least 16 bits |
| stencil | boolean | true | If true, the drawing buffer includes a stencil buffer of at least 8 bits |
| antialias | boolean | false | Whether to enable antialiasing |
| preserveDrawingBuffer | boolean | false | If true, the buffer will not be cleared and will be preserved until it is cleared or overwritten by the user |
Return Value
- RenderingContext: The rendering context
