ts-matrix-transformation.md 10.3 KB
Newer Older
Z
zengyawen 已提交
1
# Matrix Transformation
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
Z
zengyawen 已提交
5 6 7 8 9
> This animation is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.


## Modules to Import

Z
zengyawen 已提交
10 11 12 13 14

```
import matrix4 from '@ohos.matrix4'
```

Z
zengyawen 已提交
15 16

## Required Permissions
Z
zengyawen 已提交
17 18 19 20

None


Z
zengyawen 已提交
21 22 23 24
## matrix4.init

init(array: Array<number>): Object

Z
zengyawen 已提交
25 26 27

Matrix constructor, which is used to create a 4x4 matrix by using the input parameter. Column-major order is used.

Z
zengyawen 已提交
28 29

- Parameters
E
esterzhou 已提交
30
    | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
31
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
32
  | array | Array&lt;number&gt; | Yes | [1, 0, 0, 0,<br/>0, 1, 0, 0,<br/>0, 0, 1, 0,<br/>0, 0, 0, 1] | A number array whose length is 16 (4 x 4). For details, see the parameter description. |
Z
zengyawen 已提交
33 34 35 36

- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
37
  | Object | 4x4 matrix object created based on the input parameter. | 
Z
zengyawen 已提交
38 39 40 41

- Parameter description
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
42 43 44
  | m00 | number | Yes | Scaling value of the x-axis. Defaults to **1** for the unit matrix. | 
  | m01 | number | Yes | The second value, which is affected by the rotation of the x, y, and z axes. | 
  | m02 | number | Yes | The third value, which is affected by the rotation of the x, y, and z axes. | 
Z
zengyawen 已提交
45
  | m03 | number | Yes | Meaningless. | 
E
esterzhou 已提交
46 47 48
  | m10 | number | Yes | The fifth value, which is affected by the rotation of the x, y, and z axes. | 
  | m11 | number | Yes | Scaling value of the y-axis. Defaults to **1** for the unit matrix. | 
  | m12 | number | Yes | The seventh value, which is affected by the rotation of the x, y, and z axes. | 
Z
zengyawen 已提交
49
  | m13 | number | Yes | Meaningless. | 
E
esterzhou 已提交
50 51 52
  | m20 | number | Yes | The ninth value, which is affected by the rotation of the x, y, and z axes. | 
  | m21 | number | Yes | The tenth value, which is affected by the rotation of the x, y, and z axes. | 
  | m22 | number | Yes | Scaling value of the z-axis. Defaults to **1** for the unit matrix. | 
Z
zengyawen 已提交
53
  | m23 | number | Yes | Meaningless. | 
E
esterzhou 已提交
54 55 56 57
  | m30 | number | Yes | Translation value of the x-axis, in px. Defaults to **0** for the unit matrix. | 
  | m31 | number | Yes | Translation value of the y-axis, in px. Defaults to **0** for the unit matrix. | 
  | m32 | number | Yes | Translation value of the z-axis, in px. Defaults to **0** for the unit matrix. | 
  | m33 | number | Yes | Valid in homogeneous coordinates, presenting the perspective projection effect. | 
Z
zengyawen 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  // Create a 4x4 matrix.
  let matrix = Matrix4.init([1.0, 0.0, 0.0, 0.0,
                            0.0, 1.0, 0.0, 0.0,
                            0.0, 0.0, 1.0, 0.0,
                            0.0, 0.0, 0.0, 1.0])
  ```


## matrix4.identity

identity(): Object

Z
zengyawen 已提交
75 76 77 78

Matrix initialization function. Can return a unit matrix object.


Z
zengyawen 已提交
79 80 81
- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
82
  | Object | Unit matrix object. | 
Z
zengyawen 已提交
83

Z
zengyawen 已提交
84 85 86 87 88 89 90 91 92 93 94
- Example
  
  ```
  // The effect of matrix 1 is the same as that of matrix 2.
  import Matrix4 from '@ohos.matrix4'
  let matrix = Matrix4.init([1.0, 0.0, 0.0, 0.0,
                            0.0, 1.0, 0.0, 0.0,
                            0.0, 0.0, 1.0, 0.0,
                            0.0, 0.0, 0.0, 1.0])
  let matrix2 = Matrix4.identity()
  ```
Z
zengyawen 已提交
95 96


Z
zengyawen 已提交
97
## matrix4.copy
Z
zengyawen 已提交
98

Z
zengyawen 已提交
99
copy(): Object
Z
zengyawen 已提交
100 101 102 103


Matrix copy function, which is used to copy the current matrix object.

Z
zengyawen 已提交
104 105 106 107

- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
108
  | Object | Copy object of the current matrix. | 
Z
zengyawen 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().translate({x:100})
    private matrix2 = this.matrix1.copy().scale({x:2})
    build() {
      Column() {
        Image($r("app.media.bg1"))
          .width("40%")
          .height(100)
          .transform(this.matrix1)
        Image($r("app.media.bg2"))
          .width("40%")
          .height(100)
          .margin({top:50})
          .transform(this.matrix2)
Z
zengyawen 已提交
130 131
      }
    }
Z
zengyawen 已提交
132 133
  }
  ```
Z
zengyawen 已提交
134

Z
zengyawen 已提交
135
  ![en-us_image_0000001256858419](figures/en-us_image_0000001256858419.png)
Z
zengyawen 已提交
136 137


Z
zengyawen 已提交
138
## Matrix4
Z
zengyawen 已提交
139 140


Z
zengyawen 已提交
141 142 143 144
### combine

combine(matrix: Matrix4): Object

Z
zengyawen 已提交
145 146 147

Matrix overlay function, which is used to overlay the effects of two matrices to generate a new matrix object.

Z
zengyawen 已提交
148 149

- Parameters
E
esterzhou 已提交
150
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
151
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
152
  | matrix | Matrix4 | Yes | - | Matrix object to be overlaid. | 
Z
zengyawen 已提交
153 154 155 156

- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
157
  | Object | Object after matrix overlay. | 
Z
zengyawen 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().translate({x:200}).copy()
    private matrix2 = Matrix4.identity().scale({x:2}).copy()
    build() {
      Column() {
       // Translate the x-axis by 100 pixels and then scale it up or down by 2x.
        Image($r("app.media.bg1")).transform(this.matrix1.combine(this.matrix2)) 
          .width("40%")
          .height(100)
          .margin({top:50})
Z
zengyawen 已提交
175 176
      }
    }
Z
zengyawen 已提交
177 178
  }
  ```
Z
zengyawen 已提交
179

Z
zengyawen 已提交
180
  ![en-us_image_0000001212378428](figures/en-us_image_0000001212378428.png)
Z
zengyawen 已提交
181 182


Z
zengyawen 已提交
183 184 185
### invert

invert(): Object
Z
zengyawen 已提交
186 187 188 189 190


Matrix inverse function. Can return an inverse matrix of the current matrix object, that is, get an opposite effect.


Z
zengyawen 已提交
191 192 193
- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
194
  | Object | Inverse matrix object of the current matrix. | 
Z
zengyawen 已提交
195

Z
zengyawen 已提交
196 197 198 199 200 201 202 203
- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  // The effect of matrix 1 (width scaled up by 2x) is opposite to that of matrix 2 (width scaled down by 2x).
  let matrix1 = Matrix4.identity().scale({x:2})
  let matrix2 = matrix1.invert()
  ```
Z
zengyawen 已提交
204 205


Z
zengyawen 已提交
206
### translate
Z
zengyawen 已提交
207

Z
zengyawen 已提交
208
translate({x?: number, y?: number, z?: number}): Object
Z
zengyawen 已提交
209 210 211 212


Matrix translation function, which is used to add the translation effect to the x, y, and z axes of the current matrix.

Z
zengyawen 已提交
213 214

- Parameters
E
esterzhou 已提交
215
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
216
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
217 218 219
  | x | number | No | 0 | Translation distance of the x-axis, in px. | 
  | y | number | No | 0 | Translation distance of the y-axis, in px. | 
  | z | number | No | 0 | Translation distance of the z-axis, in px. | 
Z
zengyawen 已提交
220 221 222 223 224


- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
225
  | Object | Matrix object after the translation effect is added. | 
Z
zengyawen 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().translate({x:100, y:200, z:30})
    build() {
      Column() {
        Image($r("app.media.bg1")).transform(this.matrix1)
          .width("40%")
          .height(100)
Z
zengyawen 已提交
240 241
      }
    }
Z
zengyawen 已提交
242 243 244 245
  }
  ```

  ![en-us_image_0000001212058494](figures/en-us_image_0000001212058494.png)
Z
zengyawen 已提交
246 247


Z
zengyawen 已提交
248
### scale
Z
zengyawen 已提交
249

Z
zengyawen 已提交
250
scale({x?: number, y?: number, z?: number, centerX?: number, centerY?: number}): Object
Z
zengyawen 已提交
251 252 253 254


Matrix scaling function, which is used to add the scaling effect to the x, y, and z axes of the current matrix.

Z
zengyawen 已提交
255 256

- Parameters
E
esterzhou 已提交
257
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
258
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
259 260 261 262 263
  | x | number | No | 1 | Scaling multiple of the x-axis. | 
  | y | number | No | 1 | Scaling multiple of the y-axis. | 
  | z | number | No | 1 | Scaling multiple of the z-axis. | 
  | centerX | number | No | 0 | X coordinate of the center point. | 
  | centerY | number | No | 0 | Y coordinate of the center point. | 
Z
zengyawen 已提交
264 265 266 267 268


- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
269
  | Object | Matrix object after the scaling effect is added. | 
Z
zengyawen 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().scale({x:2, y:3, z:4, centerX:50, centerY:50})
    build() {
      Column() { 
        Image($r("app.media.bg1")).transform(this.matrix1)
          .width("40%")
          .height(100)
Z
zengyawen 已提交
284 285
      }
    }
Z
zengyawen 已提交
286 287 288 289
  }
  ```

  ![en-us_image_0000001256978367](figures/en-us_image_0000001256978367.png)
Z
zengyawen 已提交
290 291


Z
zengyawen 已提交
292
### rotate
Z
zengyawen 已提交
293

Z
zengyawen 已提交
294
rotate({x?: number, y?: number, z?: number, angle?: number, centerX?: Length, centerY?: Length}): Object
Z
zengyawen 已提交
295 296 297 298


Matrix rotation function, which is used to add the rotation effect to the x, y, and z axes of the current matrix.

Z
zengyawen 已提交
299 300

- Parameters
E
esterzhou 已提交
301
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
302
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
303 304 305 306 307 308
  | x | number | No | 1 | X coordinate of the rotation axis vector. | 
  | y | number | No | 1 | Y coordinate of the rotation axis vector. | 
  | z | number | No | 1 | Z coordinate of the rotation axis vector. | 
  | angle | number | No | 0 | Rotation angle. | 
  | centerX | number | No | 0 | X coordinate of the center point. | 
  | centerY | number | No | 0 | Y coordinate of the center point. | 
Z
zengyawen 已提交
309 310 311 312 313


- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
314
  | Object | Matrix object after the rotation effect is added. | 
Z
zengyawen 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().rotate({x:1, y:1, z:2, angle:30})
    build() {
      Column() {
        Image($r("app.media.bg1")).transform(this.matrix1)
          .width("40%")
          .height(100)
      }.width("100%").margin({top:50})
Z
zengyawen 已提交
330
    }
Z
zengyawen 已提交
331 332
  }
  ```
Z
zengyawen 已提交
333

Z
zengyawen 已提交
334
  ![en-us_image_0000001211898504](figures/en-us_image_0000001211898504.png)
Z
zengyawen 已提交
335 336


Z
zengyawen 已提交
337
### transformPoint
Z
zengyawen 已提交
338

Z
zengyawen 已提交
339
transformPoint(point: Point): Point
Z
zengyawen 已提交
340 341


Z
zengyawen 已提交
342
Matrix point transformation function, which is used to apply the current transformation effect to a coordinate point.
Z
zengyawen 已提交
343 344


Z
zengyawen 已提交
345
- Parameters
E
esterzhou 已提交
346
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
347
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
348
  | point | Point | Yes | - | Point to be transformed. | 
Z
zengyawen 已提交
349 350 351 352 353


- Return values
    | Type | Description | 
  | -------- | -------- |
E
esterzhou 已提交
354
  | Point | Point object after matrix transformation | 
Z
zengyawen 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

- Example
  
  ```
  import Matrix4 from '@ohos.matrix4'
  import prompt from '@system.prompt'
  
  @Entry
  @Component
  struct Test {
    private matrix1 = Matrix4.identity().transformPoint([100, 10])
    build() {
      Column() {
       Button("get Point")
        .onClick(() => {
         prompt.showToast({message:JSON.stringify(this.matrix1),duration:2000})
        }).backgroundColor(0x2788D9)
      }.width("100%").padding(50)
    }
  }
  ```
Z
zengyawen 已提交
376

Z
zengyawen 已提交
377
  ![en-us_image_0000001212218464](figures/en-us_image_0000001212218464.gif)