ts-components-canvas-canvas.md 1.8 KB
Newer Older
E
ester.zhou 已提交
1
#  Canvas
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The **\<Canvas>** component can be used to customize drawings.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
Z
zengyawen 已提交
7
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

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

None

Z
zengyawen 已提交
14
## Child Components
Z
zengyawen 已提交
15

E
ester.zhou 已提交
16
Not supported
Z
zengyawen 已提交
17 18 19

## APIs

E
ester.zhou 已提交
20
Canvas(context?: CanvasRenderingContext2D)
Z
zengyawen 已提交
21

E
ester.zhou 已提交
22
**Parameters**
Z
zengyawen 已提交
23

E
ester.zhou 已提交
24 25 26
| Name    | Type                                    | Mandatory  | Default Value | Description                        |
| ------- | ---------------------------------------- | ---- | ---- | ---------------------------- |
| context | [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md) | No   | -    | For details, see **CanvasRenderingContext2D**.|
Z
zengyawen 已提交
27 28 29

## Attributes

E
ester.zhou 已提交
30
The [universal attributes](ts-universal-attributes-size.md) are supported.
Z
zengyawen 已提交
31 32 33

## Events

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

E
ester.zhou 已提交
36 37 38
| Name                           | Parameter  | Description                  |
| ----------------------------- | ---- | -------------------- |
| onReady(event: () => void) | -   | Triggered when a canvas is ready. When this event is triggered, the width and height of the canvas can be obtained, and you can use the canvas APIs to draw images.|
Z
zengyawen 已提交
39 40 41

## Example

E
ester.zhou 已提交
42 43
```ts
// xxx.ets
Z
zengyawen 已提交
44 45 46
@Entry
@Component
struct CanvasExample {
E
ester.zhou 已提交
47
  private settings: RenderingContextSettings = new RenderingContextSettings(true)
Z
zengyawen 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  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%')
  }
}
```