• 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.<br>**NOTE**<br>An invalid value is handled as the default value.|
25| height | string \| number | No| 0 | Height.<br>**NOTE**<br>An invalid value is handled as the default value.|
26
27
28
29## Attributes
30
31In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
32
33| Name| Type| Default Value| Description|
34| -------- | -------- | -------- | -------- |
35| 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.<br>**NOTE**<br>An invalid value is handled as the default value.|
36| 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.<br>**NOTE**<br>An invalid value is handled as the default value.|
37| 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.|
38| 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.|
39| 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.<br>**NOTE**<br>If the value is invalid, no stroke will be drawn.|
40| strokeDashArray | Array&lt;Length&gt; | [] | Stroke dashes.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>An invalid value is handled as the default value.|
41| 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.<br>**NOTE**<br>An invalid value is handled as the default value.|
42| 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.|
43| 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.<br>**NOTE**<br>This attribute does not work for the **\<Line>** component, which does not have corners.|
44| 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.|
45| 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]. A value less than 0.0 evaluates to the value **0.0**. A value greater than 1.0 evaluates to the value **1.0**. Any other value evaluates to the value **1.0**.|
46| strokeWidth | Length | 1 | Stroke width.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>The value cannot be a percentage.<br>An invalid value is handled as the default value.|
47| antiAlias | boolean | true | Whether anti-aliasing is enabled.<br>Since API version 9, this API is supported in ArkTS widgets.|
48
49
50
51## Example
52
53### Example 1
54
55```ts
56// xxx.ets
57@Entry
58@Component
59struct LineExample {
60  build() {
61    Column({ space: 10 }) {
62      // 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.
63      Line()
64        .width(200)
65        .height(150)
66        .startPoint([0, 0])
67        .endPoint([50, 100])
68        .stroke(Color.Black)
69        .backgroundColor('#F5F5F5')
70      Line()
71        .width(200)
72        .height(150)
73        .startPoint([50, 50])
74        .endPoint([150, 150])
75        .strokeWidth(5)
76        .stroke(Color.Orange)
77        .strokeOpacity(0.5)
78        .backgroundColor('#F5F5F5')
79      // strokeDashOffset is used to define the offset when the associated strokeDashArray array is rendered.
80      Line()
81        .width(200)
82        .height(150)
83        .startPoint([0, 0])
84        .endPoint([100, 100])
85        .stroke(Color.Black)
86        .strokeWidth(3)
87        .strokeDashArray([10, 3])
88        .strokeDashOffset(5)
89        .backgroundColor('#F5F5F5')
90      // If the coordinates of a point are beyond the width and height range of the <Line> component, the line will exceed the drawing area.
91      Line()
92        .width(50)
93        .height(50)
94        .startPoint([0, 0])
95        .endPoint([100, 100])
96        .stroke(Color.Black)
97        .strokeWidth(3)
98        .strokeDashArray([10, 3])
99        .backgroundColor('#F5F5F5')
100    }
101  }
102}
103```
104
105![en-us_image_0000001219982725](figures/en-us_image_0000001219982725.png)
106
107### Example 2
108
109```ts
110// xxx.ets
111@Entry
112@Component
113struct LineExample1 {
114  build() {
115    Row({ space: 10 }) {
116      // Set LineCapStyle to Butt.
117      Line()
118        .width(100)
119        .height(200)
120        .startPoint([50, 50])
121        .endPoint([50, 200])
122        .stroke(Color.Black)
123        .strokeWidth(20)
124        .strokeLineCap(LineCapStyle.Butt)
125        .backgroundColor('#F5F5F5').margin(10)
126      // Set LineCapStyle to Round.
127      Line()
128        .width(100)
129        .height(200)
130        .startPoint([50, 50])
131        .endPoint([50, 200])
132        .stroke(Color.Black)
133        .strokeWidth(20)
134        .strokeLineCap(LineCapStyle.Round)
135        .backgroundColor('#F5F5F5')
136      // Set LineCapStyle to Square.
137      Line()
138        .width(100)
139        .height(200)
140        .startPoint([50, 50])
141        .endPoint([50, 200])
142        .stroke(Color.Black)
143        .strokeWidth(20)
144        .strokeLineCap(LineCapStyle.Square)
145        .backgroundColor('#F5F5F5')
146    }
147  }
148}
149```
150
151![en-us_image1_0000001219982725](figures/en-us_image1_0000001219982725.png)
152
153### Example 3
154
155```ts
156// xxx.ets
157@Entry
158@Component
159struct LineExample {
160  build() {
161    Column() {
162      Line()
163        .width(300)
164        .height(30)
165        .startPoint([50, 30])
166        .endPoint([300, 30])
167        .stroke(Color.Black)
168        .strokeWidth(10)
169      // Set the interval for strokeDashArray to 50.
170      Line()
171        .width(300)
172        .height(30)
173        .startPoint([50, 20])
174        .endPoint([300, 20])
175        .stroke(Color.Black)
176        .strokeWidth(10)
177        .strokeDashArray([50])
178      // Set the interval for strokeDashArray to 50, 10.
179      Line()
180        .width(300)
181        .height(30)
182        .startPoint([50, 20])
183        .endPoint([300, 20])
184        .stroke(Color.Black)
185        .strokeWidth(10)
186        .strokeDashArray([50, 10])
187      // Set the interval for strokeDashArray to 50, 10, 20.
188      Line()
189        .width(300)
190        .height(30)
191        .startPoint([50, 20])
192        .endPoint([300, 20])
193        .stroke(Color.Black)
194        .strokeWidth(10)
195        .strokeDashArray([50, 10, 20])
196      // Set the interval for strokeDashArray to 50, 10, 20, 30.
197      Line()
198        .width(300)
199        .height(30)
200        .startPoint([50, 20])
201        .endPoint([300, 20])
202        .stroke(Color.Black)
203        .strokeWidth(10)
204        .strokeDashArray([50, 10, 20, 30])
205
206    }
207  }
208}
209```
210
211![en-us_image2_0000001219982725](figures/en-us_image2_0000001219982725.PNG)
212