• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Drawing Geometric Shapes (ArkTS)
2
3
4## When to Use
5
6Currently, the following geometric shapes can be drawn:
7
8- Point
9
10- Arc
11
12- Circle
13
14- Path
15
16- Region
17
18- Rectangle
19
20- Rounded rectangle
21
22Most geometric shapes can be drawn by using a pen or brush. Only a pen can be used to draw a point.
23
24
25## Available APIs
26
27The following table lists the common APIs for drawing geometric shapes. For details about the usage and parameters, see [drawing.Canvas](../reference/apis-arkgraphics2d/js-apis-graphics-drawing.md#canvas).
28
29| Interface| Description|
30| -------- | -------- |
31| drawPoint(x: number, y: number): void | Draws a point.|
32| drawArc(arc: common2D.Rect, startAngle: number, sweepAngle: number): void | Draws an arc.|
33| drawCircle(x: number, y: number, radius: number): void | Draws a circle.|
34| drawPath(path: Path): void | Draws a path.|
35| drawRegion(region: Region): void | Draws a region.|
36| drawRect(left: number, top: number, right: number, bottom: number): void | Draws a rectangle.|
37| drawRoundRect(roundRect: RoundRect): void | Draws a rounded rectangle.|
38
39
40## Drawing Points
41
42Points can be drawn only on the canvas based on the pen. You can draw points by calling drawPoint(). Two parameters are required for drawing a point, that is, the x-coordinate and y-coordinate of the point to be drawn.
43
44A simple example is as follows:
45
46```ts
47// Set the pen.
48let pen = new drawing.Pen();
49// Set the color.
50pen.setColor(0xFF, 0xFF, 0x00, 0x00);
51// Set the line width.
52pen.setStrokeWidth(40);
53// Set the stroke effect.
54canvas.attachPen(pen);
55// Draw five points.
56canvas.drawPoint(200, 200);
57canvas.drawPoint(400, 400);
58canvas.drawPoint(600, 600);
59canvas.drawPoint(800, 800);
60canvas.drawPoint(1000, 1000);
61// Remove the stroke effect.
62canvas.detachPen();
63```
64
65![Screenshot_20241129174520171](figures/Screenshot_20241129174520171.jpg)
66
67
68## Drawing Arcs
69
70You can use a pen or brush to draw an arc on the canvas by calling drawArc().
71
72A rectangle ([common2D.Rect](../reference/apis-arkgraphics2d/js-apis-graphics-common2D.md#rect)) is required for drawing an arc. Drawing is performed by using a side of the rectangle as a contour, and two parameters are further required to respectively indicate a start angle (startAngle) and a sweep angle (sweepAngle) of the arc.
73
74The following is an example of drawing an arc using a pen:
75```ts
76// Create a pen.
77let pen = new drawing.Pen();
78// Set the color.
79pen.setColor({ alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00 });
80// Set the line width.
81pen.setStrokeWidth(20);
82// Set the stroke effect.
83canvas.attachPen(pen);
84// Create a rectangle object.
85const rect: common2D.Rect = {left:100, top:200, right:1000, bottom:600};
86// Draw a rectangle.
87canvas.drawArc(rect, 0, 180);
88// Remove the stroke effect.
89canvas.detachPen();
90```
91
92![image_0000002194025289](figures/image_0000002194025289.png)
93
94
95## Drawing Circles
96
97You can use a pen or brush to draw a circle on the canvas by calling drawCircle().
98
99To draw a circle, you need to obtain the x-coordinate and y-coordinate of the center point of the circle and the radius of the circle.
100
101The following is an example of drawing a circle using a pen:
102```ts
103// Create a pen.
104let pen = new drawing.Pen();
105// Set the color.
106pen.setColor({ alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00 });
107// Set the line width.
108pen.setStrokeWidth(20);
109// Set the stroke effect.
110canvas.attachPen(pen);
111// Draw a circle.
112canvas.drawCircle(630, 630, 500);
113// Remove the stroke effect.
114canvas.detachPen();
115```
116
117![Screenshot_20241129172555673](figures/Screenshot_20241129172555673.jpg)
118
119
120## Drawing Paths
121
122A path may be drawn on the canvas by using a pen or a brush. The path may be specifically used to draw a straight line, an arc, a Bezier curve, or the like, or may form another complex shape in a path combination manner.
123
124The following describes the APIs and implementation for drawing a path. For details about the usage and parameters, see [Path](../reference/apis-arkgraphics2d/js-apis-graphics-drawing.md#path). The common interfaces are as follows:
125
1261. You can use new drawing.Path() to create a path object.
127
1282. You can use the moveTo() API to set the start point of a custom path.
129
1303. You can use the lineTo() API to add a line segment from the start point or the last point of the path (if the path does not contain any content, the default value is (0,0)) to the target point.
131
132The following is a simple example of drawing a five-pointed star using a pen and brush:
133
134```ts
135let height_ = 1800;
136let width_ = 1800;
137let len = height_ / 4;
138let aX = width_ / 3;
139let aY = height_ / 6;
140let dX = aX - len * Math.sin(18.0);
141let dY = aY + len * Math.cos(18.0);
142let cX = aX + len * Math.sin(18.0);
143let cY = dY;
144let bX = aX + (len / 2.0);
145let bY = aY + Math.sqrt((cX - dX) * (cX - dX) + (len / 2.0) * (len / 2.0));
146let eX = aX - (len / 2.0);
147let eY = bY;
148
149// Create a path object and use the APIs to construct a pentagram.
150let path = new drawing.Path();
151// Specify the start point of the path.
152path.moveTo(aX, aY);
153// Draw a line segment from the last point of a path to the target point.
154path.lineTo(bX, bY);
155path.lineTo(cX, cY);
156path.lineTo(dX, dY);
157path.lineTo(eX, eY);
158// Close the path. Now the path is drawn.
159path.close()
160
161// Create a pen object.
162let pen = new drawing.Pen();
163// Set the anti-aliasing function.
164pen.setAntiAlias(true);
165// Set the stroke color.
166pen.setColor(0xFF, 0xFF, 0x00, 0x00);
167// Set the line width.
168pen.setStrokeWidth(10.0);
169// Set the stroke effect.
170canvas.attachPen(pen);
171// Create a brush.
172let brush = new drawing.Brush();
173Fill color to be superimposed on the image.
174brush.setColor(0xFF, 0x00, 0xFF, 0x00);
175// Set the brush filling effect.
176canvas.attachBrush(brush);
177Draw a route.
178canvas.drawPath(path);
179// Remove the padding effect.
180canvas.detachBrush();
181// Remove the stroke effect.
182canvas.detachPen();
183```
184
185![Screenshot_20241129164326302](figures/Screenshot_20241129164326302.jpg)
186
187
188## Drawing Regions
189
190A region is not a specific shape. You can set it to a specified rectangle or path, or combine two regions. You can use a pen or brush to draw an area. For details about the APIs, see [Region](../reference/apis-arkgraphics2d/js-apis-graphics-drawing.md#region12).
191
192Currently, the rectangular area and path area can be set by calling setRect() and setPath() respectively.
193
194The following is an example of drawing a rectangular combined area using a brush:
195
196```ts
197// Create a brush.
198let brush = new drawing.Brush();
199// Set the color.
200brush.setColor(0xFF, 0xFF,  0x00, 0x00);
201// Set the brush filling effect.
202canvas.attachBrush(brush);
203// Create region1 in the upper left corner.
204let region1 = new drawing.Region();
205region1.setRect(100, 100, 600, 600);
206// Create region2 in the lower right corner.
207let region2 = new drawing.Region();
208region2.setRect(300, 300, 900, 900);
209// Combine the two regions in XOR mode.
210region1.op(region2, drawing.RegionOp.XOR);
211Draw a region.
212canvas.drawRegion(region1);
213// Remove the padding effect.
214canvas.detachBrush();
215```
216
217![Screenshot_20241206112505234](figures/Screenshot_20241206112505234.jpg)
218
219
220## Drawing Rectangles
221
222You can use a pen or brush to draw a rectangle on the canvas. Use the drawRect() interface to draw a rectangle. The interface needs to transfer four floating point numbers, which indicate the coordinates of the left, top, right, and bottom positions of the rectangle respectively. The four coordinates are connected to form a rectangle.
223
224The following is an example of drawing a rectangle using a brush:
225
226```ts
227// Create a brush.
228let brush = new drawing.Brush();
229// Set the color.
230brush.setColor(0xFF, 0xFF,  0x00, 0x00);
231// Set the brush filling effect.
232canvas.attachBrush(brush);
233// Draw a rectangle.
234canvas.drawRect(200, 200, 1000, 700);
235// Remove the padding effect.
236canvas.detachBrush();
237```
238
239![image_0000002194110921](figures/image_0000002194110921.png)
240
241
242## Drawing Rounded Rectangles
243
244You can use a pen or brush to draw a rounded rectangle on the canvas. Use the drawRoundRect() interface to draw a rounded rectangle. The API accepts one input parameter roundRect, which corresponds to a rounded rectangle object.
245
246The rounded rectangle object is constructed using the new drawing.RoundRect() interface. The constructor function takes the following three parameters:
247
248- common2D.Rect (rectangle object). A rounded rectangle is formed by cutting rounded corners on the basis of the rectangle.
249
250- Radius of the rounded corner on the Y axis.
251
252- Radius of the rounded corner on the Y axis.
253
254The following is an example of using a brush to draw a rounded rectangle:
255
256```ts
257// Create a brush.
258let brush = new drawing.Brush();
259// Set the color.
260brush.setColor(0xFF, 0xFF, 0x00, 0x00);
261// Set the brush filling effect.
262canvas.attachBrush(brush);
263// Create a rectangle object.
264let rect: common2D.Rect = { left: 200, top: 200, right: 1000, bottom: 700 };
265// Create a rounded rectangle object.
266let rrect = new drawing.RoundRect(rect, 30, 30);
267// Draw a round rectangle on the canvas.
268canvas.drawRoundRect(rrect);
269// Remove the padding effect.
270canvas.detachBrush();
271```
272
273![image_0000002158584406](figures/image_0000002158584406.png)
274