---
name: "Canvas.getContext"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.21.8" }
title: "Canvas.getContext - 获取 Canvas 绘图上下文"
---

## Canvas.getContext

> [VERSION] Base Library >= 2.21.8

### Description

Get the Canvas drawing context

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `contextType` | `"2d"` | Yes | Context type; currently supports '2d' |

### Return Value

Type: `CanvasRenderingContext2D`

Canvas 2D rendering context

### Examples

#### Get the 2D drawing context

```js
const canvas = await this.instance.getCanvasById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ff0000';
ctx.fillRect(0, 0, 150, 100);
```
