1# @ohos.curves (Interpolation Calculation) 2 3The **Curves** module provides APIs for interpolation calculation to create step, cubic Bezier, and spring curves. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12```ts 13import Curves from '@ohos.curves' 14``` 15 16 17## Curves.initCurve<sup>9+</sup> 18 19initCurve(curve?: Curve): ICurve 20 21 22Implements initialization for the interpolation curve, which is used to create an interpolation curve based on the input parameter. 23 24**System capability**: SystemCapability.ArkUI.ArkUI.Full 25 26**Parameters** 27 28| Name| Type | Mandatory| Default Value | Description | 29| ------ | ------------------------------------------------------------ | ---- | ------------ | ---------- | 30| curve | [Curve](../arkui-ts/ts-appendix-enums.md#curve) | No | Curve.Linear | Curve type.| 31 32**Return value** 33 34| Type | Description | 35| ---------------------------------- | ---------------- | 36| [ICurve](#icurve) | Interpolation curve.| 37 38 39**Example** 40 41```ts 42import Curves from '@ohos.curves' 43Curves.initCurve(Curve.EaseIn) // Create a default ease-in curve, where the interpolation starts slowly and then picks up speed. 44``` 45 46 47## Curves.stepsCurve<sup>9+</sup> 48 49stepsCurve(count: number, end: boolean): ICurve 50 51 52Creates a step curve. 53 54**System capability**: SystemCapability.ArkUI.ArkUI.Full 55 56**Parameters** 57 58| Name| Type | Mandatory| Description | 59| ------ | ------- | ----| ------------------------------------------------------------ | 60| count | number | Yes | Number of steps. The value must be a positive integer. | 61| end | boolean | Yes | Whether jumping occurs when the interpolation ends.<br>- **true**: Jumping occurs when the interpolation ends.<br>- **false**: Jumping occurs when the interpolation starts.| 62 63**Return value** 64 65| Type | Description | 66| ---------------------------------- | ---------------- | 67| [ICurve](#icurve) | Interpolation curve.| 68 69 70**Example** 71 72```ts 73import Curves from '@ohos.curves' 74Curves.stepsCurve(9, true) // Create a step curve. 75``` 76 77 78## Curves.cubicBezierCurve<sup>9+</sup> 79 80cubicBezierCurve(x1: number, y1: number, x2: number, y2: number): ICurve 81 82 83Creates a cubic Bezier curve. The curve values must be between 0 and 1. 84 85**System capability**: SystemCapability.ArkUI.ArkUI.Full 86 87**Parameters** 88| Name | Type | Mandatory | Description | 89| ---- | ------ | ---- | -------------- | 90| x1 | number | Yes | X coordinate of the first point on the Bezier curve.| 91| y1 | number | Yes | Y coordinate of the first point on the Bezier curve.| 92| x2 | number | Yes | X coordinate of the second point on the Bezier curve.| 93| y2 | number | Yes | Y coordinate of the second point on the Bezier curve.| 94 95**Return value** 96 97| Type | Description | 98| ---------------------------------- | ---------------- | 99| [ICurve](#icurve) | Interpolation curve.| 100 101 102**Example** 103 104```ts 105import Curves from '@ohos.curves' 106Curves.cubicBezierCurve(0.1, 0.0, 0.1, 1.0) // Create a cubic Bezier curve. 107``` 108 109 110## Curves.springCurve<sup>9+</sup> 111 112springCurve(velocity: number, mass: number, stiffness: number, damping: number): ICurve 113 114 115Creates a spring curve. 116 117**System capability**: SystemCapability.ArkUI.ArkUI.Full 118 119**Parameters** 120| Name | Type | Mandatory | Description | 121| --------- | ------ | ---- | ----- | 122| velocity | number | Yes | Initial velocity. It is applied by external factors to the elastic animation. It aims to help ensure the smooth transition from the previous motion state to the elastic animation.| 123| mass | number | Yes | Mass, which influences the inertia in the spring system. The greater the mass, the greater the amplitude of the oscillation, and the slower the speed of restoring to the equilibrium position.| 124| 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.| 125| 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.| 126 127 128**Return value** 129 130| Type | Description | 131| ---------------------------------- | ---------------- | 132| [ICurve](#icurve)| Interpolation curve.| 133 134 135**Example** 136 137```ts 138import Curves from '@ohos.curves' 139Curves.springCurve(100, 1, 228, 30) // Create a spring curve. 140``` 141 142 143## Curves.springMotion<sup>9+</sup> 144 145springMotion(response?: number, dampingFraction?: number, overlapDuration?: number): ICurve 146 147Creates a spring animation curve. If multiple spring animations are applied to the same attribute of an object, each animation replaces their predecessor and inherits the velocity. 148 149**System capability**: SystemCapability.ArkUI.ArkUI.Full 150 151**Parameters** 152| Name | Type | Mandatory | Description | 153| --------- | ------ | ---- | ----- | 154| response | number | No | Duration of one complete oscillation, in seconds.<br>Default value: **0.55**| 155| dampingFraction | number | No | Damping coefficient.<br>**0**: undamped. In this case, the spring oscillates forever.<br>> 0 and < 1: underdamped. In this case, the spring overshoots the equilibrium position.<br>**1**: critically damped.<br>> 1: overdamped. In this case, the spring approaches equilibrium gradually.<br>Default value: **0.825**| 156| overlapDuration | number | No | Duration for animations to overlap, in seconds. When animations overlap, if the **response** values of the two animations are different, they will transit smoothly over this duration.<br> Default value: **0**| 157 158 159**Return value** 160 161| Type | Description | 162| ---------------------------------- | ---------------- | 163| [ICurve](#icurve)| Curve.<br>Note: The spring animation curve is physics-based. Its duration depends on the **springMotion** parameters and the previous velocity, rather than the **duration** parameter in **animation** or **animateTo**. The time cannot be normalized. Therefore, the interpolation cannot be obtained by using the [interpolate](#interpolate) function of the curve.| 164 165**Example** 166 167```ts 168import Curves from '@ohos.curves' 169Curves.springMotion() // Create a spring animation curve with default settings. 170Curves.springMotion(0.5) // Create a spring animation curve with the specified response value. 171Curves.springMotion (0.5, 0.6) // Create a spring animation curve with the specified response and dampingFraction values. 172Curves.springMotion(0.5, 0.6, 0) // Create a spring animation curve with the specified parameter values. 173``` 174 175 176## Curves.responsiveSpringMotion<sup>9+</sup> 177 178responsiveSpringMotion(response?: number, dampingFraction?: number, overlapDuration?: number): ICurve 179 180Creates a responsive spring animation curve. It is a special case of [springMotion](#curvesspringmotion9), with the only difference in the default values. It can be used together with **springMotion**. 181 182**System capability**: SystemCapability.ArkUI.ArkUI.Full 183 184**Parameters** 185| Name | Type | Mandatory | Description | 186| --------- | ------ | ---- | ----- | 187| response | number | No | See **response** in **springMotion**. Default value: **0.15**| 188| dampingFraction | number | No | See **dampingFraction** in **springMotion**. Default value: **0.86**| 189| overlapDuration | number | No | See **overlapDuration** in **springMotion**. Default value: **0.25**| 190 191**Return value** 192 193| Type | Description | 194| ---------------------------------- | ---------------- | 195| [ICurve](#icurve)| Curve.<br>**NOTE**<br>1. To apply custom settings for a spring animation, you are advised to use **springMotion**. When using **responsiveSpringMotion**, you are advised to retain the default settings.<br>2. The duration of the responsive spring animation depends on the **responsiveSpringMotion** parameters and the previous velocity, rather than the **duration** parameter in **animation** or **animateTo**. In addition, the interpolation cannot be obtained by using the [interpolate](#interpolate) function of the curve.| 196 197**Example** 198 199```ts 200import Curves from '@ohos.curves' 201Curves.responsiveSpringMotion() // Create a responsive spring animation curve with default settings. 202``` 203 204 205## ICurve 206 207 208### interpolate 209 210interpolate(fraction: number): number 211 212Implements calculation. 213 214Since API version 9, this API is supported in ArkTS widgets. 215 216**System capability**: SystemCapability.ArkUI.ArkUI.Full 217 218**Parameters** 219 220| Name | Type | Mandatory| Description | 221| -------- | ------ | ---- | -------------------------------------------- | 222| fraction | number | Yes | Current normalized time. The value ranges from 0 to 1.| 223 224**Return value** 225 226| Type | Description | 227| ------ | ------------------------------------ | 228| number | Curve interpolation corresponding to the normalized time point.| 229 230**Example** 231 232```ts 233import Curves from '@ohos.curves' 234let curve = Curves.initCurve(Curve.EaseIn) // Create an ease-in curve. 235let value: number = curve.interpolate(0.5) // Calculate the interpolation for half of the time. 236``` 237 238 239## Curves.init<sup>(deprecated)</sup> 240 241init(curve?: Curve): string 242 243 244Implements initialization to create a curve. This API is deprecated since API version 9. You are advised to use [Curves.initCurve](#curvesinitcurve9) instead. 245 246**System capability**: SystemCapability.ArkUI.ArkUI.Full 247 248**Parameters** 249 250| Name| Type | Mandatory| Default Value | Description | 251| ------ | ------------------------------------------------------------ | ---- | ------------ | ---------- | 252| curve |[Curve](../arkui-ts/ts-appendix-enums.md#curve) | No | Curve.Linear | Curve type.| 253 254 255## Curves.steps<sup>(deprecated)</sup> 256 257steps(count: number, end: boolean): string 258 259 260Creates a step curve. This API is deprecated since API version 9. You are advised to use [Curves.stepsCurve](#curvesstepscurve9) instead. 261 262**System capability**: SystemCapability.ArkUI.ArkUI.Full 263 264**Parameters** 265 266| Name| Type | Mandatory| Description | 267| ------ | ------- | ----| ------------------------------------------------------------ | 268| count | number | Yes | Number of steps. The value must be a positive integer. | 269| end | boolean | Yes | Whether jumping occurs when the interpolation ends.<br>- **true**: Jumping occurs when the interpolation ends.<br>- **false**: Jumping occurs when the interpolation starts.| 270 271 272## Curves.cubicBezier<sup>(deprecated)</sup> 273 274cubicBezier(x1: number, y1: number, x2: number, y2: number): string 275 276 277Creates a cubic Bezier curve. The curve value must range from 0 to 1. This API is deprecated since API version 9. You are advised to use [Curves.cubicBezierCurve](#curvescubicbeziercurve9) instead. 278 279**System capability**: SystemCapability.ArkUI.ArkUI.Full 280 281**Parameters** 282| Name | Type | Mandatory | Description | 283| ---- | ------ | ---- | -------------- | 284| x1 | number | Yes | X coordinate of the first point on the Bezier curve.| 285| y1 | number | Yes | Y coordinate of the first point on the Bezier curve.| 286| x2 | number | Yes | X coordinate of the second point on the Bezier curve.| 287| y2 | number | Yes | Y coordinate of the second point on the Bezier curve.| 288 289 290## Curves.spring<sup>(deprecated)</sup> 291 292spring(velocity: number, mass: number, stiffness: number, damping: number): string 293 294 295Creates a spring curve. This API is deprecated since API version 9. You are advised to use [Curves.springCurve](#curvesspringcurve9) instead. 296 297**System capability**: SystemCapability.ArkUI.ArkUI.Full 298 299**Parameters** 300 301| Name | Type | Mandatory | Description | 302| --------- | ------ | ---- | ----- | 303| velocity | number | Yes | Initial velocity. It is applied by external factors to the elastic animation. It aims to help ensure the smooth transition from the previous motion state to the elastic animation.| 304| mass | number | Yes | Mass, which influences the inertia in the spring system. The greater the mass, the greater the amplitude of the oscillation, and the slower the speed of restoring to the equilibrium position.| 305| 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.| 306| 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.| 307 308## Example 309 310```ts 311// xxx.ets 312import Curves from '@ohos.curves' 313@Entry 314@Component 315struct ImageComponent { 316 @State widthSize: number = 200 317 @State heightSize: number = 200 318 319 build() { 320 Column() { 321 Text() 322 .margin({top:100}) 323 .width(this.widthSize) 324 .height(this.heightSize) 325 .backgroundColor(Color.Red) 326 .onClick(()=> { 327 let curve = Curves.cubicBezierCurve(0.25, 0.1, 0.25, 1.0); 328 this.widthSize = curve.interpolate(0.5) * this.widthSize; 329 this.heightSize = curve.interpolate(0.5) * this.heightSize; 330 }) 331 .animation({ duration: 2000 , curve: Curves.stepsCurve(9, true) }) 332 }.width("100%").height("100%") 333 } 334} 335``` 336 337 338