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

E
ester.zhou 已提交
3 4 5 6 7 8
Matrix transformation enables you to rotate, scale, skew, or translate an image.

> **NOTE**
>
> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

Z
zengyawen 已提交
9 10 11

## Modules to Import

Mr-YX's avatar
Mr-YX 已提交
12
```ts
Z
zengyawen 已提交
13 14 15 16
import matrix4 from '@ohos.matrix4'
```


Z
zengyawen 已提交
17 18
## matrix4.init

E
ester.zhou 已提交
19 20
init(array: Array<number>): Matrix4Transit

Z
zengyawen 已提交
21

Z
zengyawen 已提交
22 23
Matrix constructor, which is used to create a 4x4 matrix by using the input parameter. Column-major order is used.

E
ester.zhou 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
**Parameters**

| Name| Type               | Mandatory| Description                                                        |
| ------ | ------------------- | ---- | ------------------------------------------------------------ |
| array  | Array&lt;number&gt; | Yes  | A number array whose length is 16 (4 x 4). For details, see **array**.<br>Default value:<br>[1, 0, 0, 0,<br>0, 1, 0, 0,<br>0, 0, 1, 0,<br>0, 0, 0, 1] |

**Return value**

| Type          | Description                        |
| -------------- | ---------------------------- |
| Matrix4Transit | 4x4 matrix object created based on the input parameter.|

**array**

| Name | Type    | Mandatory  | Description                  |
| ---- | ------ | ---- | -------------------- |
| m00  | number | Yes   | Scaling value of the x-axis. Defaults to **1** for the identity 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.  |
| m03  | number | Yes   | Meaningless.              |
| 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 identity matrix.     |
| m12  | number | Yes   | The seventh value, which is affected by the rotation of the x, y, and z axes.  |
| m13  | number | Yes   | Meaningless.              |
| 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 identity matrix.     |
| m23  | number | Yes   | Meaningless.              |
| m30  | number | Yes   | Translation value of the x-axis, in px. Defaults to **0** for the identity matrix.|
| m31  | number | Yes   | Translation value of the y-axis, in px. Defaults to **0** for the identity matrix.|
| m32  | number | Yes   | Translation value of the z-axis, in px. Defaults to **0** for the identity matrix.|
| m33  | number | Yes   | Valid in homogeneous coordinates, presenting the perspective projection effect.   |

**Example**

```ts
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])
@Entry
@Component
struct Tests {
  build() {
    Column() {
      Image($r("app.media.zh"))
        .width("40%")
        .height(100)
        .transform(matrix)
    }
  }
}
```

Z
zengyawen 已提交
80 81 82

## matrix4.identity

E
ester.zhou 已提交
83
identity(): Matrix4Transit
Z
zengyawen 已提交
84

Z
zengyawen 已提交
85

E
ester.zhou 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
Matrix initialization function. Can return an identity matrix object.

**Return value**

| Type          | Description          |
| -------------- | -------------- |
| Matrix4Transit | Identity matrix object.|

**Example**

```ts
// The effect of matrix 1 is the same as that of matrix 2.
import matrix4 from '@ohos.matrix4'
let matrix1 = 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()
@Entry
@Component
struct Tests {
  build() {
    Column() {
      Image($r("app.media.zh"))
        .width("40%")
        .height(100)
        .transform(matrix1)
      Image($r("app.media.zh"))
        .width("40%")
        .height(100)
        .margin({ top: 150 })
        .transform(matrix2)
    }
  }
}
```
Z
zengyawen 已提交
122 123


Z
zengyawen 已提交
124
## matrix4.copy
Z
zengyawen 已提交
125

E
ester.zhou 已提交
126 127
copy(): Matrix4Transit

Z
zengyawen 已提交
128 129 130

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

E
ester.zhou 已提交
131
**Return value**
Z
zengyawen 已提交
132

E
ester.zhou 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| Type          | Description                |
| -------------- | -------------------- |
| Matrix4Transit | Copy object of the current matrix.|

**Example**

```ts
// xxx.ets
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 已提交
159
    }
Z
zengyawen 已提交
160
  }
E
ester.zhou 已提交
161 162 163 164
}
```

![en-us_image_0000001256858419](figures/en-us_image_0000001256858419.png)
Z
zengyawen 已提交
165 166


Z
zengyawen 已提交
167
## Matrix4
Z
zengyawen 已提交
168

E
ester.zhou 已提交
169

Z
zengyawen 已提交
170 171
### combine

E
ester.zhou 已提交
172 173
combine(matrix: Matrix4): Matrix4Transit

Z
zengyawen 已提交
174

Z
zengyawen 已提交
175 176
Matrix overlay function, which is used to overlay the effects of two matrices to generate a new matrix object.

E
ester.zhou 已提交
177
**Parameters**
Z
zengyawen 已提交
178

E
ester.zhou 已提交
179 180 181
| Name| Type   | Mandatory| Description              |
| ------ | ------- | ---- | ------------------ |
| matrix | Matrix4 | Yes  | Matrix object to be overlaid.|
Z
zengyawen 已提交
182

E
ester.zhou 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
**Return value**

| Type          | Description              |
| -------------- | ------------------ |
| Matrix4Transit | Object after matrix overlay.|

**Example**

```ts
// xxx.ets
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() {
      // Before matrix transformation
      Image($r("app.media.icon"))
        .width("40%")
        .height(100)
        .margin({top:50})
      // Translate the x-axis by 200px, and then scale down the x-axis twice.
      Image($r("app.media.icon"))
        .transform(this.matrix1.combine(this.matrix2))
        .width("40%")
      .height(100)
        .margin({top:50})
Z
zengyawen 已提交
213
    }
Z
zengyawen 已提交
214
  }
E
ester.zhou 已提交
215 216 217 218
}
```

![en-us_image_0000001212378428](figures/en-us_image_0000001212378428.png)
Z
zengyawen 已提交
219 220


Z
zengyawen 已提交
221 222
### invert

E
ester.zhou 已提交
223 224
invert(): Matrix4Transit

Z
zengyawen 已提交
225 226 227

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

E
ester.zhou 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
**Return value**

| Type          | Description                  |
| -------------- | ---------------------- |
| Matrix4Transit | Inverse matrix object of the current matrix.|

**Example**

```ts
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()
@Entry
@Component
struct Tests {
  build() {
    Column() {
      Image($r("app.media.zh"))
        .width(200)
        .height(100)
        .transform(matrix1)
        .margin({ top: 100 })
      Image($r("app.media.zh"))
        .width(200)
        .height(100)
        .margin({ top: 150 })
        .transform(matrix2)
    }
  }
}
```
Z
zengyawen 已提交
260 261


Z
zengyawen 已提交
262
### translate
Z
zengyawen 已提交
263

E
ester.zhou 已提交
264 265
translate({x?: number, y?: number, z?: number}): Matrix4Transit

Z
zengyawen 已提交
266 267 268

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

E
ester.zhou 已提交
269
**Parameter**
Z
zengyawen 已提交
270

E
ester.zhou 已提交
271 272 273 274 275
| Name| Type  | Mandatory| Description                                 |
| ------ | ------ | ---- | ------------------------------------- |
| x      | number | No  | Translation distance of the x-axis, in px.<br>Default value: **0**|
| y      | number | No  | Translation distance of the y-axis, in px.<br>Default value: **0**|
| z      | number | No  | Translation distance of the z-axis, in px.<br>Default value: **0**|
Z
zengyawen 已提交
276

E
ester.zhou 已提交
277
**Return value**
Z
zengyawen 已提交
278

E
ester.zhou 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| Type          | Description                        |
| -------------- | ---------------------------- |
| Matrix4Transit | Matrix object after the translation effect is added.|

**Example**

```ts
// xxx.ets
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 已提交
298
    }
Z
zengyawen 已提交
299
  }
E
ester.zhou 已提交
300 301 302 303
}
```

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

Z
zengyawen 已提交
305

Z
zengyawen 已提交
306
### scale
Z
zengyawen 已提交
307

E
ester.zhou 已提交
308 309
scale({x?: number, y?: number, z?: number, centerX?: number, centerY?: number}): Matrix4Transit

Z
zengyawen 已提交
310 311 312

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

E
ester.zhou 已提交
313
**Parameters**
Z
zengyawen 已提交
314

E
ester.zhou 已提交
315 316 317 318 319 320 321
| Name | Type  | Mandatory| Description                             |
| ------- | ------ | ---- | --------------------------------- |
| x       | number | No  | Scaling multiple of the x-axis.<br>Default value: **1**    |
| y       | number | No  | Scaling multiple of the y-axis.<br>Default value: **1**    |
| z       | number | No  | Scaling multiple of the z-axis.<br>Default value: **1**    |
| centerX | number | No  | X coordinate of the center point.<br>Default value: **0**|
| centerY | number | No  | Y coordinate of the center point.<br>Default value: **0**|
Z
zengyawen 已提交
322

E
ester.zhou 已提交
323
**Return value**
Z
zengyawen 已提交
324

E
ester.zhou 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
| Type          | Description                        |
| -------------- | ---------------------------- |
| Matrix4Transit | Matrix object after the scaling effect is added.|

**Example**

```ts
// xxx.ets
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 已提交
344
    }
Z
zengyawen 已提交
345
  }
E
ester.zhou 已提交
346 347 348 349
}
```

![zh-cn_image_0000001219864131](figures/zh-cn_image_0000001219864131.png)
Z
zengyawen 已提交
350

Z
zengyawen 已提交
351

Z
zengyawen 已提交
352
### rotate
Z
zengyawen 已提交
353

E
ester.zhou 已提交
354 355
rotate({x?: number, y?: number, z?: number, angle?: number, centerX?: Length, centerY?: Length}): Matrix4Transit

Z
zengyawen 已提交
356 357 358

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

E
ester.zhou 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
**Parameters**

| Name | Type  | Mandatory| Description                             |
| ------- | ------ | ---- | --------------------------------- |
| x       | number | No  | X coordinate of the rotation axis vector.<br>Default value: **1**  |
| y       | number | No  | Y coordinate of the rotation axis vector.<br>Default value: **1**  |
| z       | number | No  | Z coordinate of the rotation axis vector.<br>Default value: **1**  |
| angle   | number | No  | Rotation angle.<br>Default value: **0**         |
| centerX | number | No  | X coordinate of the center point.<br>Default value: **0**|
| centerY | number | No  | Y coordinate of the center point.<br>Default value: **0**|

**Return value**

| Type          | Description                        |
| -------------- | ---------------------------- |
| Matrix4Transit | Matrix object after the rotation effect is added.|

**Example**

```ts
// xxx.ets
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 已提交
392
  }
E
ester.zhou 已提交
393 394 395 396
}
```

![en-us_image_0000001211898504](figures/en-us_image_0000001211898504.png)
Z
zengyawen 已提交
397 398


Z
zengyawen 已提交
399
### transformPoint
Z
zengyawen 已提交
400

Z
zengyawen 已提交
401
transformPoint(point: Point): Point
Z
zengyawen 已提交
402

E
ester.zhou 已提交
403

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

E
ester.zhou 已提交
406
**Parameters**
Z
zengyawen 已提交
407

E
ester.zhou 已提交
408 409 410
| Name| Type | Mandatory| Description              |
| ------ | ----- | ---- | ------------------ |
| point  | Point | Yes  | Point to be transformed.|
Z
zengyawen 已提交
411

E
ester.zhou 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
**Return value**

| Type   | Description              |
| ----- | ---------------- |
| Point | Point object after matrix transformation|

**Example**

```ts
// xxx.ets
import matrix4 from '@ohos.matrix4'
import prompt from '@system.prompt'

@Entry
@Component
struct Test {
  private matrix1 = matrix4.identity().transformPoint([100, 10])
Z
zengyawen 已提交
429
  
E
ester.zhou 已提交
430 431 432 433 434 435 436
  build() {
    Column() {
     Button("get Point")
      .onClick(() => {
       prompt.showToast({message:JSON.stringify(this.matrix1),duration:2000})
      }).backgroundColor(0x2788D9)
    }.width("100%").padding(50)
Z
zengyawen 已提交
437
  }
E
ester.zhou 已提交
438 439
}
```
Z
zengyawen 已提交
440

E
ester.zhou 已提交
441
![en-us_image_0000001212218464](figures/en-us_image_0000001212218464.gif)