---
name: "Canvas.requestAnimationFrame"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.21.8" }
title: "Canvas.requestAnimationFrame - 请求动画帧"
---

## Canvas.requestAnimationFrame

> [VERSION] Base Library >= 2.21.8

### Description

Schedule a requestAnimationFrame callback that calls the specified function before the next repaint

### Parameters

`(callback: Function)`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `callback` | `(time: number) => void` | Yes | Callback to run before the next repaint |

### Return Value

None


### Examples

#### Use requestAnimationFrame to render animations

```js
const canvas = await this.instance.getCanvasById('myCanvas');
const ctx = canvas.getContext('2d');
let x = 0;
function render() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.fillRect(x++, 0, 50, 50);
  canvas.requestAnimationFrame(render);
}
canvas.requestAnimationFrame(render);
```
