1e41f4b71Sopenharmony_ci# Drawing Geometric Shapes (Shape) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThe drawing components are used to draw graphs on the page. The **\<Shape>** component is the parent component of the drawing components. The attributes of **\<Shape>** are universal attributes supported by all the drawing components. For details, see [Shape](../reference/apis-arkui/arkui-ts/ts-drawing-components-shape.md). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Creating a Drawing Component 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciA drawing component can be created in either of the following ways: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Create a drawing component with **\<Shape>** as their parent to implement the effect similar to SVG. The API used is as follows: 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci ```ts 14e41f4b71Sopenharmony_ci Shape(value?: PixelMap) 15e41f4b71Sopenharmony_ci ``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci In the API, the **value** parameter sets the drawing target. You can draw a graph in the specified **PixelMap** object. If the **value** parameter is not set, the graph is drawn in the current drawing target. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci ```ts 20e41f4b71Sopenharmony_ci Shape() { 21e41f4b71Sopenharmony_ci Rect().width(300).height(50) 22e41f4b71Sopenharmony_ci } 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci- Create an independent drawing component to draw a specific shape. Seven shapes are supported: [Circle](../reference/apis-arkui/arkui-ts/ts-drawing-components-circle.md), [Ellipse](../reference/apis-arkui/arkui-ts/ts-drawing-components-ellipse.md), [Line](../reference/apis-arkui/arkui-ts/ts-drawing-components-line.md), [Polyline](../reference/apis-arkui/arkui-ts/ts-drawing-components-polyline.md), [Polygon](../reference/apis-arkui/arkui-ts/ts-drawing-components-polygon.md), [Path](../reference/apis-arkui/arkui-ts/ts-drawing-components-path.md), and [Rect](../reference/apis-arkui/arkui-ts/ts-drawing-components-rect.md). The following uses the **Circle** API as an example: 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci ```ts 29e41f4b71Sopenharmony_ci Circle(options?: {width?: string | number, height?: string | number} 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci This API draws a circle on a page. The **width** parameter indicates the width of the circle, and the **height** parameter indicates the height of the circle. The diameter of the circle is determined by the minimum width and height. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci ```ts 35e41f4b71Sopenharmony_ci Circle({ width: 150, height: 150 }) 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci  39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci## Viewport 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci```ts 45e41f4b71Sopenharmony_civiewPort{ x?: number | string, y?: number | string, width?: number | string, height?: number | string } 46e41f4b71Sopenharmony_ci``` 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciCreates a viewport, which is a rectangle in the user space that maps to the view boundary established for the associated SVG element. Among the four optional parameters, **x** and **y** represent the coordinates of the upper left corner of the viewport, and **width** and **height** represent the size of the viewport. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ciThe following examples describe how to use the viewport: 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci- Zoom in or zoom out a graph through the shape viewport. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci ```ts 55e41f4b71Sopenharmony_ci class tmp{ 56e41f4b71Sopenharmony_ci x:number = 0 57e41f4b71Sopenharmony_ci y:number = 0 58e41f4b71Sopenharmony_ci width:number = 75 59e41f4b71Sopenharmony_ci height:number = 75 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci let viep:tmp = new tmp() 62e41f4b71Sopenharmony_ci class tmp1{ 63e41f4b71Sopenharmony_ci x:number = 0 64e41f4b71Sopenharmony_ci y:number = 0 65e41f4b71Sopenharmony_ci width:number = 300 66e41f4b71Sopenharmony_ci height:number = 300 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci let viep1:tmp1 = new tmp1() 69e41f4b71Sopenharmony_ci // Draw a circle whose width and height are both 75. 70e41f4b71Sopenharmony_ci Text ('Original Size Circle') 71e41f4b71Sopenharmony_ci Circle({width: 75, height: 75}).fill('#E87361') 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci Row({space:10}) { 74e41f4b71Sopenharmony_ci Column() { 75e41f4b71Sopenharmony_ci // Create a shape component whose width and height are both 150, the background color is yellow, and a viewport whose width and height are both 75. Fill the viewport with a blue rectangle and draw a circle with a diameter of 75 in the viewport. 76e41f4b71Sopenharmony_ci // The drawing is complete. The viewport is zoomed in twice based on the width and height of the component. 77e41f4b71Sopenharmony_ci Text ('Enlarged Circle') 78e41f4b71Sopenharmony_ci Shape() { 79e41f4b71Sopenharmony_ci Rect().width('100%').height('100%').fill('#0097D4') 80e41f4b71Sopenharmony_ci Circle({width: 75, height: 75}).fill('#E87361') 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci .viewPort(viep) 83e41f4b71Sopenharmony_ci .width(150) 84e41f4b71Sopenharmony_ci .height(150) 85e41f4b71Sopenharmony_ci .backgroundColor('#F5DC62') 86e41f4b71Sopenharmony_ci } 87e41f4b71Sopenharmony_ci Column() { 88e41f4b71Sopenharmony_ci // Create a shape component whose width and height are both 150, the background color is yellow, and a viewport whose width and height are both 300. Fill the viewport with a green rectangle and draw a circle with a diameter of 75 in the viewport. 89e41f4b71Sopenharmony_ci // After the drawing is complete, the viewport is zoomed out by twice based on the width and height of the component. 90e41f4b71Sopenharmony_ci Text ('Shrunk Circle') 91e41f4b71Sopenharmony_ci Shape() { 92e41f4b71Sopenharmony_ci Rect().width('100%').height('100%').fill('#BDDB69') 93e41f4b71Sopenharmony_ci Circle({width: 75, height: 75}).fill('#E87361') 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci .viewPort(viep1) 96e41f4b71Sopenharmony_ci .width(150) 97e41f4b71Sopenharmony_ci .height(150) 98e41f4b71Sopenharmony_ci .backgroundColor('#F5DC62') 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci ``` 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci  104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci- Create a shape component whose width and height are both 300, with a yellow background and a viewport whose width and height are both 300. Fill the viewport with a blue rectangle and draw a circle with a radius of 75 in the viewport. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci ```ts 108e41f4b71Sopenharmony_ci class tmp{ 109e41f4b71Sopenharmony_ci x:number = 0 110e41f4b71Sopenharmony_ci y:number = 0 111e41f4b71Sopenharmony_ci width:number = 300 112e41f4b71Sopenharmony_ci height:number = 300 113e41f4b71Sopenharmony_ci } 114e41f4b71Sopenharmony_ci let viep:tmp = new tmp() 115e41f4b71Sopenharmony_ci class tmp1{ 116e41f4b71Sopenharmony_ci x:number = -150 117e41f4b71Sopenharmony_ci y:number = -150 118e41f4b71Sopenharmony_ci width:number = 300 119e41f4b71Sopenharmony_ci height:number = 300 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci let viep1:tmp1 = new tmp1() 122e41f4b71Sopenharmony_ci Shape() { 123e41f4b71Sopenharmony_ci Rect().width("100%").height("100%").fill("#0097D4") 124e41f4b71Sopenharmony_ci Circle({ width: 150, height: 150 }).fill("#E87361") 125e41f4b71Sopenharmony_ci } 126e41f4b71Sopenharmony_ci .viewPort(viep) 127e41f4b71Sopenharmony_ci .width(300) 128e41f4b71Sopenharmony_ci .height(300) 129e41f4b71Sopenharmony_ci .backgroundColor("#F5DC62") 130e41f4b71Sopenharmony_ci ``` 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci  .jpg) 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci- Create a shape component whose width and height are both 300, with a yellow background and a viewport whose width and height are both 300. Fill the viewport with a blue rectangle, draw a circle with a radius of 75 in the viewport, and move the viewport 150 to the right and below respectively. 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci ```ts 137e41f4b71Sopenharmony_ci Shape() { 138e41f4b71Sopenharmony_ci Rect().width("100%").height("100%").fill("#0097D4") 139e41f4b71Sopenharmony_ci Circle({ width: 150, height: 150 }).fill("#E87361") 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci .viewPort(viep1) 142e41f4b71Sopenharmony_ci .width(300) 143e41f4b71Sopenharmony_ci .height(300) 144e41f4b71Sopenharmony_ci .backgroundColor("#F5DC62") 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci ``` 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci  .jpg) 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci## Setting Styles 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciThe drawing component allows you to change the component style through various attributes. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci- You can use **fill** to set the color of the filling area of the component. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci ```ts 158e41f4b71Sopenharmony_ci Path() 159e41f4b71Sopenharmony_ci .width(100) 160e41f4b71Sopenharmony_ci .height(100) 161e41f4b71Sopenharmony_ci .commands('M150 0 L300 300 L0 300 Z') 162e41f4b71Sopenharmony_ci .fill("#E87361") 163e41f4b71Sopenharmony_ci ``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci .jpg) 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci- You can use **stroke** to set the stroke color of a component. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci ```ts 170e41f4b71Sopenharmony_ci Path() 171e41f4b71Sopenharmony_ci .width(100) 172e41f4b71Sopenharmony_ci .height(100) 173e41f4b71Sopenharmony_ci .fillOpacity(0) 174e41f4b71Sopenharmony_ci .commands('M150 0 L300 300 L0 300 Z') 175e41f4b71Sopenharmony_ci .stroke(Color.Red) 176e41f4b71Sopenharmony_ci ``` 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci  179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci- You can use **strokeOpacity** to set the stroke opacity. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci ```ts 183e41f4b71Sopenharmony_ci Path() 184e41f4b71Sopenharmony_ci .width(100) 185e41f4b71Sopenharmony_ci .height(100) 186e41f4b71Sopenharmony_ci .fillOpacity(0) 187e41f4b71Sopenharmony_ci .commands('M150 0 L300 300 L0 300 Z') 188e41f4b71Sopenharmony_ci .stroke(Color.Red) 189e41f4b71Sopenharmony_ci .strokeWidth(10) 190e41f4b71Sopenharmony_ci .strokeOpacity(0.2) 191e41f4b71Sopenharmony_ci ``` 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci  194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci- You can use **strokeLineJoin** to set the join style of the stroke. Options include **Bevel**, **Miter**, and **Round**. 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci ```ts 198e41f4b71Sopenharmony_ci Polyline() 199e41f4b71Sopenharmony_ci .width(100) 200e41f4b71Sopenharmony_ci .height(100) 201e41f4b71Sopenharmony_ci .fillOpacity(0) 202e41f4b71Sopenharmony_ci .stroke(Color.Red) 203e41f4b71Sopenharmony_ci .strokeWidth(8) 204e41f4b71Sopenharmony_ci .points([[20, 0], [0, 100], [100, 90]]) 205e41f4b71Sopenharmony_ci // Set the join style of the stroke to Round. 206e41f4b71Sopenharmony_ci .strokeLineJoin(LineJoinStyle.Round) 207e41f4b71Sopenharmony_ci ``` 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci  210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci- **strokeMiterLimit** places a limit on the ratio of the miter length to the value of **strokeWidth** used to draw a miter join. 212e41f4b71Sopenharmony_ci The miter length indicates the distance from the outer tip to the inner corner of the miter. This attribute must be set to a value greater than or equal to 1 and takes effect when **strokeLineJoin** is set to **LineJoinStyle.Miter**. 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci ```ts 215e41f4b71Sopenharmony_ci Polyline() 216e41f4b71Sopenharmony_ci .width(100) 217e41f4b71Sopenharmony_ci .height(100) 218e41f4b71Sopenharmony_ci .fillOpacity(0) 219e41f4b71Sopenharmony_ci .stroke(Color.Red) 220e41f4b71Sopenharmony_ci .strokeWidth(10) 221e41f4b71Sopenharmony_ci .points([[20, 0], [20, 100], [100, 100]]) 222e41f4b71Sopenharmony_ci // Set the join style of the stroke to Miter. 223e41f4b71Sopenharmony_ci .strokeLineJoin(LineJoinStyle.Miter) 224e41f4b71Sopenharmony_ci // Set the limit on the ratio of the miter length to the value of strokeWidth used to draw a miter join. 225e41f4b71Sopenharmony_ci .strokeMiterLimit(1/Math.sin(45)) 226e41f4b71Sopenharmony_ci Polyline() 227e41f4b71Sopenharmony_ci .width(100) 228e41f4b71Sopenharmony_ci .height(100) 229e41f4b71Sopenharmony_ci .fillOpacity(0) 230e41f4b71Sopenharmony_ci .stroke(Color.Red) 231e41f4b71Sopenharmony_ci .strokeWidth(10) 232e41f4b71Sopenharmony_ci .points([[20, 0], [20, 100], [100, 100]]) 233e41f4b71Sopenharmony_ci .strokeLineJoin(LineJoinStyle.Miter) 234e41f4b71Sopenharmony_ci .strokeMiterLimit(1.42) 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci  238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci- Use the **antiAlias** attribute to set whether to enable anti-aliasing. The default value is true, indicating that anti-aliasing is enabled. 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci ```ts 242e41f4b71Sopenharmony_ci // Enable anti-aliasing. 243e41f4b71Sopenharmony_ci Circle() 244e41f4b71Sopenharmony_ci .width(150) 245e41f4b71Sopenharmony_ci .height(200) 246e41f4b71Sopenharmony_ci .fillOpacity(0) 247e41f4b71Sopenharmony_ci .strokeWidth(5) 248e41f4b71Sopenharmony_ci .stroke(Color.Black) 249e41f4b71Sopenharmony_ci ``` 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci  252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci ```ts 254e41f4b71Sopenharmony_ci // Disable anti-aliasing. 255e41f4b71Sopenharmony_ci Circle() 256e41f4b71Sopenharmony_ci .width(150) 257e41f4b71Sopenharmony_ci .height(200) 258e41f4b71Sopenharmony_ci .fillOpacity(0) 259e41f4b71Sopenharmony_ci .strokeWidth(5) 260e41f4b71Sopenharmony_ci .stroke(Color.Black) 261e41f4b71Sopenharmony_ci .antiAlias(false) 262e41f4b71Sopenharmony_ci ``` 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci  265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci## Example Scenario 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci- Draw a closed path at (-80, -5). The fill color is 0x317AF7, the stroke width is 3, the stroke color is red, and the stroke join style is miter (default value). 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci ```ts 272e41f4b71Sopenharmony_ci @Entry 273e41f4b71Sopenharmony_ci @Component 274e41f4b71Sopenharmony_ci struct ShapeExample { 275e41f4b71Sopenharmony_ci build() { 276e41f4b71Sopenharmony_ci Column({ space: 10 }) { 277e41f4b71Sopenharmony_ci Shape() { 278e41f4b71Sopenharmony_ci Path().width(200).height(60).commands('M0 0 L400 0 L400 150 Z') 279e41f4b71Sopenharmony_ci } 280e41f4b71Sopenharmony_ci .viewPort({ x: -80, y: -5, width: 500, height: 300 }) 281e41f4b71Sopenharmony_ci .fill(0x317AF7) 282e41f4b71Sopenharmony_ci .stroke(Color.Red) 283e41f4b71Sopenharmony_ci .strokeWidth(3) 284e41f4b71Sopenharmony_ci .strokeLineJoin(LineJoinStyle.Miter) 285e41f4b71Sopenharmony_ci .strokeMiterLimit(5) 286e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 15 }) 287e41f4b71Sopenharmony_ci } 288e41f4b71Sopenharmony_ci } 289e41f4b71Sopenharmony_ci ``` 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci  292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci- Draw a circle with a diameter of 150 mm and a ring with a diameter of 150 mm and a red dotted line (use the shorter side as the diameter if the width and height are different). 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci ```ts 296e41f4b71Sopenharmony_ci @Entry 297e41f4b71Sopenharmony_ci @Component 298e41f4b71Sopenharmony_ci struct CircleExample { 299e41f4b71Sopenharmony_ci build() { 300e41f4b71Sopenharmony_ci Column({ space: 10 }) { 301e41f4b71Sopenharmony_ci // Draw a circle whose diameter is 150. 302e41f4b71Sopenharmony_ci Circle({ width: 150, height: 150 }) 303e41f4b71Sopenharmony_ci // Draw a ring with a diameter of 150 mm and a red dotted line. 304e41f4b71Sopenharmony_ci Circle() 305e41f4b71Sopenharmony_ci .width(150) 306e41f4b71Sopenharmony_ci .height(200) 307e41f4b71Sopenharmony_ci .fillOpacity(0) 308e41f4b71Sopenharmony_ci .strokeWidth(3) 309e41f4b71Sopenharmony_ci .stroke(Color.Red) 310e41f4b71Sopenharmony_ci .strokeDashArray([1, 2]) 311e41f4b71Sopenharmony_ci }.width('100%') 312e41f4b71Sopenharmony_ci } 313e41f4b71Sopenharmony_ci } 314e41f4b71Sopenharmony_ci ``` 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci  317