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## rotate 10 11rotate(value: RotateOptions) 12 13Rotates the component. 14 15**Widget capability**: This API can be used in ArkTS widgets since API version 9. 16 17**Atomic service API**: This API can be used in atomic services since API version 11. 18 19**System capability**: SystemCapability.ArkUI.ArkUI.Full 20 21**Parameters** 22 23| Name| Type | Mandatory| Description | 24| ------ | --------------------------------------- | ---- | ------------------------------------------------------------ | 25| value | [RotateOptions](#rotateoptions) | Yes | 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>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: When **x**, **y**, and **z** are not specified, their default values are **0**, **0**, and **1**, respectively. If any of **x**, **y**, and **z** is specified, the default value for the unspecified one is **0**.<br>{<br>centerX: '50%',<br>centerY: '50%'<br>centerZ: 0,<br>perspective: 0<br>}<br>Unit: vp<br><br>Since API version 10, **CenterZ** and **perspective** are supported in ArkTS widgets.| 26 27## translate 28 29translate(value: TranslateOptions) 30 31Translates the component. 32 33**Widget capability**: This API can be used in ArkTS widgets since API version 9. 34 35**Atomic service API**: This API can be used in atomic services since API version 11. 36 37**System capability**: SystemCapability.ArkUI.ArkUI.Full 38 39**Parameters** 40 41| Name| Type | Mandatory| Description | 42| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 43| value | [TranslateOptions](#translateoptions) | Yes | 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>Unit: vp<br><br>**NOTE**<br>When the component is translated along the z-axis, the position of the observation point remains unchanged. As such, the component appears larger when the value of **z** places it closer to the observation point and smaller when the value of **z** places it further away from the observation point.<br> | 44 45## scale 46 47scale(value: ScaleOptions) 48 49Scales the component. 50 51**Widget capability**: This API can be used in ArkTS widgets since API version 9. 52 53**Atomic service API**: This API can be used in atomic services since API version 11. 54 55**System capability**: SystemCapability.ArkUI.ArkUI.Full 56 57**Parameters** 58 59| Name| Type | Mandatory| Description | 60| ------ | ------------------------------------- | ---- | ------------------------------------------------------------ | 61| value | [ScaleOptions](#scaleoptions) | Yes | 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>} | 62 63## transform 64 65transform(value: object) 66 67Sets the transformation matrix of the component. 68 69**Atomic service API**: This API can be used in atomic services since API version 11. 70 71**System capability**: SystemCapability.ArkUI.ArkUI.Full 72 73**Parameters** 74 75| Name| Type | Mandatory| Description | 76| ------ | --------------------------------------- | ---- | ------------------------ | 77| value | object | Yes | Transformation matrix of the component. Only the [Matrix4Transit](../js-apis-matrix4.md) object type is supported.| 78 79## RotateOptions 80 81**Atomic service API**: This API can be used in atomic services since API version 11. 82 83**System capability**: SystemCapability.ArkUI.ArkUI.Full 84 85| Name | Type | Mandatory| Description | 86| ------------------------- | -------------------------- | ---- | ------------------------------------------------------------ | 87| x | number | No | X coordinate of the rotation axis vector.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 88| y | number | No | Y coordinate of the rotation axis vector.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 89| z | number | No | Z coordinate of the rotation axis vector.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 90| angle | number \| string | Yes | Angle to rotate. 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>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 91| centerX | number \| string | No | X coordinate of the transformation center point (anchor).<br>Unit: vp<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 92| centerY | number \| string | No | Y coordinate of the transformation center point (anchor).<br>Unit: vp<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 93| centerZ<sup>10+</sup> | number | No | Z-axis anchor, that is, the z-component of the 3D rotation center point.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 10.| 94| perspective<sup>10+</sup> | number | No | Distance from the user to the z=0 plane.<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>**Widget capability**: This API can be used in ArkTS widgets since API version 10.| 95 96## TranslateOptions 97 98**Widget capability**: This API can be used in ArkTS widgets since API version 9. 99 100**Atomic service API**: This API can be used in atomic services since API version 11. 101 102**System capability**: SystemCapability.ArkUI.ArkUI.Full 103 104| Name| Type | Mandatory| Description | 105| ---- | -------------------------- | ---- | --------------- | 106| x | number \| string | No | Translation distance along the x-axis.| 107| y | number \| string | No | Translation distance along the y-axis.| 108| z | number \| string | No | Translation distance along the z-axis.| 109 110## ScaleOptions 111 112**Widget capability**: This API can be used in ArkTS widgets since API version 9. 113 114**Atomic service API**: This API can be used in atomic services since API version 11. 115 116**System capability**: SystemCapability.ArkUI.ArkUI.Full 117 118| Name | Type | Mandatory| Description | 119| ------- | -------------------------- | ---- | ------------------------------------------------------------ | 120| x | number | No | Scale ratio along the x-axis. x > 1: The component is scaled up along the x-axis.<br>0 < x < 1: The component is scaled down along the x-axis.<br>x < 0: The component is scaled in the reverse direction of the x-axis.| 121| y | number | No | Scale ratio along the y-axis. y > 1: The component is scaled up along the y-axis.<br>0 < y < 1: The component is scaled down along the y-axis.<br>y < 0: The component is scaled in the reverse direction of the y-axis.| 122| z | number | No | Scale ratio along the z-axis. z > 1: The component is scaled up along the z-axis.<br>0 < z < 1: The component is scaled down along the z-axis.<br>z < 0: The component is scaled in the reverse direction of the z-axis.| 123| centerX | number \| string | No | X coordinate of the transformation center point (anchor).<br>Unit: vp| 124| centerY | number \| string | No | Y coordinate of the transformation center point (anchor).<br>Unit: vp| 125 126> **NOTE** 127> 128> If the **rotate** and **scale** attributes are both set for a component, the values of **centerX** and **centerY** conflict. In this case, the one that is set later in time prevails. 129 130## Example 131 132### Example 1: Adding Graphical Transformation Effects 133 134This example demonstrates how to apply various graphical transformations using the **rotate**, **translate**, **scale**, and **transform** APIs. 135 136```ts 137// xxx.ets 138import { matrix4 } from '@kit.ArkUI'; 139 140@Entry 141@Component 142struct TransformExample { 143 build() { 144 Column() { 145 Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14) 146 Row() 147 .rotate({ 148 x: 0, 149 y: 0, 150 z: 1, 151 centerX: '50%', 152 centerY: '50%', 153 angle: 300 154 }) // The component rotates around the center point of the rotation axis (0,0,1) clockwise by 300 degrees. 155 .width(100).height(100).backgroundColor(0xAFEEEE) 156 157 Text('translate').width('90%').fontColor(0xCCCCCC).padding(10).fontSize(14) 158 Row() 159 .translate({ x: 100, y: 10 }) // The component translates by 100 along the x-axis and by 10 along the y-axis. 160 .width(100) 161 .height(100) 162 .backgroundColor(0xAFEEEE) 163 .margin({ bottom: 10 }) 164 165 Text('scale').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14) 166 Row() 167 .scale({ x: 2, y: 0.5}) // The height and width are doubled. The z-axis has no effect in 2D mode. 168 .width(100).height(100).backgroundColor(0xAFEEEE) 169 170 Text('Matrix4').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14) 171 Row() 172 .width(100).height(100).backgroundColor(0xAFEEEE) 173 .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({ 174 x: 0, 175 y: 0, 176 z: 1, 177 angle: 60 178 })) 179 }.width('100%').margin({ top: 5 }) 180 } 181} 182``` 183 184 185 186### Example 2: Setting the Rotation Perspective 187 188This example demonstrates how to use the **perspective** API to add a perspective effect to a component. 189 190```ts 191// xxx.ets 192@Entry 193@Component 194struct Index { 195 @State prep: number = 10; 196 197 build() { 198 Row() { 199 Column() { 200 Stack() 201 .width(100) 202 .height(100) 203 .backgroundColor(Color.Red) 204 .rotate({ y: 1, angle: 45, perspective: this.prep }) 205 Button('change prep') 206 .margin({ top: 100 }) 207 .onClick(() => { 208 animateTo({ 209 duration: 2000, 210 curve: Curve.EaseIn, 211 iterations: 1, 212 playMode: PlayMode.Normal, 213 onFinish: () => { 214 console.info('play end') 215 } 216 }, () => { 217 this.prep = 500 // Change the component's perspective from 10 to 500. 218 }) 219 }) 220 } 221 .width('100%') 222 } 223 .height('100%') 224 } 225} 226``` 227 228 229 230### Example 3: Implementing Rotation Around a Center Point 231 232This example shows how to achieve the same rotation effect by setting different parameters for** rotate** and **transform**. 233 234```ts 235import { matrix4 } from '@kit.ArkUI' 236 237@Entry 238@Component 239struct MatrixExample { 240 build() { 241 Column({ space: 100 }) { 242 Text('Hello1') 243 .textAlign(TextAlign.Center) 244 .width(100) 245 .height(60) 246 .borderWidth(1) 247 248 Text('Hello2') 249 .textAlign(TextAlign.Center) 250 .width(100) 251 .height(60) 252 .borderWidth(1) 253 .rotate({ 254 // Rotate 90 degrees around the anchor (100 vp, 60 vp), where the value of centerX and centerY in rotate or scale are the component's anchors. 255 z: 1, 256 angle: 90, 257 centerX: 100, 258 centerY: 60 259 }) 260 261 Text('Hello3') 262 .textAlign(TextAlign.Center) 263 .width(100) 264 .height(60) 265 .borderWidth(1) 266 .transform(matrix4.identity() 267 .rotate({ 268 // The component's anchor (centerX, centerY) is (50%, 50%) by default, which is (50 vp, 30 vp). 269 // Set (centerX, centerY) of rotate in transform to (50 vp, 30 vp), which is an additional offset from the component's own anchor. 270 // This transformation is equivalent to rotating around (100 vp, 60 vp), achieving the same rotation effect as "Hello2." 271 z: 1, 272 angle: 90, 273 centerX: vp2px(50), 274 centerY: vp2px(30) 275 })) 276 277 Text('Hello4') 278 .textAlign(TextAlign.Center) 279 .width(100) 280 .height(60) 281 .borderWidth(1) 282 .scale({ 283 // centerX and centerY take effect only when x or y is set. 284 // Set the component anchor to (100 vp, 60 vp). 285 x: 1, 286 y: 1, 287 centerX: 100, 288 centerY: 60 289 })// centerX and centerY are not specified for rotate in transform. As such, the rotation center is not offset from the component's own anchor. 290 // The component rotates around (100 vp, 60 vp) through the anchor set by scale, achieving the same rotation effect as "Hello2." 291 .transform(matrix4.identity().rotate({ z: 1, angle: 90 })) 292 }.width('100%') 293 .height('100%') 294 } 295} 296``` 297 298 299