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