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

Z
zengyawen 已提交
3

E
esterzhou 已提交
4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
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 9 10

## Modules to Import

  
Z
zengyawen 已提交
11 12 13 14
```
import curves from '@ohos.curves'
```

Z
zengyawen 已提交
15 16

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

None


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

init(curve?: Curve): Object

Z
zengyawen 已提交
25 26 27

Implements initialization for the interpolation curve, which is used to create an interpolation curve object based on the input parameter.

Z
zengyawen 已提交
28 29

- Parameters
E
esterzhou 已提交
30
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
31
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
32
  | curve | Curve | No | Linear | Curve object. | 
Z
zengyawen 已提交
33 34 35 36 37 38 39 40 41

- Return values
  Curve object.


## curves.steps

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

Z
zengyawen 已提交
42 43 44

Constructs a step curve object.

Z
zengyawen 已提交
45 46

- Parameters
E
esterzhou 已提交
47
    | Name | Type | Mandatory | Default Value | Description | 
Z
zengyawen 已提交
48
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
49 50
  | count | number | Yes | - | Number of steps. Must be a positive integer. | 
  | 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 已提交
51 52 53 54 55 56 57 58 59

- Return values
  Curve object.


## curves.cubicBezier

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

Z
zengyawen 已提交
60 61 62

Constructs a third-order Bezier curve object. The curve value must be between 0 and 1.

Z
zengyawen 已提交
63 64 65 66

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
67 68 69 70
  | 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 已提交
71 72 73 74 75 76 77 78 79

- Return values
  Curve object.


## curves.spring

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

Z
zengyawen 已提交
80 81 82 83

Constructs a spring curve object.


Z
zengyawen 已提交
84 85 86
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
87
  | velocity | number | Yes | Initial velocity. | 
Z
zengyawen 已提交
88 89 90 91 92 93 94 95 96 97 98
  | mass | number | Yes | Mass. | 
  | stiffness | number | Yes | Stiffness. | 
  | damping | number | Yes | Damping. | 

- Return values
  Curve object.


## Example

  
Z
zengyawen 已提交
99 100 101 102 103 104 105 106 107
```
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.
```


Z
zengyawen 已提交
108 109 110
  Curve objects can be created only by the preceding APIs.
  | API | Description | 
| -------- | -------- |
E
esterzhou 已提交
111
| 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 已提交
112 113


Z
zengyawen 已提交
114 115 116 117 118 119 120
- Example
    
  ```
  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 已提交
121 122


Z
zengyawen 已提交
123
## Example
Z
zengyawen 已提交
124

Z
zengyawen 已提交
125
  
Z
zengyawen 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
```
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%")
  }
}
```

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