• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.arkui.componentUtils (componentUtils)
2
3The **componentUtils** module provides API for obtaining the coordinates and size of the drawing area of a component.
4
5> **NOTE**
6>
7> The APIs of this module are supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](./js-apis-arkui-UIContext.md#uicontext).
10>
11> Since API version 10, you can use the [getComponentUtils](./js-apis-arkui-UIContext.md#getcomponentutils) API in **UIContext** to obtain the **ComponentUtils** object associated with the current UI context. For this API to work correctly, call it after the notification indicating completion of component layout is received through [@ohos.arkui.inspector (layout callback)](js-apis-arkui-inspector.md).
12
13## Modules to Import
14
15```ts
16import componentUtils from '@ohos.arkui.componentUtils'
17```
18## componentUtils.getRectangleById
19
20getRectangleById(id: string): ComponentInfo
21
22Obtains a **ComponentInfo** object based on the component ID.
23
24**System capability**: SystemCapability.ArkUI.ArkUI.Full
25
26**Parameters**
27
28| Name| Type  | Mandatory| Description      |
29| ------ | ------ | ---- | ---------- |
30| id     | string | Yes  | Component ID.|
31
32**Return value**
33
34| Type  | Description      |
35| ------ | ---------- |
36| [ComponentInfo](#componentinfo) | **ComponentInfo** object, which provides the size, position, translation, scaling, rotation, and affine matrix information of the component.|
37
38**Example**
39
40```ts
41import componentUtils from '@ohos.arkui.componentUtils';
42let modePosition:componentUtils.ComponentInfo = componentUtils.getRectangleById("onClick");
43```
44
45## ComponentInfo
46
47**System capability**: SystemCapability.ArkUI.ArkUI.Full
48
49| Name          | Type            | Mandatory          | Description                        |
50| ---------------|------------     | -----------------------------| -----------------------------|
51| size           | [Size](#size) | Yes| Component size.                   |
52| localOffset    | [Offset](#offset) | Yes| Offset of the component relative to the parent component.        |
53| windowOffset   | [Offset](#offset) | Yes| Offset of the component relative to the window.          |
54| screenOffset   | [Offset](#offset) | Yes| Offset of the component relative to the screen.          |
55| translate      | [TranslateResult](#translateresult) | Yes| Translation of the component.               |
56| scale          | [ScaleResult](#scaleresult) | Yes| Scaling of the component.               |
57| rotate         | [RotateResult](#rotateresult) | Yes| Rotation of the component.               |
58| transform      | [Matrix4Result](#matrix4result) | Yes| Affine matrix of the component, which is a 4x4 matrix object created based on the input parameter. |
59
60### Size
61
62**System capability**: SystemCapability.ArkUI.ArkUI.Full
63
64| Name    | Type| Mandatory| Description                              |
65| -------- | ---- | ----------------------------------| ----------------------------------|
66| width    | number | Yes| Component width.                        |
67| height   | number | Yes| Component height.                        |
68
69### Offset
70
71**System capability**: SystemCapability.ArkUI.ArkUI.Full
72
73| Name    | Type| Mandatory| Description                              |
74| --------| ---- | -----------------------------------| -----------------------------------|
75| x       | number | Yes| X coordinate.                          |
76| y       | number | Yes| Y coordinate.                          |
77
78### TranslateResult
79
80**System capability**: SystemCapability.ArkUI.ArkUI.Full
81
82| Name    | Type| Mandatory| Description                              |
83| --------| ---- | -----------------------------------| -----------------------------------|
84| x       | number | Yes| Translation distance along the x-axis.                      |
85| y       | number | Yes| Translation distance along the y-axis.                      |
86| z       | number | Yes| Translation distance along the z-axis.                      |
87
88### ScaleResult
89
90**System capability**: SystemCapability.ArkUI.ArkUI.Full
91
92| Name    | Type| Mandatory| Description                              |
93| --------| ---- | -----------------------------------| -----------------------------------|
94| x       | number | Yes| Scale factor along the x-axis.                      |
95| y       | number | Yes| Scale factor along the y-axis.                      |
96| z       | number | Yes| Scale factor along the z-axis.                      |
97| centerX | number | Yes| X coordinate of the center point.                 |
98| centerY | number | Yes| Y coordinate of the center point.               |
99
100### RotateResult
101
102**System capability**: SystemCapability.ArkUI.ArkUI.Full
103
104| Name    | Type| Mandatory| Description                              |
105| --------| ---- | -----------------------------------| -----------------------------------|
106| x       | number | Yes| X coordinate of the rotation vector.                  |
107| y       | number | Yes| Y coordinate of the rotation vector.                  |
108| z       | number | Yes| Z coordinate of the rotation vector.                  |
109| angle   | number | Yes| Rotation angle.                         |
110| centerX | number | Yes| X coordinate of the center point.                |
111| centerY | number | Yes| Y coordinate of the center point.                |
112
113### Matrix4Result
114
115**System capability**: SystemCapability.ArkUI.ArkUI.Full
116
117| Value Range  | Description                              |
118| --------| -----------------------------------|
119| [number,number,number,number,<br>number,number,number,number,<br>number,number,number,number,<br>number,number,number,number] | A number array whose length is 16 (4 x 4). For details, see **4 x 4 matrix description**.|
120
121**4 x 4 matrix description**
122
123| Name| Type  | Mandatory| Description                                |
124| ------ | ------ | ---- | ------------------------------------ |
125| m00    | number | Yes  | Scale factor along the x-axis. Defaults to **1** for the identity matrix.        |
126| m01    | number | Yes  | The second value, which is affected by the rotation of the x, y, and z axes.    |
127| m02    | number | Yes  | The third value, which is affected by the rotation of the x, y, and z axes.    |
128| m03    | number | Yes  | Meaningless value.                        |
129| m10    | number | Yes  | The fifth value, which is affected by the rotation of the x, y, and z axes.    |
130| m11    | number | Yes  | Scale factor along the y-axis. Defaults to **1** for the identity matrix.        |
131| m12    | number | Yes  | The seventh value, which is affected by the rotation of the x, y, and z axes.    |
132| m13    | number | Yes  | Meaningless value.                        |
133| m20    | number | Yes  | The ninth value, which is affected by the rotation of the x, y, and z axes.    |
134| m21    | number | Yes  | The tenth value, which is affected by the rotation of the x, y, and z axes.   |
135| m22    | number | Yes  | Scale factor along the z-axis. Defaults to **1** for the identity matrix.        |
136| m23    | number | Yes  | Meaningless value.                        |
137| m30    | number | Yes  | Translation value of the x-axis, in px. Defaults to **0** for the unit matrix.|
138| m31    | number | Yes  | Translation value of the y-axis, in px. The default value is **0** for the identity matrix.|
139| m32    | number | Yes  | Translation value of the z-axis, in px. The default value is **0** for the identity matrix.|
140| m33    | number | Yes  | Valid in homogeneous coordinates, presenting the perspective projection effect.  |
141
142**Example**
143
144  ```ts
145import matrix4 from '@ohos.matrix4';
146import componentUtils from '@ohos.arkui.componentUtils';
147
148@Entry
149@Component
150struct Utils{
151  private getComponentRect(key:string) {
152    console.info("Mode Key: " + key);
153    let modePosition = componentUtils.getRectangleById(key);
154
155    let localOffsetWidth = modePosition.size.width;
156    let localOffsetHeight = modePosition.size.height;
157    let localOffsetX = modePosition.localOffset.x;
158    let localOffsetY = modePosition.localOffset.y;
159
160    let windowOffsetX = modePosition.windowOffset.x;
161    let windowOffsetY = modePosition.windowOffset.y;
162
163    let screenOffsetX = modePosition.screenOffset.x;
164    let screenOffsetY = modePosition.screenOffset.y;
165
166    let translateX = modePosition.translate.x;
167    let translateY = modePosition.translate.y;
168    let translateZ = modePosition.translate.z;
169
170    let scaleX = modePosition.scale.x;
171    let scaleY = modePosition.scale.y;
172    let scaleZ = modePosition.scale.z;
173    let scaleCenterX = modePosition.scale.centerX;
174    let scaleCenterY = modePosition.scale.centerY;
175
176    let rotateX = modePosition.rotate.x;
177    let rotateY = modePosition.rotate.y;
178    let rotateZ = modePosition.rotate.z;
179    let rotateCenterX = modePosition.rotate.centerX;
180    let rotateCenterY = modePosition.rotate.centerY;
181    let rotateAngle = modePosition.rotate.angle;
182
183    let Matrix4_1 = modePosition.transform[0];
184    let Matrix4_2 = modePosition.transform[1];
185    let Matrix4_3 = modePosition.transform[2];
186    let Matrix4_4 = modePosition.transform[3];
187    let Matrix4_5 = modePosition.transform[4];
188    let Matrix4_6 = modePosition.transform[5];
189    let Matrix4_7 = modePosition.transform[6];
190    let Matrix4_8 = modePosition.transform[7];
191    let Matrix4_9 = modePosition.transform[8];
192    let Matrix4_10 = modePosition.transform[9];
193    let Matrix4_11 = modePosition.transform[10];
194    let Matrix4_12 = modePosition.transform[11];
195    let Matrix4_13 = modePosition.transform[12];
196    let Matrix4_14 = modePosition.transform[13];
197    let Matrix4_15 = modePosition.transform[14];
198    let Matrix4_16 = modePosition.transform[15];
199    console.info("[getRectangleById] current component obj is: " + modePosition );
200  }
201  @State x: number = 120;
202  @State y: number = 10;
203  @State z: number = 100;
204  private matrix1 = matrix4.identity().translate({ x: this.x, y: this.y, z: this.z });
205  build() {
206    Column() {
207        Image($r("app.media.icon"))
208          .transform(this.matrix1)
209          .translate({ x: 100, y: 10, z: 50})
210          .scale({ x: 2, y: 0.5, z: 1 })
211          .rotate({
212            x: 1,
213            y: 1,
214            z: 1,
215            centerX: '50%',
216            centerY: '50%',
217            angle: 300
218          })
219          .width("40%")
220          .height(100)
221          .key("image_01")
222      Button() {
223        Text('getRectangleById').fontSize(40).fontWeight(FontWeight.Bold);
224      }.margin({ top: 20 })
225      .onClick(() => {
226        this.getComponentRect("image_01");
227      }).id('onClick');
228    }
229  }
230}
231  ```
232