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

Z
zengyawen 已提交
3

Z
zengyawen 已提交
4 5 6 7 8 9
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> 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 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

- Parameters
    | Name | Type | Mandatory | Default Value | Description |
  | -------- | -------- | -------- | -------- | -------- |
  | array | Array&lt;number&gt; | Yes | [1,&nbsp;0,&nbsp;0,&nbsp;0,<br/>0,&nbsp;1,&nbsp;0,&nbsp;0,<br/>0,&nbsp;0,&nbsp;1,&nbsp;0,<br/>0,&nbsp;0,&nbsp;0,&nbsp;1] | A&nbsp;number&nbsp;array&nbsp;whose&nbsp;length&nbsp;is&nbsp;16&nbsp;(4&nbsp;x&nbsp;4).&nbsp;For&nbsp;details,&nbsp;see&nbsp;the&nbsp;parameter description. |

- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | 4x4&nbsp;matrix&nbsp;object&nbsp;created&nbsp;based&nbsp;on&nbsp;the&nbsp;input&nbsp;parameter. | 

- Parameter description
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | m00 | number | Yes | Scaling&nbsp;value&nbsp;of&nbsp;the&nbsp;x-axis.&nbsp;Defaults&nbsp;to&nbsp;**1**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m01 | number | Yes | The&nbsp;second&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m02 | number | Yes | The&nbsp;third&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m03 | number | Yes | Meaningless. | 
  | m10 | number | Yes | The&nbsp;fifth&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m11 | number | Yes | Scaling&nbsp;value&nbsp;of&nbsp;the&nbsp;y-axis.&nbsp;Defaults&nbsp;to&nbsp;**1**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m12 | number | Yes | The&nbsp;seventh&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m13 | number | Yes | Meaningless. | 
  | m20 | number | Yes | The&nbsp;ninth&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m21 | number | Yes | The&nbsp;tenth&nbsp;value,&nbsp;which&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;rotation&nbsp;of&nbsp;the&nbsp;x,&nbsp;y,&nbsp;and&nbsp;z&nbsp;axes. | 
  | m22 | number | Yes | Scaling&nbsp;value&nbsp;of&nbsp;the&nbsp;z-axis.&nbsp;Defaults&nbsp;to&nbsp;**1**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m23 | number | Yes | Meaningless. | 
  | m30 | number | Yes | Translation&nbsp;value&nbsp;of&nbsp;the&nbsp;x-axis,&nbsp;in&nbsp;px.&nbsp;Defaults&nbsp;to&nbsp;**0**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m31 | number | Yes | Translation&nbsp;value&nbsp;of&nbsp;the&nbsp;y-axis,&nbsp;in&nbsp;px.&nbsp;Defaults&nbsp;to&nbsp;**0**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m32 | number | Yes | Translation&nbsp;value&nbsp;of&nbsp;the&nbsp;z-axis,&nbsp;in&nbsp;px.&nbsp;Defaults&nbsp;to&nbsp;**0**&nbsp;for&nbsp;the&nbsp;unit&nbsp;matrix. | 
  | m33 | number | Yes | Valid&nbsp;in&nbsp;homogeneous&nbsp;coordinates,&nbsp;presenting&nbsp;the&nbsp;perspective&nbsp;projection&nbsp;effect. | 

- 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 82
- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Unit&nbsp;matrix&nbsp;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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Copy&nbsp;object&nbsp;of&nbsp;the&nbsp;current&nbsp;matrix. | 

- 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

- Parameters
    | Name | Type | Mandatory | Default&nbsp;Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | matrix | Matrix4 | Yes | - | Matrix&nbsp;object&nbsp;to&nbsp;be&nbsp;overlaid. | 

- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Object&nbsp;after&nbsp;matrix&nbsp;overlay. | 

- 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 194
- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Inverse&nbsp;matrix&nbsp;object&nbsp;of&nbsp;the&nbsp;current&nbsp;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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

- Parameters
    | Name | Type | Mandatory | Default&nbsp;Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | No | 0 | Translation&nbsp;distance&nbsp;of&nbsp;the&nbsp;x-axis,&nbsp;in&nbsp;px. | 
  | y | number | No | 0 | Translation&nbsp;distance&nbsp;of&nbsp;the&nbsp;y-axis,&nbsp;in&nbsp;px. | 
  | z | number | No | 0 | Translation&nbsp;distance&nbsp;of&nbsp;the&nbsp;z-axis,&nbsp;in&nbsp;px. | 


- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Matrix&nbsp;object&nbsp;after&nbsp;the&nbsp;translation&nbsp;effect&nbsp;is&nbsp;added. | 

- 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

- Parameters
    | Name | Type | Mandatory | Default&nbsp;Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | No | 1 | Scaling&nbsp;multiple&nbsp;of&nbsp;the&nbsp;x-axis. | 
  | y | number | No | 1 | Scaling&nbsp;multiple&nbsp;of&nbsp;the&nbsp;y-axis. | 
  | z | number | No | 1 | Scaling&nbsp;multiple&nbsp;of&nbsp;the&nbsp;z-axis. | 
  | centerX | number | No | 0 | X&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;point. | 
  | centerY | number | No | 0 | Y&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;point. | 


- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Matrix&nbsp;object&nbsp;after&nbsp;the&nbsp;scaling&nbsp;effect&nbsp;is&nbsp;added. | 

- 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

- Parameters
    | Name | Type | Mandatory | Default&nbsp;Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | x | number | No | 1 | X&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;rotation&nbsp;axis&nbsp;vector. | 
  | y | number | No | 1 | Y&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;rotation&nbsp;axis&nbsp;vector. | 
  | z | number | No | 1 | Z&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;rotation&nbsp;axis&nbsp;vector. | 
  | angle | number | No | 0 | Rotation&nbsp;angle. | 
  | centerX | number | No | 0 | X&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;point. | 
  | centerY | number | No | 0 | Y&nbsp;coordinate&nbsp;of&nbsp;the&nbsp;center&nbsp;point. | 


- Return values
    | Type | Description | 
  | -------- | -------- |
  | Object | Matrix&nbsp;object&nbsp;after&nbsp;the&nbsp;rotation&nbsp;effect&nbsp;is&nbsp;added. | 

- 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
- Parameters
    | Name | Type | Mandatory | Default&nbsp;Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | point | Point | Yes | - | Point&nbsp;to&nbsp;be&nbsp;transformed. | 


- Return values
    | Type | Description | 
  | -------- | -------- |
  | Point | Point&nbsp;object&nbsp;after&nbsp;matrix&nbsp;transformation | 

- 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)