1e41f4b71Sopenharmony_ci# Applying Custom Drawing in the Widget
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciYou can apply custom drawing in your ArkTS widget to create a more vibrant experience. Use the [\<Canvas>](../reference/apis-arkui/arkui-ts/ts-components-canvas-canvas.md) component to create a canvas on the widget, and then use the [CanvasRenderingContext2D](../reference/apis-arkui/arkui-ts/ts-canvasrenderingcontext2d.md) object to draw custom graphics on the canvas. The following code snippet draws a smiling face in the center of a canvas.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci```ts
7e41f4b71Sopenharmony_ci@Entry
8e41f4b71Sopenharmony_ci@Component
9e41f4b71Sopenharmony_cistruct CustomCanvasDrawingCard {
10e41f4b71Sopenharmony_ci  private canvasWidth: number = 0;
11e41f4b71Sopenharmony_ci  private canvasHeight: number = 0;
12e41f4b71Sopenharmony_ci  // Initialize CanvasRenderingContext2D and RenderingContextSettings.
13e41f4b71Sopenharmony_ci  private settings: RenderingContextSettings = new RenderingContextSettings(true);
14e41f4b71Sopenharmony_ci  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci  build() {
17e41f4b71Sopenharmony_ci    Column() {
18e41f4b71Sopenharmony_ci      Row() {
19e41f4b71Sopenharmony_ci        Canvas(this.context)
20e41f4b71Sopenharmony_ci          .width('100%')
21e41f4b71Sopenharmony_ci          .height('100%')
22e41f4b71Sopenharmony_ci          .onReady(() => {
23e41f4b71Sopenharmony_ci            // Obtain the actual width and height of the canvas in the onReady callback.
24e41f4b71Sopenharmony_ci            this.canvasWidth = this.context.width;
25e41f4b71Sopenharmony_ci            this.canvasHeight = this.context.height;
26e41f4b71Sopenharmony_ci            // Draw the background of the canvas.
27e41f4b71Sopenharmony_ci            this.context.fillStyle = '#EEF0FF';
28e41f4b71Sopenharmony_ci            this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
29e41f4b71Sopenharmony_ci            // Draw a circle in the center of the canvas.
30e41f4b71Sopenharmony_ci            this.context.beginPath();
31e41f4b71Sopenharmony_ci            let radius = this.context.width / 3;
32e41f4b71Sopenharmony_ci            let circleX = this.context.width / 2;
33e41f4b71Sopenharmony_ci            let circleY = this.context.height / 2;
34e41f4b71Sopenharmony_ci            this.context.moveTo(circleX - radius, circleY);
35e41f4b71Sopenharmony_ci            this.context.arc(circleX, circleY, radius, 2 * Math.PI, 0, true);
36e41f4b71Sopenharmony_ci            this.context.closePath();
37e41f4b71Sopenharmony_ci            this.context.fillStyle = '#5A5FFF';
38e41f4b71Sopenharmony_ci            this.context.fill();
39e41f4b71Sopenharmony_ci            // Draw the left eye of the smiling face.
40e41f4b71Sopenharmony_ci            let leftR = radius / 13;
41e41f4b71Sopenharmony_ci            let leftX = circleX - (radius / 2.3);
42e41f4b71Sopenharmony_ci            let leftY = circleY - (radius / 4.5);
43e41f4b71Sopenharmony_ci            this.context.beginPath();
44e41f4b71Sopenharmony_ci            this.context.arc(leftX, leftY, leftR, 0, 2 * Math.PI, true);
45e41f4b71Sopenharmony_ci            this.context.closePath();
46e41f4b71Sopenharmony_ci            this.context.strokeStyle = '#FFFFFF';
47e41f4b71Sopenharmony_ci            this.context.lineWidth = 15;
48e41f4b71Sopenharmony_ci            this.context.stroke();
49e41f4b71Sopenharmony_ci            // Draw the right eye of the smiling face.
50e41f4b71Sopenharmony_ci            let rightR = radius / 13;
51e41f4b71Sopenharmony_ci            let rightX = circleX + (radius / 2.3);
52e41f4b71Sopenharmony_ci            let rightY = circleY - (radius / 4.5);
53e41f4b71Sopenharmony_ci            this.context.beginPath();
54e41f4b71Sopenharmony_ci            this.context.arc(rightX, rightY, rightR, 0, 2 * Math.PI, true);
55e41f4b71Sopenharmony_ci            this.context.closePath();
56e41f4b71Sopenharmony_ci            this.context.strokeStyle = '#FFFFFF';
57e41f4b71Sopenharmony_ci            this.context.lineWidth = 15;
58e41f4b71Sopenharmony_ci            this.context.stroke();
59e41f4b71Sopenharmony_ci            // Draw the nose of the smiling face.
60e41f4b71Sopenharmony_ci            let startX = circleX;
61e41f4b71Sopenharmony_ci            let startY = circleY - 20;
62e41f4b71Sopenharmony_ci            this.context.beginPath();
63e41f4b71Sopenharmony_ci            this.context.moveTo(startX, startY);
64e41f4b71Sopenharmony_ci            this.context.lineTo(startX - 8, startY + 40);
65e41f4b71Sopenharmony_ci            this.context.lineTo(startX + 8, startY + 40);
66e41f4b71Sopenharmony_ci            this.context.strokeStyle = '#FFFFFF';
67e41f4b71Sopenharmony_ci            this.context.lineWidth = 15;
68e41f4b71Sopenharmony_ci            this.context.lineCap = 'round';
69e41f4b71Sopenharmony_ci            this.context.lineJoin = 'round';
70e41f4b71Sopenharmony_ci            this.context.stroke();
71e41f4b71Sopenharmony_ci            // Draw the mouth of the smiling face.
72e41f4b71Sopenharmony_ci            let mouthR = radius / 2;
73e41f4b71Sopenharmony_ci            let mouthX = circleX;
74e41f4b71Sopenharmony_ci            let mouthY = circleY + 10;
75e41f4b71Sopenharmony_ci            this.context.beginPath();
76e41f4b71Sopenharmony_ci            this.context.arc(mouthX, mouthY, mouthR, Math.PI / 1.4, Math.PI / 3.4, true);
77e41f4b71Sopenharmony_ci            this.context.strokeStyle = '#FFFFFF';
78e41f4b71Sopenharmony_ci            this.context.lineWidth = 15;
79e41f4b71Sopenharmony_ci            this.context.stroke();
80e41f4b71Sopenharmony_ci            this.context.closePath();
81e41f4b71Sopenharmony_ci          })
82e41f4b71Sopenharmony_ci      }
83e41f4b71Sopenharmony_ci    }.height('100%').width('100%')
84e41f4b71Sopenharmony_ci  }
85e41f4b71Sopenharmony_ci}
86e41f4b71Sopenharmony_ci```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciThe figure below shows the effect.
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci![WidgetCanvasDemo](figures/WidgetCanvasDemo.jpeg)
91