1# Shape 2 3The **Shape** component is the parent component of the drawing components. The attributes described in this topic are universal attributes supported by all the drawing components. 4 51. Drawing components use **Shape** as their parent to implement the effect similar to SVG. 6 72. The **Shape** component is used independently to draw a specific shape. 8 9> **NOTE** 10> 11> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 12 13 14## Child Components 15 16The following child components are supported: Rect](ts-drawing-components-rect.md), [Path](ts-drawing-components-path.md), [Circle](ts-drawing-components-circle.md), [Ellipse](ts-drawing-components-ellipse.md), [Polyline](ts-drawing-components-polyline.md), [Polygon](ts-drawing-components-polygon.md), [Image](ts-basic-components-image.md), [Text](ts-basic-components-text.md), [Column](ts-container-column.md), [Row](ts-container-row.md), Shape 17 18 19## APIs 20 21Shape(value?: PixelMap) 22 23Since API version 9, this API is supported in ArkTS widgets, except that **PixelMap** objects are not supported. 24 25**Atomic service API**: This API can be used in atomic services since API version 11. 26 27**System capability**: SystemCapability.ArkUI.ArkUI.Full 28 29**Parameters** 30 31| Name| Type| Mandatory| Description| 32| -------- | -------- | -------- | -------- | 33| value | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | No| Shape to draw. You can draw a shape in the specified **PixelMap** object. If no object is specified, the shape is drawn in the current drawing target.| 34 35 36## Attributes 37 38In addition to the [universal attributes](ts-component-general-attributes.md), the following attributes are supported. 39 40### viewPort 41 42viewPort(value: ViewportRect) 43 44Sets the viewport of the shape. 45 46**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 47 48**Atomic service API**: This API can be used in atomic services since API version 11. 49 50**System capability**: SystemCapability.ArkUI.ArkUI.Full 51 52**Parameters** 53 54| Name| Type| Mandatory| Description| 55| -------- | -------- | -------- | -------- | 56| value | [ViewportRect](ts-drawing-components-shape.md#viewportrect18) | Yes| Options of the viewport.| 57 58## ViewportRect<sup>18+</sup> 59Describes the options of the viewport. 60 61**Widget capability**: This API can be used in ArkTS widgets since API version 18. 62 63**Atomic service API**: This API can be used in atomic services since API version 18. 64 65**System capability**: SystemCapability.ArkUI.ArkUI.Full 66 67| Name| Type| Mandatory| Description| 68| -------- | -------- | -------- | -------- | 69| x | string \| number | No| Horizontal coordinate of the start point of the viewport.<br>Default value: **0**.<br>Default unit: vp.<br>Invalid values are treated as the default value.| 70| y | string \| number | No| Vertical coordinate of the start point of the viewport.<br>Default value: **0**.<br>Default unit: vp.<br>Invalid values are treated as the default value.| 71| width | string \| number | No| Width of the viewport. The value must be greater than or equal to 0.<br>Default value: **0**.<br>Default unit: vp.<br>Invalid values are treated as the default value.| 72| height | string \| number | No| Height of the viewport. The value must be greater than or equal to 0.<br>Default value: **0**.<br>Default unit: vp.<br>Invalid values are treated as the default value.| 73 74### fill 75 76fill(value: ResourceColor) 77 78Sets the color of the fill area. An invalid value is handled as the default value. If this attribute and the universal attribute **foregroundColor** are both set, whichever is set later takes effect. 79 80**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 81 82**Atomic service API**: This API can be used in atomic services since API version 11. 83 84**System capability**: SystemCapability.ArkUI.ArkUI.Full 85 86**Parameters** 87 88| Name| Type | Mandatory| Description | 89| ------ | ------------------------------------------ | ---- | -------------------------------------- | 90| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Color of the fill area.<br>Default value: **Color.Black**| 91 92### fillOpacity 93 94fillOpacity(value: number | string | Resource) 95 96Sets the opacity of the fill area. The value range is [0.0, 1.0]. A value less than 0.0 is treated as **0.0**. A value greater than 1.0 is treated as **1.0**. Any other value is treated as **1.0**. 97 98**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 99 100**Atomic service API**: This API can be used in atomic services since API version 11. 101 102**System capability**: SystemCapability.ArkUI.ArkUI.Full 103 104**Parameters** 105 106| Name| Type | Mandatory| Description | 107| ------ | ------------------------------------------------------------ | ---- | ------------------------------ | 108| value | number \| string \| [Resource](ts-types.md#resource) | Yes | Opacity of the fill area.<br>Default value: **1**| 109 110### stroke 111 112stroke(value: ResourceColor) 113 114Sets the stroke color. If this attribute is not set, the component does not have any stroke. If the value is invalid, no stroke will be drawn. 115 116**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 117 118**Atomic service API**: This API can be used in atomic services since API version 11. 119 120**System capability**: SystemCapability.ArkUI.ArkUI.Full 121 122**Parameters** 123 124| Name| Type | Mandatory| Description | 125| ------ | ------------------------------------------ | ---- | ---------- | 126| value | [ResourceColor](ts-types.md#resourcecolor) | Yes | Stroke color.| 127 128### strokeDashArray 129 130strokeDashArray(value: Array<any>) 131 132Sets the stroke dashes. The value must be greater than or equal to 0. Invalid values are treated as the default value. 133 134**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 135 136**Atomic service API**: This API can be used in atomic services since API version 11. 137 138**System capability**: SystemCapability.ArkUI.ArkUI.Full 139 140**Parameters** 141 142| Name| Type | Mandatory| Description | 143| ------ | ---------------- | ---- | ------------------------- | 144| value | Array<any> | Yes | Stroke dashes.<br>Default value: **[]**<br>Default unit: vp| 145 146### strokeDashOffset 147 148strokeDashOffset(value: number | string) 149 150Sets the offset of the start point for drawing the stroke. Invalid values are treated as the default value. 151 152**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 153 154**Atomic service API**: This API can be used in atomic services since API version 11. 155 156**System capability**: SystemCapability.ArkUI.ArkUI.Full 157 158**Parameters** 159 160| Name| Type | Mandatory| Description | 161| ------ | -------------------------- | ---- | ------------------------------------ | 162| value | number \| string | Yes | Offset of the start point for drawing the stroke.<br>Default value: **0**.<br>Default unit: vp| 163 164### strokeLineCap 165 166strokeLineCap(value: LineCapStyle) 167 168Sets the cap style of the stroke. 169 170**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 171 172**Atomic service API**: This API can be used in atomic services since API version 11. 173 174**System capability**: SystemCapability.ArkUI.ArkUI.Full 175 176**Parameters** 177 178| Name| Type | Mandatory| Description | 179| ------ | ------------------------------------------------- | ---- | ------------------------------------------------ | 180| value | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | Yes | Cap style of the stroke.<br>Default value: **LineCapStyle.Butt**| 181 182### strokeLineJoin 183 184strokeLineJoin(value: LineJoinStyle) 185 186Sets the join style of the stroke. 187 188**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 189 190**Atomic service API**: This API can be used in atomic services since API version 11. 191 192**System capability**: SystemCapability.ArkUI.ArkUI.Full 193 194**Parameters** 195 196| Name| Type | Mandatory| Description | 197| ------ | --------------------------------------------------- | ---- | -------------------------------------------------- | 198| value | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | Yes | Join style of the stroke.<br>Default value: **LineJoinStyle.Miter**| 199 200### strokeMiterLimit 201 202strokeMiterLimit(value: number | string) 203 204Sets the limit on the ratio of the miter length to the value of **strokeWidth** used to draw a miter join. The miter length indicates the distance from the outer tip to the inner corner of the miter. This attribute works only when **strokeLineJoin** is set to **LineJoinStyle.Miter**. 205 206The value must be greater than or equal to 1.0. If the value is in the [0, 1) range, the value **1.0** will be used. In other cases, the default value will be used. 207 208**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 209 210**Atomic service API**: This API can be used in atomic services since API version 11. 211 212**System capability**: SystemCapability.ArkUI.ArkUI.Full 213 214**Parameters** 215 216| Name| Type | Mandatory| Description | 217| ------ | -------------------------- | ---- | ---------------------------------------------- | 218| value | number \| string | Yes | Limit on the ratio of the miter length to the value of **strokeWidth** used to draw a miter join.<br>Default value: **4**| 219 220### strokeOpacity 221 222strokeOpacity(value: number | string | Resource) 223 224Sets the stroke opacity. The value range is [0.0, 1.0]. A value less than 0.0 is treated as **0.0**. A value greater than 1.0 is treated as **1.0**. Any other value is treated as **1.0**. 225 226**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 227 228**Atomic service API**: This API can be used in atomic services since API version 11. 229 230**System capability**: SystemCapability.ArkUI.ArkUI.Full 231 232**Parameters** 233 234| Name| Type | Mandatory| Description | 235| ------ | ------------------------------------------------------------ | ---- | -------------------------- | 236| value | number \| string \| [Resource](ts-types.md#resource) | Yes | Stroke opacity.<br>Default value: **1**| 237 238### strokeWidth 239 240strokeWidth(value: number | string) 241 242Sets the stroke width. If this attribute is of the string type, percentage values are not supported and will be treated as 1 px. 243 244**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 245 246**Atomic service API**: This API can be used in atomic services since API version 11. 247 248**System capability**: SystemCapability.ArkUI.ArkUI.Full 249 250**Parameters** 251 252| Name| Type | Mandatory| Description | 253| ------ | ---------------------------- | ---- | ------------------------ | 254| value | number \| string | Yes | Stroke width. The value must be greater than or equal to 0.<br>Default value: **1**.<br>Default unit: vp.<br>Invalid values are treated as the default value.| 255 256### antiAlias 257 258antiAlias(value: boolean) 259 260Specifies whether anti-aliasing is enabled. 261 262**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 263 264**Atomic service API**: This API can be used in atomic services since API version 11. 265 266**System capability**: SystemCapability.ArkUI.ArkUI.Full 267 268**Parameters** 269 270| Name| Type | Mandatory| Description | 271| ------ | ------- | ---- | ------------------------------------- | 272| value | boolean | Yes | Whether anti-aliasing is enabled.<br>**true**: Anti-aliasing is enabled.<br>**false**: Anti-aliasing is disabled.<br>Default value: **true**| 273 274### mesh<sup>8+</sup> 275 276mesh(value: Array<number>, column: number, row: number) 277 278Sets the mesh effect. 279 280**Widget capability**: Since API version 9, this API is supported in ArkTS widgets. 281 282**Atomic service API**: This API can be used in atomic services since API version 11. 283 284**System capability**: SystemCapability.ArkUI.ArkUI.Full 285 286**Parameters** 287 288| Name| Type | Mandatory| Description | 289| ------ | ------------------- | ---- | ------------------------------------------------------------ | 290| value | Array<number> | Yes | An array of lengths (column + 1) * (row + 1) * 2, which records the position of each vertex of the distorted bitmap.| 291| column | number | Yes | Number of columns in the mesh matrix. | 292| row | number | Yes | Number of rows in the mesh matrix. | 293 294## Example 295 296This example demonstrates how to use the **Shape** component to draw rectangles, ellipses, and lines. 297 298```ts 299// xxx.ets 300@Entry 301@Component 302struct ShapeExample { 303 build() { 304 Column({ space: 10 }) { 305 Text('basic').fontSize(11).fontColor(0xCCCCCC).width(320) 306 // Draw a 300 x 50 rectangle with strokes at (-2, -2). The fill color is 0x317AF7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default). 307 // Draw a 300 x 50 ellipse with strokes at (-2, 58). The fill color is 0x317AF7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default). 308 // Draw a 300 x 10 straight line at (-2, 118). The fill color is 0x317AF7, the stroke color is black, the stroke width is 4, the stroke dash is 20, the offset is 10 to the left, the cap style is rounded, the join style is rounded, and anti-aliasing is enabled (default). 309 Shape() { 310 Rect().width(300).height(50) 311 Ellipse().width(300).height(50).offset({ x: 0, y: 60 }) 312 Path().width(300).height(10).commands('M0 0 L900 0').offset({ x: 0, y: 120 }) 313 } 314 .width(350) 315 .height(140) 316 .viewPort({ 317 x: -2, 318 y: -2, 319 width: 304, 320 height: 130 321 }) 322 .fill(0x317AF7) 323 .stroke(Color.Black) 324 .strokeWidth(4) 325 .strokeDashArray([20]) 326 .strokeDashOffset(10) 327 .strokeLineCap(LineCapStyle.Round) 328 .strokeLineJoin(LineJoinStyle.Round) 329 .antiAlias(true) 330 331 // Draw a 300 x 50 rectangle with strokes at (0, 0) and (-5, -5). The drawing start point is the midpoint of the stroke width by default. To fully display the strokes, you must set the coordinates of the start position of the viewport to negative values so that the viewport is offset by half the stroke width. 332 Shape() { 333 Rect().width(300).height(50) 334 } 335 .width(350) 336 .height(80) 337 .viewPort({ 338 x: 0, 339 y: 0, 340 width: 320, 341 height: 70 342 }) 343 .fill(0x317AF7) 344 .stroke(Color.Black) 345 .strokeWidth(10) 346 347 Shape() { 348 Rect().width(300).height(50) 349 } 350 .width(350) 351 .height(80) 352 .viewPort({ 353 x: -5, 354 y: -5, 355 width: 320, 356 height: 70 357 }) 358 .fill(0x317AF7) 359 .stroke(Color.Black) 360 .strokeWidth(10) 361 362 Text('path').fontSize(11).fontColor(0xCCCCCC).width(320) 363 // Draw a straight line at (0, -5). The fill color is 0xEE8443, the stroke width is 10, and the stroke dash is 20. 364 Shape() { 365 Path().width(300).height(10).commands('M0 0 L900 0') 366 } 367 .width(350) 368 .height(20) 369 .viewPort({ 370 x: 0, 371 y: -5, 372 width: 300, 373 height: 20 374 }) 375 .stroke(0xEE8443) 376 .strokeWidth(10) 377 .strokeDashArray([20]) 378 379 // Draw a straight line at (0, -5). The fill color is 0xEE8443, the stroke width is 10, the stroke dash is 20, and the offset is 10 to the left. 380 Shape() { 381 Path().width(300).height(10).commands('M0 0 L900 0') 382 } 383 .width(350) 384 .height(20) 385 .viewPort({ 386 x: 0, 387 y: -5, 388 width: 300, 389 height: 20 390 }) 391 .stroke(0xEE8443) 392 .strokeWidth(10) 393 .strokeDashArray([20]) 394 .strokeDashOffset(10) 395 396 // Draw a straight line at (0, -5). The fill color is 0xEE8443, the stroke width is 10, and the stroke opacity is 0.5. 397 Shape() { 398 Path().width(300).height(10).commands('M0 0 L900 0') 399 } 400 .width(350) 401 .height(20) 402 .viewPort({ 403 x: 0, 404 y: -5, 405 width: 300, 406 height: 20 407 }) 408 .stroke(0xEE8443) 409 .strokeWidth(10) 410 .strokeOpacity(0.5) 411 412 // Draw a straight line at (0, -5). The fill color is 0xEE8443, the stroke width is 10, the stroke dash is 20, and the cap style is rounded. 413 Shape() { 414 Path().width(300).height(10).commands('M0 0 L900 0') 415 } 416 .width(350) 417 .height(20) 418 .viewPort({ 419 x: 0, 420 y: -5, 421 width: 300, 422 height: 20 423 }) 424 .stroke(0xEE8443) 425 .strokeWidth(10) 426 .strokeDashArray([20]) 427 .strokeLineCap(LineCapStyle.Round) 428 429 // Draw a closed path at (-20, -5). The fill color is 0x317AF7, the stroke width is 10, the stroke color is 0xEE8443, and the join style is miter (default value). 430 Shape() { 431 Path().width(200).height(60).commands('M0 0 L400 0 L400 150 Z') 432 } 433 .width(300) 434 .height(200) 435 .viewPort({ 436 x: -20, 437 y: -5, 438 width: 310, 439 height: 90 440 }) 441 .fill(0x317AF7) 442 .stroke(0xEE8443) 443 .strokeWidth(10) 444 .strokeLineJoin(LineJoinStyle.Miter) 445 .strokeMiterLimit(5) 446 }.width('100%').margin({ top: 15 }) 447 } 448} 449``` 450 451 452 453