1e41f4b71Sopenharmony_ci# Canvas 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **\<Canvas>** component can be used to customize drawings. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Child Components 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciNot supported 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## APIs 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Canvas 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciCanvas(context?: CanvasRenderingContext2D | DrawingRenderingContext) 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Parameters** 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 28e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 29e41f4b71Sopenharmony_ci| context | [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md) \| [DrawingRenderingContext<sup>12+</sup>](ts-drawingrenderingcontext.md) | No | 2D rendering context for a canvas.<br>**CanvasRenderingContext2D**: Canvases cannot share one **CanvasRenderingContext2D** object. For details, see [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md). **DrawingRenderingContext**: Canvases cannot share one **DrawingRenderingContext** object. For details, see [DrawingRenderingContext](ts-drawingrenderingcontext.md). | 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### Canvas<sup>12+</sup> 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciCanvas(context: CanvasRenderingContext2D | DrawingRenderingContext, imageAIOptions: ImageAIOptions) 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Parameters** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 40e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 41e41f4b71Sopenharmony_ci| context | [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md) \| [DrawingRenderingContext<sup>12+</sup>](ts-drawingrenderingcontext.md) | Yes | 2D rendering context for a canvas.<br>**CanvasRenderingContext2D**: Canvases cannot share one **CanvasRenderingContext2D** object. For details, see [CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md). **DrawingRenderingContext**: Canvases cannot share one **DrawingRenderingContext** object. For details, see [DrawingRenderingContext](ts-drawingrenderingcontext.md). | 42e41f4b71Sopenharmony_ci| imageAIOptions | [ImageAIOptions](ts-image-common.md#imageaioptions) | Yes | AI image analysis options. You can configure the analysis type or bind an analyzer controller through this parameter. | 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci## Attributes 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ciIn addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci### enableAnalyzer<sup>12+</sup> 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ciSpecifies whether to enable the AI image analyzer. This attribute must be used together with [StartImageAnalyzer](ts-canvasrenderingcontext2d.md#startimageanalyzer12) and [StopImageAnalyzer](ts-canvasrenderingcontext2d.md#stopimageanalyzer12) in [context](ts-canvasrenderingcontext2d.md). 51e41f4b71Sopenharmony_ciThis attribute cannot be used together with the [overlay](ts-universal-attributes-overlay.md#overlay) attribute. If they are set at the same time, the **CustomBuilder** attribute in **overlay** has no effect. This feature depends on device capabilities. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**Parameters** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 60e41f4b71Sopenharmony_ci| ------ | ------- | ---- | ------------------------------------------------------------ | 61e41f4b71Sopenharmony_ci| enable | boolean | Yes | Whether to enable the AI image analyzer. The value **true** means to enable the AI image analyzer.<br>Default value: **false** | 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci## Events 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 9. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciIn addition to the [universal events](ts-universal-events-click.md), the following events are supported. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci| Name | Description | 74e41f4b71Sopenharmony_ci| -------------------------- | ---------------------------------------- | 75e41f4b71Sopenharmony_ci| onReady(event: () => void) | Triggered when a canvas is ready or its size changes. When this event is triggered, the canvas is cleared. The width and height of the canvas can then be obtained, and you can use the canvas APIs to draw images. If the canvas is merely relocated, the **onAreaChange** event is triggered, but the **onReady** event is not.<br>The **onAreaChange** event is triggered after the **onReady** event. | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**Example** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciThis example describes how to use the methods in **CanvasRenderingContext2D** for drawing on a canvas. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci```ts 82e41f4b71Sopenharmony_ci// xxx.ets 83e41f4b71Sopenharmony_ci@Entry 84e41f4b71Sopenharmony_ci@Component 85e41f4b71Sopenharmony_cistruct CanvasExample { 86e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 87e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci build() { 90e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 91e41f4b71Sopenharmony_ci Canvas(this.context) 92e41f4b71Sopenharmony_ci .width('100%') 93e41f4b71Sopenharmony_ci .height('100%') 94e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 95e41f4b71Sopenharmony_ci .onReady(() => { 96e41f4b71Sopenharmony_ci this.context.fillRect(0, 30, 100, 100) 97e41f4b71Sopenharmony_ci }) 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci .width('100%') 100e41f4b71Sopenharmony_ci .height('100%') 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci} 103e41f4b71Sopenharmony_ci``` 104e41f4b71Sopenharmony_ci  105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ciThis example describes how to use the methods in **DrawingRenderingContext** for drawing on a canvas. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci```ts 110e41f4b71Sopenharmony_ci// xxx.ets 111e41f4b71Sopenharmony_ci@Entry 112e41f4b71Sopenharmony_ci@Component 113e41f4b71Sopenharmony_cistruct CanvasExample { 114e41f4b71Sopenharmony_ci private context: DrawingRenderingContext = new DrawingRenderingContext() 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci build() { 117e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 118e41f4b71Sopenharmony_ci Canvas(this.context) 119e41f4b71Sopenharmony_ci .width('100%') 120e41f4b71Sopenharmony_ci .height('100%') 121e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 122e41f4b71Sopenharmony_ci .onReady(() => { 123e41f4b71Sopenharmony_ci this.context.canvas.drawCircle(200, 200, 100) 124e41f4b71Sopenharmony_ci this.context.invalidate() 125e41f4b71Sopenharmony_ci }) 126e41f4b71Sopenharmony_ci } 127e41f4b71Sopenharmony_ci .width('100%') 128e41f4b71Sopenharmony_ci .height('100%') 129e41f4b71Sopenharmony_ci } 130e41f4b71Sopenharmony_ci} 131e41f4b71Sopenharmony_ci``` 132e41f4b71Sopenharmony_ci  133