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