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

E
ester.zhou 已提交
3 4 5 6 7
Interpolation calculation can be implemented to set the animation interpolation curve, which is used to construct the step curve, cubic Bezier curve, and spring curve objects.

> **NOTE**
>
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


Z
zengyawen 已提交
10 11
## Modules to Import

E
ester.zhou 已提交
12 13
```js
import Curves from '@ohos.curves'
Z
zengyawen 已提交
14 15 16
```


E
ester.zhou 已提交
17
## Curves.initCurve<sup>9+</sup>
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
initCurve(curve?: Curve): ICurve
Z
zengyawen 已提交
20 21


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

E
ester.zhou 已提交
24 25 26 27 28 29 30 31 32 33 34
**Parameters**

| Name| Type                                                        | Mandatory| Default Value      | Description      |
| ------ | ------------------------------------------------------------ | ---- | ------------ | ---------- |
| curve  | [Curve](#curve-enums)| No  | Curve.Linear | Curve type.|

**Return value**

| Type                          | Description            |
| ---------------------------------- | ---------------- |
|  [ICurve](#icurve) | Interpolation object of the curve.|
35

Z
zengyawen 已提交
36

E
ester.zhou 已提交
37 38 39 40 41 42 43
**Example**

```ts
import Curves from '@ohos.curves'
Curves.initCurve(Curve.EaseIn) // Create an ease-in curve.
```

Z
zengyawen 已提交
44

E
ester.zhou 已提交
45 46 47
##  Curves.stepsCurve<sup>9+</sup>

stepsCurve(count: number, end: boolean): ICurve
Z
zengyawen 已提交
48 49


Z
zengyawen 已提交
50 51
Constructs a step curve object.

E
ester.zhou 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
**Parameters**

| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ----| ------------------------------------------------------------ |
| count  | number  | Yes  | Number of steps. Must be a positive integer.                                  |
| end    | boolean | Yes  | Whether the step change occurs at the start or end point of each interval.<br>- **true**: The step change occurs at the end point.<br>- **false**: The step change occurs at the start point.|

**Return value**

| Type                          | Description            |
| ---------------------------------- | ---------------- |
|  [ICurve](#icurve) | Curve object.|


**Example**

```ts
import Curves from '@ohos.curves'
Curves.stepsCurve(9, true) // Create a step curve.
```
72

Z
zengyawen 已提交
73

E
ester.zhou 已提交
74
## Curves.cubicBezierCurve<sup>9+</sup>
Z
zengyawen 已提交
75

E
ester.zhou 已提交
76
cubicBezierCurve(x1: number, y1: number, x2: number, y2: number): ICurve
Z
zengyawen 已提交
77 78


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

81

E
ester.zhou 已提交
82 83 84 85 86 87 88
**Parameters**
| 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 已提交
89

E
ester.zhou 已提交
90
**Return value**
Z
zengyawen 已提交
91

E
ester.zhou 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| Type                          | Description            |
| ---------------------------------- | ---------------- |
|  [ICurve](#icurve) | Curve object.|


**Example**

```ts
import Curves from '@ohos.curves'
Curves.cubicBezierCurve(0.1, 0.0, 0.1, 1.0) // Create a cubic Bezier curve.
```


##  Curves.springCurve<sup>9+</sup>

springCurve(velocity: number, mass: number, stiffness: number, damping: number): ICurve
Z
zengyawen 已提交
108 109


Z
zengyawen 已提交
110 111
Constructs a spring curve object.

112

E
ester.zhou 已提交
113 114 115 116 117 118 119
**Parameters**
| Name      | Type    | Mandatory  | Description   |
| --------- | ------ | ---- | ----- |
| velocity  | number | Yes   | Initial velocity. It is a parameter that affects the elastic dynamic effect by external factors. It aims to ensure the smooth transition from the previous motion state to the elastic dynamic effect.|
| mass      | number | Yes   | Quality. The force object of the elastic system will have inertia effects on the elastic system. The greater the mass, the greater the amplitude of the oscillation, and the slower the speed of restoring to the equilibrium position.|
| stiffness | number | Yes   | Stiffness. It is the degree to which an object deforms by resisting the force applied. In an elastic system, the greater the stiffness, the stronger the ability to resist deformation, and the faster the speed of restoring to the equilibrium position.|
| damping   | number | Yes   | Damping. It is a pure number and has no real physical meaning. It is used to describe the oscillation and attenuation of the system after being disturbed. The larger the damping, the smaller the number of oscillations of elastic motion and the smaller the oscillation amplitude.|
Z
zengyawen 已提交
120

Z
zengyawen 已提交
121

E
ester.zhou 已提交
122 123 124 125 126 127 128 129
**Return value**

| Type                          | Description            |
| ---------------------------------- | ---------------- |
|  [ICurve](#icurve)| Curve object.|


**Example**
Z
zengyawen 已提交
130

Mr-YX's avatar
Mr-YX 已提交
131
```ts
Z
zengyawen 已提交
132
import Curves from '@ohos.curves'
E
ester.zhou 已提交
133
Curves.springCurve(100, 1, 228, 30) // Create a spring curve.
Z
zengyawen 已提交
134 135 136
```


E
ester.zhou 已提交
137 138 139 140 141 142 143 144 145 146 147 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 175 176 177 178 179 180 181 182 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 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 240 241 242 243 244 245 246 247 248
## ICurve


### interpolate

interpolate(fraction: number): number


Calculation function of the interpolation curve. Passing a normalized time parameter to this function returns the current interpolation.

**Parameters**

| Name  | Type  | Mandatory| Description                                        |
| -------- | ------ | ---- | -------------------------------------------- |
| fraction | number | Yes  | Current normalized time. The value ranges from 0 to 1.|

**Return value**

| Type  | Description                                |
| ------ | ------------------------------------ |
| number | The curve interpolation corresponding to the normalized time point is returned.|

**Example**

```ts
import Curves from '@ohos.curves'
let curve = Curves.initCurve(Curve.EaseIn) // Create an ease-in curve.
let value: number = curve.interpolate(0.5) // Calculate the interpolation for half of the time.
```


## Curves.init<sup>(deprecated)</sup>

init(curve?: Curve): string


Implements initialization to create a curve object based on the input parameter. This API is deprecated since API version 9. Use [Curves.initCurve](#curvesinitcurve9) instead.

**Parameters**

| Name| Type                                                        | Mandatory| Default Value      | Description      |
| ------ | ------------------------------------------------------------ | ---- | ------------ | ---------- |
| curve  |[Curve](#curve-enums)| No  | Curve.Linear | Curve type.|


## Curves.steps<sup>(deprecated)</sup>

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


Constructs a step curve object. This API is deprecated since API version 9. Use [Curves.stepsCurve](# curvesstepscurve9) instead.

**Parameters**

| Name| Type   | Mandatory| Description                                                        |
| ------ | ------- | ----| ------------------------------------------------------------ |
| count  | number  | Yes  | Number of steps. Must be a positive integer.                                  |
| end    | boolean | Yes  | Whether the step change occurs at the start or end of each interval.<br>- **true**: The step change occurs at the end point.<br>- **false**: The step change occurs at the start point.|


## Curves.cubicBezier<sup>(deprecated)</sup>

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


Constructs a cubic Bezier curve object. The curve value must range from 0 to 1. This API is deprecated since API version 9. Use [Curves.cubicBezierCurve](#curvescubicbeziercurve9) instead.


**Parameters**
| 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.|


## Curves.spring<sup>(deprecated)</sup>

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


Constructs a spring curve object. This API is deprecated since API version 9. Use [Curves.cubicBezierCurve](#curvescubicbeziercurve9) instead.

**Parameters**

| Name      | Type    | Mandatory  | Description   |
| --------- | ------ | ---- | ----- |
| velocity  | number | Yes   | Initial velocity. It is a parameter that affects the elastic dynamic effect by external factors. It aims to ensure the smooth transition from the previous motion state to the elastic dynamic effect.|
| mass      | number | Yes   | Quality. The force object of the elastic system will have inertia effects on the elastic system. The greater the mass, the greater the amplitude of the oscillation, and the slower the speed of restoring to the equilibrium position.|
| stiffness | number | Yes   | Stiffness. It is the degree to which an object deforms by resisting the force applied. In an elastic system, the greater the stiffness, the stronger the ability to resist deformation, and the faster the speed of restoring to the equilibrium position.|
| damping   | number | Yes   | Damping. It is a pure number and has no real physical meaning. It is used to describe the oscillation and attenuation of the system after being disturbed. The larger the damping, the smaller the number of oscillations of elastic motion and the smaller the oscillation amplitude.|


## Curve

| Name                 | Description                                      |
| ------------------- | ---------------------------------------- |
| Linear              | The animation speed keeps unchanged.                       |
| Ease                | The animation starts slowly, accelerates, and then slows down towards the end. The cubic-bezier curve (0.25, 0.1, 0.25, 1.0) is used.|
| EaseIn              | The animation starts at a low speed and then picks up speed until the end. The cubic-bezier curve (0.42, 0.0, 1.0, 1.0) is used.|
| EaseOut             | The animation ends at a low speed. The cubic-bezier curve (0.0, 0.0, 0.58, 1.0) is used.|
| EaseInOut           | The animation starts and ends at a low speed. The cubic-bezier curve (0.42, 0.0, 0.58, 1.0) is used.|
| FastOutSlowIn       | The animation uses the standard cubic-bezier curve (0.4, 0.0, 0.2, 1.0).|
| LinearOutSlowIn     | The animation uses the deceleration cubic-bezier curve (0.0, 0.0, 0.2, 1.0).|
| FastOutLinearIn     | The animation uses the acceleration cubic-bezier curve (0.4, 0.0, 1.0, 1.0).|
| ExtremeDeceleration | The animation uses the extreme deceleration cubic-bezier curve (0.0, 0.0, 0.0, 1.0).|
| Sharp               | The animation uses the sharp cubic-bezier curve (0.33, 0.0, 0.67, 1.0).|
| Rhythm              | The animation uses the rhythm cubic-bezier curve (0.7, 0.0, 0.2, 1.0).|
| Smooth              | The animation uses the smooth cubic-bezier curve (0.4, 0.0, 0.4, 1.0).|
| Friction            | The animation uses the friction cubic-bezier curve (0.2, 0.0, 0.2, 1.0).|

Z
zengyawen 已提交
249

Z
zengyawen 已提交
250
## Example
Z
zengyawen 已提交
251

Mr-YX's avatar
Mr-YX 已提交
252
```ts
E
ester.zhou 已提交
253
// xxx.ets
Z
zengyawen 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
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(()=> {
E
ester.zhou 已提交
268
          let curve = Curves.cubicBezierCurve(0.25, 0.1, 0.25, 1.0);
Z
zengyawen 已提交
269 270 271
          this.widthSize = curve.interpolate(0.5) * this.widthSize;
          this.heightSize = curve.interpolate(0.5) * this.heightSize;
        })
E
ester.zhou 已提交
272
        .animation({duration: 2000 , curve: Curves.stepsCurve(9, true)})
Z
zengyawen 已提交
273 274 275 276
    }.width("100%").height("100%")
  }
}
```
E
ester.zhou 已提交
277
![en-us_image_0000001212058456](figures/en-us_image_0000001212058456.gif)