ts-components-canvas-canvas.md 1.6 KB
Newer Older
Z
zengyawen 已提交
1
# Canvas
Z
zengyawen 已提交
2 3


E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9 10 11

The **<Canvas>** component can be used to customize drawings.


## Required Permissions
Z
zengyawen 已提交
12 13 14

None

Z
zengyawen 已提交
15 16

## Child Components
Z
zengyawen 已提交
17 18 19

None

Z
zengyawen 已提交
20 21 22 23 24 25

## APIs

Canvas(context: CanvasRenderingContext2D)

- Parameters
E
esterzhou 已提交
26
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
28
  | context | [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md) | Yes | - | For details, see CanvasRenderingContext2D. |
Z
zengyawen 已提交
29 30 31 32


## Attributes

E
ester.zhou 已提交
33
[Universal attributes]( ts-universal-attributes-index.md) are supported.
Z
zengyawen 已提交
34 35 36 37


## Events

E
ester.zhou 已提交
38
In addition to [universal events](ts-universal-events-index.md), the following events are supported.
Z
zengyawen 已提交
39

E
ester.zhou 已提交
40
| Name | Parameter | Description |
Z
zengyawen 已提交
41
| -------- | -------- | -------- |
E
ester.zhou 已提交
42
| onReady(callback: () => void) | None | Triggered when . |
Z
zengyawen 已提交
43 44 45 46


## Example

Z
zengyawen 已提交
47 48 49 50 51

```
@Entry
@Component
struct CanvasExample {
E
ester.zhou 已提交
52
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Canvas(this.context)
        .width('100%')
        .height('100%')
        .backgroundColor('#ffff00')
        .onReady(() =>{
          this.context.fillRect(0,30,100,100)
        })
    }
    .width('100%')
    .height('100%')
  }
}
```