1e41f4b71Sopenharmony_ci# Drawing Custom Graphics Using the Canvas (Canvas)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci**Canvas** provides a canvas component for drawing custom graphics. You can use the **CanvasRenderingContext2D** and **OffscreenCanvasRenderingContext2D** objects to draw graphics on the **Canvas** component. The drawing objects can be basic shapes, text, and images.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Drawing Custom Graphics on the Canvas
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciYou can draw custom graphics on the canvas in any of the following ways:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Use [CanvasRenderingContext2D](../reference/arkui-ts/ts-canvasrenderingcontext2d.md).
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci  ```ts
15e41f4b71Sopenharmony_ci  @Entry
16e41f4b71Sopenharmony_ci  @Component
17e41f4b71Sopenharmony_ci  struct CanvasExample1 {
18e41f4b71Sopenharmony_ci    // Configure the parameters of the CanvasRenderingContext2D object, including whether to enable anti-aliasing. The value true indicates that anti-aliasing is enabled.
19e41f4b71Sopenharmony_ci    private settings: RenderingContextSettings = new RenderingContextSettings(true)
20e41f4b71Sopenharmony_ci    // Create a CanvasRenderingContext2D object by calling CanvasRenderingContext2D object in Canvas.
21e41f4b71Sopenharmony_ci    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
22e41f4b71Sopenharmony_ci  
23e41f4b71Sopenharmony_ci    build() {
24e41f4b71Sopenharmony_ci      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
25e41f4b71Sopenharmony_ci        // Invoke the CanvasRenderingContext2D object in Canvas.
26e41f4b71Sopenharmony_ci        Canvas(this.context)
27e41f4b71Sopenharmony_ci          .width('100%')
28e41f4b71Sopenharmony_ci          .height('100%')
29e41f4b71Sopenharmony_ci          .backgroundColor('#F5DC62')
30e41f4b71Sopenharmony_ci          .onReady(() => {
31e41f4b71Sopenharmony_ci            // You can draw content here.
32e41f4b71Sopenharmony_ci            this.context.strokeRect(50, 50, 200, 150);
33e41f4b71Sopenharmony_ci          })
34e41f4b71Sopenharmony_ci      }
35e41f4b71Sopenharmony_ci      .width('100%')
36e41f4b71Sopenharmony_ci      .height('100%')
37e41f4b71Sopenharmony_ci    }
38e41f4b71Sopenharmony_ci  }
39e41f4b71Sopenharmony_ci  
40e41f4b71Sopenharmony_ci  ```
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci  ![2023022793003(1)](figures/2023022793003(1).jpg)
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci- Drawing offscreen onto a canvas is a process where content to draw onto the canvas is first drawn in the buffer, and then converted into a picture, and finally the picture is drawn on the canvas. This process increases the drawing efficiency. Specifically, the implementation is as follows:
45e41f4b71Sopenharmony_ci  1. Use the **transferToImageBitmap** API to create an **ImageBitmap** object for the image that is recently rendered off the screen canvas.
46e41f4b71Sopenharmony_ci  2. Use the **transferFromImageBitmap** API of the **CanvasRenderingContext2D** object to display the created **ImageBitmap** object.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci    For details, see [OffscreenCanvasRenderingContext2D](../reference/arkui-ts/ts-offscreencanvasrenderingcontext2d.md).
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci  ```ts
51e41f4b71Sopenharmony_ci  @Entry
52e41f4b71Sopenharmony_ci  @Component
53e41f4b71Sopenharmony_ci  struct CanvasExample2 {
54e41f4b71Sopenharmony_ci  // Configure the parameters of the CanvasRenderingContext2D and OffscreenCanvasRenderingContext2D objects, including whether to enable anti-aliasing. The value true indicates that anti-aliasing is enabled.
55e41f4b71Sopenharmony_ci    private settings: RenderingContextSettings = new RenderingContextSettings(true)
56e41f4b71Sopenharmony_ci    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
57e41f4b71Sopenharmony_ci  // Create an OffscreenCanvas object. width indicates the width of the offscreen canvas, and height indicates the height of the offscreen canvas.
58e41f4b71Sopenharmony_ci    private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
59e41f4b71Sopenharmony_ci   
60e41f4b71Sopenharmony_ci    build() {
61e41f4b71Sopenharmony_ci      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
62e41f4b71Sopenharmony_ci        Canvas(this.context)
63e41f4b71Sopenharmony_ci          .width('100%')
64e41f4b71Sopenharmony_ci          .height('100%')
65e41f4b71Sopenharmony_ci          .backgroundColor('#F5DC62')
66e41f4b71Sopenharmony_ci          .onReady(() =>{
67e41f4b71Sopenharmony_ci            let offContext = this.offCanvas.getContext("2d", this.settings)
68e41f4b71Sopenharmony_ci            // You can draw content here.
69e41f4b71Sopenharmony_ci            offContext.strokeRect(50, 50, 200, 150);
70e41f4b71Sopenharmony_ci            // Display the image rendered by the offscreen drawing value on the common canvas.
71e41f4b71Sopenharmony_ci            let image = this.offCanvas.transferToImageBitmap();
72e41f4b71Sopenharmony_ci            this.context.transferFromImageBitmap(image);
73e41f4b71Sopenharmony_ci          })
74e41f4b71Sopenharmony_ci      }
75e41f4b71Sopenharmony_ci      .width('100%')
76e41f4b71Sopenharmony_ci      .height('100%')
77e41f4b71Sopenharmony_ci    }
78e41f4b71Sopenharmony_ci  }
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci  ```
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci  ![2023022793003(1)](figures/2023022793003(1).jpg)
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci  >**NOTE**
85e41f4b71Sopenharmony_ci  >
86e41f4b71Sopenharmony_ci  >The APIs called for drawing on the canvas through the **CanvasRenderingContext2D** and **OffscreenCanvasRenderingContext2D** objects are the same. Unless otherwise specified, the value unit of the parameters in these APIs is vp.
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci- Before loading the Lottie animation on the canvas, download the Lottie as follows:
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci  ```ts
91e41f4b71Sopenharmony_ci  import lottie from '@ohos/lottie'
92e41f4b71Sopenharmony_ci  ```
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci  For details about the APIs, see [Lottie](https://gitee.com/openharmony-tpc/lottieETS).
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci## Initializing the Canvas Component
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**onReady(event: () => void)** is the event callback when the **Canvas** component initialization is complete. After this event is called, the determined width and height of the **Canvas** component can be obtained. The **CanvasRenderingContext2D** and **OffscreenCanvasRenderingContext2D** objects can then be used to call related APIs to draw graphics.
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci```ts
102e41f4b71Sopenharmony_ciCanvas(this.context)
103e41f4b71Sopenharmony_ci  .width('100%')
104e41f4b71Sopenharmony_ci  .height('100%')
105e41f4b71Sopenharmony_ci  .backgroundColor('#F5DC62')
106e41f4b71Sopenharmony_ci  .onReady(() => {
107e41f4b71Sopenharmony_ci    this.context.fillStyle = '#0097D4';
108e41f4b71Sopenharmony_ci    this.context.fillRect(50, 50, 100, 100);
109e41f4b71Sopenharmony_ci  })
110e41f4b71Sopenharmony_ci```
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci![2023022793350(1)](figures/2023022793350(1).jpg)
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci## Canvas Component Drawing Modes
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ciTwo modes are available for drawing with the **Canvas** component: Alternatively, you can separately define the **Path2d** object to build an ideal path without the **Canvas** component and **onReady()** lifecycle callback, and then use the **Canvas** component for drawing after **onReady()** is invoked.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci- After the **onReady()** callback of the **Canvas** component is invoked, use the **CanvasRenderingContext2D** and **OffscreenCanvasRenderingContext2D** objects to call related APIs for drawing.
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci  ```ts
122e41f4b71Sopenharmony_ci  Canvas(this.context)
123e41f4b71Sopenharmony_ci    .width('100%')
124e41f4b71Sopenharmony_ci    .height('100%')
125e41f4b71Sopenharmony_ci    .backgroundColor('#F5DC62')
126e41f4b71Sopenharmony_ci    .onReady(() =>{
127e41f4b71Sopenharmony_ci      this.context.beginPath();
128e41f4b71Sopenharmony_ci      this.context.moveTo(50, 50);
129e41f4b71Sopenharmony_ci      this.context.lineTo(280, 160);
130e41f4b71Sopenharmony_ci      this.context.stroke();
131e41f4b71Sopenharmony_ci     })
132e41f4b71Sopenharmony_ci  ```
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci  ![2023022793719(1)](figures/2023022793719(1).jpg)
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci- Define an individual **path2d** object to build an ideal path, and then call the **stroke** or **fill** API of the **CanvasRenderingContext2D** and **OffscreenCanvasRenderingContext2D** objects to draw the path. For details, see [Path2D](../reference/arkui-ts/ts-components-canvas-path2d.md).
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci  ```ts
139e41f4b71Sopenharmony_ci  Canvas(this.context)
140e41f4b71Sopenharmony_ci    .width('100%')
141e41f4b71Sopenharmony_ci    .height('100%')
142e41f4b71Sopenharmony_ci    .backgroundColor('#F5DC62')
143e41f4b71Sopenharmony_ci    .onReady(() =>{
144e41f4b71Sopenharmony_ci       let region = new Path2D();
145e41f4b71Sopenharmony_ci       region.arc(100, 75, 50, 0, 6.28);
146e41f4b71Sopenharmony_ci       this.context.stroke(region);
147e41f4b71Sopenharmony_ci    })
148e41f4b71Sopenharmony_ci  ```
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci  ![2023022794031(1)](figures/2023022794031(1).jpg)
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci## Common Usage of the Canvas Component
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci**OffscreenCanvasRenderingContext2D** and **CanvasRenderingContext2D** provide a large number of attributes and methods, which can be used to draw text and graphics and process pixels. They are the core of the **Canvas** component. Common APIs include [fill](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#fill), [clip](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#clip), and [stroke](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#stroke). In addition, attributes such as [fillStyle](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#fillstyle), [globalAlpha](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#globalalpha), and [strokeStyle](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#strokestyle) are provided to spruce up the graphics. This topic describes typical usage of the canvas.
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci- Draw a basic shape.
158e41f4b71Sopenharmony_ci  You can draw a basic shape by calling APIs such as [arc](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#arc), [ellipse](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#ellipse), and [rect](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#rect).
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci  ```ts
161e41f4b71Sopenharmony_ci  Canvas(this.context)
162e41f4b71Sopenharmony_ci    .width('100%')
163e41f4b71Sopenharmony_ci    .height('100%')
164e41f4b71Sopenharmony_ci    .backgroundColor('#F5DC62')
165e41f4b71Sopenharmony_ci    .onReady(() =>{
166e41f4b71Sopenharmony_ci       // Draw a rectangle.
167e41f4b71Sopenharmony_ci       this.context.beginPath();
168e41f4b71Sopenharmony_ci       this.context.rect(100, 50, 100, 100);
169e41f4b71Sopenharmony_ci       this.context.stroke();
170e41f4b71Sopenharmony_ci       // Draw a circle on the canvas.
171e41f4b71Sopenharmony_ci       this.context.beginPath();
172e41f4b71Sopenharmony_ci       this.context.arc(150, 250, 50, 0, 6.28);
173e41f4b71Sopenharmony_ci       this.context.stroke();
174e41f4b71Sopenharmony_ci       // Draw an oval on the canvas.
175e41f4b71Sopenharmony_ci       this.context.beginPath();
176e41f4b71Sopenharmony_ci       this.context.ellipse(150, 450, 50, 100, Math.PI * 0.25, Math.PI * 0, Math.PI * 2);
177e41f4b71Sopenharmony_ci       this.context.stroke();
178e41f4b71Sopenharmony_ci    })
179e41f4b71Sopenharmony_ci  ```
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci  ![2023022794521(1)](figures/2023022794521(1).jpg)
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci- Draw text.
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci  You can use APIs such as [fillText](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#filltext) and [strokeText](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#stroketext) to draw text.
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci  ```ts
188e41f4b71Sopenharmony_ci  Canvas(this.context)
189e41f4b71Sopenharmony_ci    .width('100%')
190e41f4b71Sopenharmony_ci    .height('100%')
191e41f4b71Sopenharmony_ci    .backgroundColor('#F5DC62')
192e41f4b71Sopenharmony_ci    .onReady(() =>{
193e41f4b71Sopenharmony_ci       // Draw filled text on the canvas.
194e41f4b71Sopenharmony_ci       this.context.font = '50px sans-serif';
195e41f4b71Sopenharmony_ci       this.context.fillText("Hello World!", 50, 100);
196e41f4b71Sopenharmony_ci       // Draw a text stroke on the canvas.
197e41f4b71Sopenharmony_ci       this.context.font = '55px sans-serif';
198e41f4b71Sopenharmony_ci       this.context.strokeText("Hello World!", 50, 150);
199e41f4b71Sopenharmony_ci    })
200e41f4b71Sopenharmony_ci  ```
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci  ![2023022795105(1)](figures/2023022795105(1).jpg)
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci- Draw images and processes image pixel information.
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci  You can draw an image by calling APIs such as [drawImage](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#drawimage) and [putImageData](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#putimagedata). You can also process image pixel information by calling APIs such as [createImageData](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#createimagedata), [getPixelMap](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#getpixelmap), and [getImageData](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#getimagedata).
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci  ```ts
209e41f4b71Sopenharmony_ci  @Entry
210e41f4b71Sopenharmony_ci  @Component
211e41f4b71Sopenharmony_ci  struct GetImageData {
212e41f4b71Sopenharmony_ci   private settings: RenderingContextSettings = new RenderingContextSettings(true)
213e41f4b71Sopenharmony_ci   private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
214e41f4b71Sopenharmony_ci   private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600)
215e41f4b71Sopenharmony_ci   private img:ImageBitmap = new ImageBitmap("/common/images/1234.png")
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci    build() {
218e41f4b71Sopenharmony_ci      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
219e41f4b71Sopenharmony_ci        Canvas(this.context)
220e41f4b71Sopenharmony_ci          .width('100%')
221e41f4b71Sopenharmony_ci          .height('100%')
222e41f4b71Sopenharmony_ci          .backgroundColor('#F5DC62')
223e41f4b71Sopenharmony_ci          .onReady(() =>{
224e41f4b71Sopenharmony_ci            let offContext = this.offCanvas.getContext("2d", this.settings)
225e41f4b71Sopenharmony_ci            // Use the drawImage API to draw an image in the area with the width and height of 130 starting from (0, 0).
226e41f4b71Sopenharmony_ci            offContext.drawImage(this.img,0,0,130,130);
227e41f4b71Sopenharmony_ci            // Use the getImageData API to obtain the image data with the width and height of 130 starting from (50, 50).
228e41f4b71Sopenharmony_ci            let imagedata = offContext.getImageData(50,50,130,130);
229e41f4b71Sopenharmony_ci            // Use the putImageData API to draw the obtained image data in the area starting from (150, 150).
230e41f4b71Sopenharmony_ci            offContext.putImageData(imagedata,150,150);
231e41f4b71Sopenharmony_ci            // Draw the offscreen drawing content to the canvas.
232e41f4b71Sopenharmony_ci            let image = this.offCanvas.transferToImageBitmap();
233e41f4b71Sopenharmony_ci            this.context.transferFromImageBitmap(image);
234e41f4b71Sopenharmony_ci          })
235e41f4b71Sopenharmony_ci      }
236e41f4b71Sopenharmony_ci      .width('100%')
237e41f4b71Sopenharmony_ci      .height('100%')
238e41f4b71Sopenharmony_ci    }
239e41f4b71Sopenharmony_ci  }
240e41f4b71Sopenharmony_ci  ```
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci  ![drawimage](figures/drawimage.PNG)
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci- Other usage
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci  **Canvas** also provides other usage. For example, regarding [CanvasGradient](../reference/arkui-ts/ts-components-canvas-canvasgradient.md), you can create a linear gradient with [createLinearGradient](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#createlineargradient) or create a radial gradient with [createRadialGradient](../reference/arkui-ts/ts-canvasrenderingcontext2d.md#createradialgradient), among others.
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci  ```ts
249e41f4b71Sopenharmony_ci    Canvas(this.context)
250e41f4b71Sopenharmony_ci    .width('100%')
251e41f4b71Sopenharmony_ci    .height('100%')
252e41f4b71Sopenharmony_ci    .backgroundColor('#F5DC62')
253e41f4b71Sopenharmony_ci    .onReady(() =>{
254e41f4b71Sopenharmony_ci       // Create a CanvasGradient object with radial gradient colors.
255e41f4b71Sopenharmony_ci       let grad = this.context.createRadialGradient(200,200,50, 200,200,200)
256e41f4b71Sopenharmony_ci       // Set the gradient color stop for the CanvasGradient object, including the offset and colors.
257e41f4b71Sopenharmony_ci       grad.addColorStop(0.0, '#E87361');
258e41f4b71Sopenharmony_ci       grad.addColorStop(0.5, '#FFFFF0');
259e41f4b71Sopenharmony_ci       grad.addColorStop(1.0, '#BDDB69');
260e41f4b71Sopenharmony_ci       // Fill the rectangle with the CanvasGradient object.
261e41f4b71Sopenharmony_ci       this.context.fillStyle = grad;
262e41f4b71Sopenharmony_ci       this.context.fillRect(0, 0, 400, 400);
263e41f4b71Sopenharmony_ci    })
264e41f4b71Sopenharmony_ci  ```
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci  ![2023022700701(1)](figures/2023022700701(1).jpg)
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci## Example Scenario
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci- Draw a basic shape.
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci  ```ts
274e41f4b71Sopenharmony_ci  @Entry
275e41f4b71Sopenharmony_ci  @Component
276e41f4b71Sopenharmony_ci  struct ClearRect {
277e41f4b71Sopenharmony_ci   private settings: RenderingContextSettings = new RenderingContextSettings(true);
278e41f4b71Sopenharmony_ci   private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci    build() {
281e41f4b71Sopenharmony_ci      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
282e41f4b71Sopenharmony_ci        Canvas(this.context)
283e41f4b71Sopenharmony_ci          .width('100%')
284e41f4b71Sopenharmony_ci          .height('100%')
285e41f4b71Sopenharmony_ci          .backgroundColor('#F5DC62')
286e41f4b71Sopenharmony_ci          .onReady(() =>{
287e41f4b71Sopenharmony_ci            // Set the fill color to blue.
288e41f4b71Sopenharmony_ci            this.context.fillStyle = '#0097D4';
289e41f4b71Sopenharmony_ci            // Take (50, 50) as the upper left corner and draw a rectangle with the width and height of 200.
290e41f4b71Sopenharmony_ci            this.context.fillRect(50,50,200,200);
291e41f4b71Sopenharmony_ci            // Use (70, 70) as the upper left corner and clear the area with the width of 150 and height of 100.
292e41f4b71Sopenharmony_ci            this.context.clearRect(70,70,150,100);
293e41f4b71Sopenharmony_ci        })
294e41f4b71Sopenharmony_ci      }
295e41f4b71Sopenharmony_ci      .width('100%')
296e41f4b71Sopenharmony_ci      .height('100%')
297e41f4b71Sopenharmony_ci    }
298e41f4b71Sopenharmony_ci  }
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci  ```
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci  ![2023022701120(1)](figures/2023022701120(1).jpg)
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci- Draw an irregular shape.
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci  ```ts
307e41f4b71Sopenharmony_ci  @Entry
308e41f4b71Sopenharmony_ci  @Component
309e41f4b71Sopenharmony_ci  struct Path2d {
310e41f4b71Sopenharmony_ci    private settings: RenderingContextSettings = new RenderingContextSettings(true);
311e41f4b71Sopenharmony_ci    private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
312e41f4b71Sopenharmony_ci    
313e41f4b71Sopenharmony_ci    build() {
314e41f4b71Sopenharmony_ci      Row() {
315e41f4b71Sopenharmony_ci        Column() {
316e41f4b71Sopenharmony_ci          Canvas(this.context)
317e41f4b71Sopenharmony_ci            .width('100%')
318e41f4b71Sopenharmony_ci            .height('100%')
319e41f4b71Sopenharmony_ci            .backgroundColor('#F5DC62')
320e41f4b71Sopenharmony_ci            .onReady(() =>{
321e41f4b71Sopenharmony_ci              // Use the Path2D API to create a pentagon.
322e41f4b71Sopenharmony_ci              let path = new Path2D();
323e41f4b71Sopenharmony_ci              path.moveTo(150, 50);
324e41f4b71Sopenharmony_ci              path.lineTo(50, 150);
325e41f4b71Sopenharmony_ci              path.lineTo(100, 250);
326e41f4b71Sopenharmony_ci              path.lineTo(200, 250);
327e41f4b71Sopenharmony_ci              path.lineTo(250, 150);
328e41f4b71Sopenharmony_ci              path.closePath();
329e41f4b71Sopenharmony_ci              // Set the fill color to blue.
330e41f4b71Sopenharmony_ci              this.context.fillStyle = '#0097D4';
331e41f4b71Sopenharmony_ci              // Draw the pentagon described by Path2D in the canvas in fill mode.
332e41f4b71Sopenharmony_ci              this.context.fill(path);
333e41f4b71Sopenharmony_ci            })
334e41f4b71Sopenharmony_ci        }
335e41f4b71Sopenharmony_ci        .width('100%')
336e41f4b71Sopenharmony_ci      }
337e41f4b71Sopenharmony_ci      .height('100%')
338e41f4b71Sopenharmony_ci    }
339e41f4b71Sopenharmony_ci  }
340e41f4b71Sopenharmony_ci  ```
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci  ![2023032422159](figures/2023032422159.jpg)
343