ohos.curves (Interpolation Calculation)

Note:

Currently in the beta phase.

Provides interpolator functions such as initialization, cubic Bézier curves, and spring curves.

Import Module

import kit.ArkUI.*

class Curves

public class Curves {}

Function: Contains interpolator functions such as initialization, cubic Bézier curves, and spring curves.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

static func cubicBezierCurve(Float32, Float32, Float32, Float32)

public static func cubicBezierCurve(x1: Float32, y1: Float32, x2: Float32, y2: Float32): ICurve

Function: Constructs a cubic Bézier curve object, ensuring the curve values are between 0 and 1.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
x1 Float32 Yes - Determines the x-coordinate of the first point of the Bézier curve.
Range: [0, 1]
Note:
Values less than 0 are treated as 0; values greater than 1 are treated as 1.
y1 Float32 Yes - Determines the y-coordinate of the first point of the Bézier curve.
Range: (-∞, +∞).
x2 Float32 Yes - Determines the x-coordinate of the second point of the Bézier curve.
Range: [0, 1].
Note:
Values less than 0 are treated as 0; values greater than 1 are treated as 1.
y2 Float32 Yes - Determines the y-coordinate of the second point of the Bézier curve.
Range: (-∞, +∞).

Return Value:

Type Description
ICurve Returns the interpolator object of the curve.

static func customCurve((Float32) -> Float32)

public static func customCurve(interpolate: (Float32) -> Float32): ICurve

Function: Creates a custom curve.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
interpolate (Float32) -> Float32 Yes - User-defined interpolation callback function.
The input x value for interpolation at the start of the animation.
Range: [0, 1].
The return value is the y value of the curve.
Range: [0, 1].
Note:
When fraction equals 0, a return value of 0 corresponds to the animation start point. A non-zero return value will cause a jump effect at the start. When fraction equals 1, a return value of 1 corresponds to the animation end point. A non-1 return value will cause the animation's final value to deviate from the state variable's value, resulting in a jump effect to the state variable's value.

Return Value:

Type Description
ICurve Returns the interpolator object of the curve.

static func initCurve(Curve)

public static func initCurve(curve!: Curve = Curve.Linear): ICurve

Function: Initializes an interpolation curve function, creating an interpolation curve object based on the input parameters.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
curve Curve No Curve.Linear Named parameter. The type of curve.

Return Value:

Type Description
ICurve Returns the interpolator object of the curve.

static func interpolatingSpring(Float32, Float32, Float32, Float32)

public static func interpolatingSpring(velocity: Float32, mass: Float32, stiffness: Float32, damping: Float32): ICurve

Function: Constructs an interpolator spring curve object, generating an animation curve from 0 to 1. The actual animation value is calculated through interpolation based on the curve. The animation duration is determined by the curve parameters and is not controlled by the duration parameter in animation or animateTo.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
velocity Float32 Yes - Initial velocity. A parameter representing external influences on the spring animation, ensuring a smooth transition from the previous motion state to the spring animation. This velocity is normalized, equal to the actual velocity at the start of the animation divided by the change in the animation property.
Range: (-∞, +∞).
mass Float32 Yes - Mass. The object subjected to forces in the spring system, affecting the system's inertia. Larger mass results in greater oscillation amplitude and slower return to equilibrium.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as 1.
stiffness Float32 Yes - Stiffness. Represents the object's resistance to deformation under applied force. Higher stiffness results in faster return to equilibrium.
Note:
Values less than or equal to 0 are treated as 1.
damping Float32 Yes - Damping. Describes the oscillation and decay of the system after disturbance. Higher damping results in fewer oscillations and smaller amplitudes.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as 1.

Return Value:

Type Description
ICurve The interpolator object of the curve.
Note: The spring animation curve is a physical curve. The duration parameter in animation, animateTo, and pageTransition does not take effect. The animation duration depends on the interpolatingSpring curve parameters. Time cannot be normalized, so interpolation cannot be obtained via the interpolate function of this curve.

static func responsiveSpringMotion(Float32, Float32, Float32)

public static func responsiveSpringMotion(response!: Float32 = 0.15, dampingFraction!: Float32 = 0.86,
    overlapDuration!: Float32 = 0.25): ICurve

Function: Creates a responsive spring animation curve. It is a special case of springMotion with different default parameters and can be used alongside springMotion.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
response Float32 No 0.15 Named parameter. Same as response in springMotion.
Unit: seconds.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as the default value 0.15.
dampingFraction Float32 No 0.86 Named parameter. Same as dampingFraction in springMotion.
Unit: seconds.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as the default value 0.86.
overlapDuration Float32 No 0.25 Named parameter. Same as overlapDuration in springMotion.
Unit: seconds.
Range: (0, +∞).
Note:
The responsive spring animation curve is a special case of springMotion with different default parameters. For custom spring curves, it is recommended to use springMotion. For hand-following animations, it is recommended to use the default parameters of the responsive spring animation curve.
The duration parameter in animation, animateTo, and pageTransition does not take effect. The animation duration depends on the responsiveSpringMotion curve parameters and previous velocity. Interpolation cannot be obtained via the interpolate function of this curve.

Return Value:

Type Description
ICurve The curve object.
Note:
1. The responsive spring animation curve is a special case of springMotion with different default parameters. For custom spring curves, it is recommended to use springMotion. For hand-following animations, it is recommended to use the default parameters of the responsive spring animation curve.
2. The duration parameter in animation, animateTo, and pageTransition does not take effect. The animation duration depends on the responsiveSpringMotion curve parameters and previous velocity. Interpolation cannot be obtained via the interpolate function of this curve.

static func springCurve(Float32, Float32, Float32, Float32)

public static func springCurve(velocity: Float32, mass: Float32, stiffness: Float32, damping: Float32): ICurve

Function: Creates a spring curve object. The curve shape is determined by the spring parameters, and the animation duration is controlled by the duration parameter in animation and animateTo.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
velocity Float32 Yes - Initial velocity. Represents external influences on the spring animation, ensuring a smooth transition from the previous motion state to the spring animation. This velocity is normalized, equal to the actual velocity at the start of the animation divided by the change in the animation property.
Range: (-∞, +∞).
mass Float32 Yes - Mass. The object subjected to forces in the spring system, affecting the system's inertia. Larger mass results in greater oscillation amplitude and slower return to equilibrium.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as 1.
stiffness Float32 Yes - Stiffness. Represents the object's resistance to deformation under applied force. Higher stiffness results in faster return to equilibrium.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as 1.
damping Float32 Yes - Damping. Describes the oscillation and decay of the system after disturbance. Higher damping results in fewer oscillations and smaller amplitudes.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as 1.

Return Value:

Type Description
ICurve Returns the interpolator object of the curve.

static func springMotion(Float32, Float32, Float32)

public static func springMotion(response!: Float32 = 0.55, dampingFraction!: Float32 = 0.825,
    overlapDuration!: Float32 = 0.0): ICurve

Function: Creates a spring animation curve object. If multiple spring animations are applied to the same property of the same object, each animation will replace the previous one and inherit its velocity.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
response Float32 No 0.55 Named parameter. The natural oscillation period of the spring, determining the speed of returning to equilibrium.
Unit: seconds.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as the default value 0.55.
dampingFraction Float32 No 0.825 Named parameter. Damping coefficient.
0 indicates no damping, resulting in continuous oscillation;
Values greater than 0 and less than 1 indicate underdamping, causing overshooting;
1 indicates critical damping;
Values greater than 1 indicate overdamping, gradually approaching the target value.
Unit: seconds.
Range: (0, +∞).
Note:
Values less than or equal to 0 are treated as the default value 0.825.
overlapDuration Float32 No 0.0 Named parameter. The transition duration for spring animation inheritance. When animations are inherited, if the response parameters of consecutive spring animations differ, the response parameter will smoothly transition over the overlapDuration time.
Unit: seconds.
Range: (0, +∞).
Note:
Values less than 0 are treated as the default value 0.
The spring animation curve is a physical curve. The duration parameter in animation, animateTo, and pageTransition does not take effect. The animation duration depends on the springMotion curve parameters and previous velocity. Time cannot be normalized, so interpolation cannot be obtained via the interpolate function of this curve.

Return Value:

Type Description
ICurve The curve object.
Note: The spring animation curve is a physical curve. The duration parameter in animation, animateTo, and pageTransition does not take effect. The animation duration depends on the springMotion curve parameters and previous velocity. Time cannot be normalized, so interpolation cannot be obtained via the interpolate function of this curve.

static func stepsCurve(Int32, Bool)

public static func stepsCurve(count: Int32, end: Bool): ICurve

Function: Creates a step curve object.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
count Int32 Yes - The number of steps, which must be a positive integer.
Range: [1, +∞).
Note:
Values less than 1 are treated as 1.
end Bool Yes - Determines whether the step change occurs at the start or end of each interval.
- true: Step change occurs at the end.
- false: Step change occurs at the start.

Return Value:

Type Description
ICurve Returns the interpolator object of the curve.

class ICurve

public class ICurve {}

Function: A curve object that supports creating different types of curve objects via methods such as cubicBezierCurve and interpolatingSpring in this module. The curve object can call its member method interpolate.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

func interpolate(Float32)

public func interpolate(fraction: Float32): Float32

Function: The interpolation calculation function of the interpolation curve, which returns the current interpolation value based on the input normalized time parameter.

System Capability: SystemCapability.ArkUI.ArkUI.Full

Since: 22

Parameters:

Parameter Type Required Default Description
fraction Float32 Yes - The current normalized time parameter.
Range