ts-interpolation-calculation.md 4.4 KB
Newer Older
Z
zengyawen 已提交
1
# Interpolation Calculation
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3

4
> **NOTE**<br>
Z
zengyawen 已提交
5
> This animation is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8
## Modules to Import

Mr-YX's avatar
Mr-YX 已提交
9
```ts
Z
zengyawen 已提交
10 11 12
import curves from '@ohos.curves'
```

Z
zengyawen 已提交
13
## Required Permissions
Z
zengyawen 已提交
14 15 16

None

Z
zengyawen 已提交
17 18 19 20
## curves.init

init(curve?: Curve): Object

Z
zengyawen 已提交
21 22
Implements initialization for the interpolation curve, which is used to create an interpolation curve object based on the input parameter.

Z
zengyawen 已提交
23
- Parameters
24

25
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
26
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
27
  | curve | Curve | No | Linear | Curve object. |
Z
zengyawen 已提交
28

E
ester.zhou 已提交
29
- Return value<br>
Z
zengyawen 已提交
30 31 32 33 34 35
  Curve object.

## curves.steps

steps(count: number, end: boolean): Object

Z
zengyawen 已提交
36 37
Constructs a step curve object.

Z
zengyawen 已提交
38
- Parameters
39

40
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
41
  | -------- | -------- | -------- | -------- | -------- |
42
  | count | number | Yes | - | Number of steps. The value must be a positive integer. |
E
ester.zhou 已提交
43
  | end | boolean | No | true | Step change at the start or end point of each interval. Defaults to **true**, indicating that the step change occurs at the end point. |
Z
zengyawen 已提交
44

E
ester.zhou 已提交
45 46
- Return value
  
Z
zengyawen 已提交
47 48 49 50 51 52
  Curve object.

## curves.cubicBezier

cubicBezier(x1: number, y1: number, x2: number, y2: number): Object

Z
zengyawen 已提交
53 54
Constructs a third-order Bezier curve object. The curve value must be between 0 and 1.

Mr-YX's avatar
Mr-YX 已提交
55
1. Parameters
56

57 58 59 60 61 62
   | Name | Type   | Mandatory | Description                                                    |
   | ---- | ------ | --------- | -------------------------------------------------------------- |
   | x1   | number | Yes       | Horizontal coordinate of the first point on the Bezier curve.  |
   | y1   | number | Yes       | Vertical coordinate of the first point on the Bezier curve.    |
   | x2   | number | Yes       | Horizontal coordinate of the second point on the Bezier curve. |
   | y2   | number | Yes       | Vertical coordinate of the second point on the Bezier curve.   |
Z
zengyawen 已提交
63

Mr-YX's avatar
Mr-YX 已提交
64
2. Return value
E
ester.zhou 已提交
65
  
Z
zengyawen 已提交
66 67 68 69 70 71
  Curve object.

## curves.spring

spring(velocity: number, mass: number, stiffness: number, damping: number): Object

Z
zengyawen 已提交
72 73
Constructs a spring curve object.

Mr-YX's avatar
Mr-YX 已提交
74
1. Parameters
75

76 77 78 79 80 81
   | Name      | Type   | Mandatory | Description       |
   | --------- | ------ | --------- | ----------------- |
   | velocity  | number | Yes       | Initial velocity. |
   | mass      | number | Yes       | Mass.             |
   | stiffness | number | Yes       | Stiffness.        |
   | damping   | number | Yes       | Damping.          |
Z
zengyawen 已提交
82

Mr-YX's avatar
Mr-YX 已提交
83
2. Return value
E
ester.zhou 已提交
84
  
Z
zengyawen 已提交
85 86 87 88
  Curve object.

## Example

Mr-YX's avatar
Mr-YX 已提交
89
```ts
Z
zengyawen 已提交
90 91 92 93 94 95 96
import Curves from '@ohos.curves'
let curve1 = Curves.init() // Create a default linear interpolation curve.
let curve2 = Curves.init(Curve.EaseIn) // Create an interpolation curve which is slow and then fast by default.
let curve3 = Curves.spring(100, 1, 228, 30) // Create a spring interpolation curve.
let curve3 = Curves.cubicBezier(0.1, 0.0, 0.1, 1.0) // Create a third-order Bezier curve.
```

E
ester.zhou 已提交
97
Curve objects can be created only by the preceding APIs.
E
ester.zhou 已提交
98
| API | Description |
Z
zengyawen 已提交
99
| -------- | -------- |
E
ester.zhou 已提交
100
| interpolate(time: number): number | Calculation function of the interpolation curve. Passing a normalized time parameter to this function returns the current interpolation.<br/>**time**: indicates the current normalized time. The value ranges from 0 to 1.<br/>The curve interpolation corresponding to the normalized time point is returned. |
Z
zengyawen 已提交
101

Z
zengyawen 已提交
102
- Example
E
ester.zhou 已提交
103
  
Mr-YX's avatar
Mr-YX 已提交
104
  ```ts
Z
zengyawen 已提交
105 106 107 108
  import Curves from '@ohos.curves'
  let curve = Curves.init(Curve.EaseIn) // Create an interpolation curve which is slow and then fast by default.
  let value: number = curve.interpolate(0.5) // Calculate the interpolation for half of the time.
  ```
Z
zengyawen 已提交
109

Z
zengyawen 已提交
110
## Example
Z
zengyawen 已提交
111

Mr-YX's avatar
Mr-YX 已提交
112
```ts
Z
zengyawen 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
import Curves from '@ohos.curves'
@Entry
@Component
struct ImageComponent {
  @State widthSize: number = 200
  @State heightSize: number = 200
  build() {
    Column() {
      Text()
        .margin({top:100})
        .width(this.widthSize)
        .height(this.heightSize)
        .backgroundColor(Color.Red)
        .onClick(()=> {
          let curve = Curves.cubicBezier(0.25, 0.1, 0.25, 1.0);
          this.widthSize = curve.interpolate(0.5) * this.widthSize;
          this.heightSize = curve.interpolate(0.5) * this.heightSize;
        })
        .animation({duration: 2000 , curve: Curves.spring(0.25, 0.1, 0.25, 1.0)})
    }.width("100%").height("100%")
  }
}
```

Mr-YX's avatar
Mr-YX 已提交
137
![en-us_image_0000001212058456](figures/en-us_image_0000001212058456.gif)