• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Transformation
2
3Transformation attributes allow you to rotate, translate, scale, or transform a component.
4
5> **NOTE**
6>
7> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8
9## Attributes
10
11
12| Name       | Type                                    | Description                                      |
13| --------- | ---------------------------------------- | ---------------------------------------- |
14| rotate    | {<br>x?: number,<br>y?: number,<br>z?: number,<br>angle: number \| string,<br>centerX?: number \| string,<br>centerY?: number \| string<br>} | How the component rotates in the coordinate system (as shown below) with the upper left corner of the component as the coordinate origin. (x, y, z) specifies a vector as the axis of rotation.<br>- **angle**: rotation angle. A positive angle indicates a clockwise rotation, and a negative angle indicates a counterclockwise rotation. The value can be of the string type, for example, **'90deg'**.<br>- **centerX** and **centerY** are used to set the center of rotation.<br>The axis and center of rotation are set based on the coordinate system, which remains where it is when the component is moved.<br>Default value:<br>{<br>x: 0,<br>y: 0,<br>z: 0,<br>centerX: '50%',<br>centerY: '50%'<br>}<br>![coordinates](figures/coordinates.png)<br>Since API version 9, this API is supported in ArkTS widgets.|
15| translate | {<br>x?: number \| string,<br>y?: number \| string,<br>z? : number \| string<br>} | How the component is translated in the coordinate system (as shown below) with the upper left corner of the component as the coordinate origin. Values of **x**, **y**, and **z** indicate the translation distance along the respective axis. A positive value indicates a forward movement towards the respective axis, and a negative value indicates a backward movement towards the respective axis. The translation distance can be a number or a string (for example, **'10px'** or **'10%'**).<br>Default value:<br>{<br>x: 0,<br>y: 0,<br>z: 0<br>}<br>![coordinates](figures/coordinates.png)<br>Since API version 9, this API is supported in ArkTS widgets.|
16| scale     | {<br>x?: number,<br>y?: number,<br>z?: number,<br>centerX?: number \| string,<br>centerY?: number \| string<br>} | Scale ratio along the x-, y-, and z-axis. The default value is **1**. **centerX** and **centerY** are used to set the scale center point.<br>Default value:<br>{<br>x: 1,<br>y: 1,<br>z: 1,<br>centerX:'50%',<br>centerY:'50%'<br>}<br>Since API version 9, this API is supported in ArkTS widgets.|
17| transform | [Matrix4Transit](../apis/js-apis-matrix4.md) | Transformation matrix of the component.                            |
18
19> **NOTE**
20>
21> If both the **rotate** and **scale** attributes are set for a component, the values of **centerX** and **centerY** conflict. In this case, the one that is set later in time prevails.
22
23## Example
24
25```ts
26// xxx.ets
27import matrix4 from '@ohos.matrix4'
28
29@Entry
30@Component
31struct TransformExample {
32  build() {
33    Column() {
34      Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
35      Row()
36        .rotate({
37          x: 0,
38          y: 0,
39          z: 1,
40          centerX: '50%',
41          centerY: '50%',
42          angle: 300
43        }) // The component rotates around the center point of the rotation axis (0,0,1) clockwise by 300 degrees.
44        .width(100).height(100).backgroundColor(0xAFEEEE)
45
46      Text('translate').width('90%').fontColor(0xCCCCCC).padding(10).fontSize(14)
47      Row()
48        .translate({ x: 100, y: 10 }) // The component translates by 100 along the x-axis and by 10 along the y-axis.
49        .width(100).height(100).backgroundColor(0xAFEEEE).margin({ bottom: 10 })
50
51      Text('scale').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
52      Row()
53        .scale({ x: 2, y: 0.5}) // The height and width are doubled. The z-axis has no effect in 2D mode.
54        .width(100).height(100).backgroundColor(0xAFEEEE)
55
56      Text('Matrix4').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
57      Row()
58        .width(100).height(100).backgroundColor(0xAFEEEE)
59        .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({
60          x: 0,
61          y: 0,
62          z: 1,
63          angle: 60
64        }))
65    }.width('100%').margin({ top: 5 })
66  }
67}
68```
69
70![transform](figures/transform.PNG)
71