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

## Canvas.getContext

> [VERSION] 基础库 >= 2.21.8

### 描述

获取 Canvas 绘图上下文

### 参数

`Params`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `contextType` | `"2d"` | 是 | 上下文类型，当前支持 '2d' |

### 返回值

类型: `CanvasRenderingContext2D`

Canvas 2D 渲染上下文

### 示例代码

#### 获取 2D 绘图上下文

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