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

Z
zengyawen 已提交
3

Z
zengyawen 已提交
4 5
> ![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.
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 30 31 32 33 34 35 36 37 38 39 40 41

- Parameters
    | Name | Type | Mandatory | Default Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | curve | Curve | No | Linear | Curve object. | 

- 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 47 48 49 50 51 52 53 54 55 56 57 58 59

- Parameters
    | Name | Type | Mandatory | Default Value | Description | 
  | -------- | -------- | -------- | -------- | -------- |
  | 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. | 

- 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 67 68 69 70 71 72 73 74 75 76 77 78 79

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

- 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 87 88 89 90 91 92 93 94 95 96 97 98
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | velocity | number | Yes | Initial velocity. | 
  | 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 111
  Curve objects can be created only by the preceding APIs.
  | API | Description | 
| -------- | -------- |
| interpolate(time:&nbsp;number):&nbsp;number | Calculation&nbsp;function&nbsp;of&nbsp;the&nbsp;interpolation&nbsp;curve.&nbsp;Passing&nbsp;a&nbsp;normalized&nbsp;time&nbsp;parameter&nbsp;to&nbsp;this&nbsp;function&nbsp;returns&nbsp;the&nbsp;current&nbsp;interpolation.<br/>**time**:&nbsp;indicates&nbsp;the&nbsp;current&nbsp;normalized&nbsp;time.&nbsp;The&nbsp;value&nbsp;ranges&nbsp;from&nbsp;0&nbsp;to&nbsp;1.<br/>The&nbsp;curve&nbsp;interpolation&nbsp;corresponding&nbsp;to&nbsp;the&nbsp;normalized&nbsp;time&nbsp;point&nbsp;is&nbsp;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)