• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Line
2
3The **\<Line>** component is used to draw a straight line.
4
5>  **NOTE**
6>
7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9## Child Components
10
11Not supported
12
13
14## APIs
15
16Line(value?: {width?: string | number, height?: string | number})
17
18Since API version 9, this API is supported in ArkTS widgets.
19
20**Parameters**
21
22| Name| Type| Mandatory| Default Value| Description|
23| -------- | -------- | -------- | -------- | -------- |
24| width | string \| number | No| 0 | Width.|
25| height | string \| number | No| 0 | Height.|
26
27
28## Attributes
29
30In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
31
32| Name| Type| Default Value| Description|
33| -------- | -------- | -------- | -------- |
34| startPoint | Array&lt;Length&gt; | [0, 0] | Coordinates (relative coordinates) of the start point of the line, in vp.<br>Since API version 9, this API is supported in ArkTS widgets.|
35| endPoint   | Array&lt;Length&gt; | [0, 0] | Coordinates (relative coordinates) of the end point of the line, in vp.<br>Since API version 9, this API is supported in ArkTS widgets.|
36| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | Color of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a closed shape.|
37| fillOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Opacity of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a closed shape.|
38| stroke | [ResourceColor](ts-types.md) | - | Stroke color. If this attribute is not set, the component does not have any stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
39| strokeDashArray | Array&lt;Length&gt; | [] | Stroke dashes.<br>Since API version 9, this API is supported in ArkTS widgets.|
40| strokeDashOffset | number \| string | 0 | Offset of the start point for drawing the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
41| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | Cap style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
42| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | Join style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.|
43| strokeMiterLimit | number \| string | 4 | Limit value when the sharp angle is drawn as a miter.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a shape with a sharp angle.|
44| strokeOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Stroke opacity.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>The value range is [0.0, 1.0]. If the set value is less than 0.0, **0.0** will be used. If the set value is greater than 1.0, **1.0** will be used.|
45| strokeWidth | Length | 1 | Stroke width.<br>Since API version 9, this API is supported in ArkTS widgets.|
46| antiAlias | boolean | true | Whether anti-aliasing is enabled.<br>Since API version 9, this API is supported in ArkTS widgets.|
47
48## Example
49
50### Example 1
51
52```ts
53// xxx.ets
54@Entry
55@Component
56struct LineExample {
57  build() {
58    Column({ space: 10 }) {
59      // The coordinates of the start and end points of the line are determined relative to the coordinates of the drawing area of the <Line> component.
60      Line()
61        .startPoint([0, 0])
62        .endPoint([50, 100])
63        .stroke(Color.Black)
64        .backgroundColor('#F5F5F5')
65      Line()
66        .width(200)
67        .height(200)
68        .startPoint([50, 50])
69        .endPoint([150, 150])
70        .strokeWidth(5)
71        .stroke(Color.Orange)
72        .strokeOpacity(0.5)
73        .backgroundColor('#F5F5F5')
74      // If the coordinates of a point are beyond the width and height range of the <Line> component, the line will exceed the drawing area.
75      Line({ width: 50, height: 50 })
76        .startPoint([0, 0])
77        .endPoint([100, 100])
78        .stroke(Color.Black)
79        .strokeWidth(3)
80        .strokeDashArray([10, 3])
81        .backgroundColor('#F5F5F5')
82      // strokeDashOffset is used to define the offset when the associated strokeDashArray array is rendered.
83      Line({ width: 50, height: 50 })
84        .startPoint([0, 0])
85        .endPoint([100, 100])
86        .stroke(Color.Black)
87        .strokeWidth(3)
88        .strokeDashArray([10, 3])
89        .strokeDashOffset(5)
90        .backgroundColor('#F5F5F5')
91    }
92  }
93}
94```
95
96![en-us_image_0000001219982725](figures/en-us_image_0000001219982725.png)
97
98### Example 2
99
100```ts
101// xxx.ets
102@Entry
103@Component
104struct LineExample1 {
105  build() {
106    Row({ space: 10 }) {
107      // Set LineCapStyle to Butt.
108      Line()
109        .width(100)
110        .height(200)
111        .startPoint([50, 50])
112        .endPoint([50, 200])
113        .stroke(Color.Black)
114        .strokeWidth(20)
115        .strokeLineCap(LineCapStyle.Butt)
116        .backgroundColor('#F5F5F5').margin(10)
117      // Set LineCapStyle to Round.
118      Line()
119        .width(100)
120        .height(200)
121        .startPoint([50, 50])
122        .endPoint([50, 200])
123        .stroke(Color.Black)
124        .strokeWidth(20)
125        .strokeLineCap(LineCapStyle.Round)
126        .backgroundColor('#F5F5F5')
127      // Set LineCapStyle to Square.
128      Line()
129        .width(100)
130        .height(200)
131        .startPoint([50, 50])
132        .endPoint([50, 200])
133        .stroke(Color.Black)
134        .strokeWidth(20)
135        .strokeLineCap(LineCapStyle.Square)
136        .backgroundColor('#F5F5F5')
137    }
138  }
139}
140```
141
142![en-us_image1_0000001219982725](figures/en-us_image1_0000001219982725.png)
143
144### Example 3
145
146```ts
147// xxx.ets
148@Entry
149@Component
150struct LineExample {
151  build() {
152    Column() {
153      Line()
154        .startPoint([50, 30])
155        .endPoint([300, 30])
156        .stroke(Color.Black)
157        .strokeWidth(10)
158      // Set the interval for strokeDashArray to 50.
159      Line()
160        .startPoint([50, 20])
161        .endPoint([300, 20])
162        .stroke(Color.Black)
163        .strokeWidth(10)
164        .strokeDashArray([50])
165      // Set the interval for strokeDashArray to 50, 10.
166      Line()
167        .startPoint([50, 20])
168        .endPoint([300, 20])
169        .stroke(Color.Black)
170        .strokeWidth(10)
171        .strokeDashArray([50, 10])
172      // Set the interval for strokeDashArray to 50, 10, 20.
173      Line()
174        .startPoint([50, 20])
175        .endPoint([300, 20])
176        .stroke(Color.Black)
177        .strokeWidth(10)
178        .strokeDashArray([50, 10, 20])
179      // Set the interval for strokeDashArray to 50, 10, 20, 30.
180      Line()
181        .startPoint([50, 20])
182        .endPoint([300, 20])
183        .stroke(Color.Black)
184        .strokeWidth(10)
185        .strokeDashArray([50, 10, 20, 30])
186
187    }
188  }
189}
190```
191
192![en-us_image2_0000001219982725](figures/en-us_image2_0000001219982725.PNG)
193