Skip to main content

AnimationCurve

AnimationCurve

Describes change of a numeric value over time. Values are stored in Keyframes, interpolation is defined by tangents on Keyframes. The curve is a cubic Hermite spline, see https://en.wikipedia.org/wiki/Cubic_Hermite_spline.

Kind: global class
Implements: Iterable<Keyframe>
Author: Alex Goldring
Copyright: Company Named Limited (c) 2025

new exports.AnimationCurve()

Example

const jump_curve = AnimationCurve.from([
Keyframe.from(0, 0), // start at height 0
Keyframe.from(0.3, 2), // at time 0.3, jump height will be 2 meters
Keyframe.from(1, 0) // at time 1.0, land back on the ground
]);

jump_curve.evaluate(0.1); // what is the height at time 0.1?

Example

const curve = AnimationCurve.easeInOut();

sprite.transparency = curve.evaluate(time); // smoothly animate transparency of the sprite

animationCurve.keys : Array.<Keyframe>

Keyframes defining the curve, in chronological order. The curve manages the contents of this array, do not modify it directly.

Kind: instance property of AnimationCurve
Read only: true

animationCurve.length ⇒ number

Number of keyframes in the curve.

Kind: instance property of AnimationCurve

animationCurve.start_time ⇒ number

Timestamp of the first keyframe. Returns 0 if there are no keyframes.

Kind: instance property of AnimationCurve

animationCurve.end_time ⇒ number

Time of the last chronological key in the curve. Returns 0 if there are no keyframes.

Kind: instance property of AnimationCurve

animationCurve.duration ⇒ number

Time difference between the first and the last keyframe. Returns 0 if there are no keyframes.

Kind: instance property of AnimationCurve

animationCurve.isAnimationCurve : boolean

Useful for fast type checks

Kind: instance property of AnimationCurve
Read only: true

animationCurve.getKeyIndexByTime

use getKeyIndexLow instead

Kind: instance property of AnimationCurve

animationCurve.add(key) ⇒ number

Add a new keyframe into the animation. Keyframes can be added out of order, they will be inserted into the correct chronological position.

Kind: instance method of AnimationCurve
Returns: number - key index where it was inserted at

ParamType
keyKeyframe

animationCurve.addMany(keys)

Insert multiple keyframes. Input doesn't need to be sorted.

Kind: instance method of AnimationCurve

ParamType
keysArray.<Keyframe>

animationCurve.remove(key) ⇒ boolean

Kind: instance method of AnimationCurve
Returns: boolean - true if the key was removed, false if the key was not found

ParamType
keyKeyframe

animationCurve.clear()

Remove all keys, making the curve empty.

Kind: instance method of AnimationCurve

animationCurve.isEmpty() ⇒ boolean

Does this curve have any keyframes?

Kind: instance method of AnimationCurve
Returns: boolean - true iff keyframe count == 0, false otherwise

animationCurve.getKeyIndexLow(time) ⇒ number

Returns index of a key that is just before or at the time specified. Useful for insertion and evaluation logic. Note: if time is past the end of last key - index of the last key will be returned instead

Kind: instance method of AnimationCurve
Returns: number - index of the key

ParamType
timenumber

animationCurve.evaluate(time) ⇒ number

Evaluate interpolated value across the curve at a given time.

Kind: instance method of AnimationCurve
Returns: number - value at the specified time

ParamTypeDescription
timenumbertime in seconds

animationCurve.alignTangents(index)

Set tangents of a key to match surrounding keys Produces a smoother looking curve

Kind: instance method of AnimationCurve

ParamTypeDescription
indexnumberindex of keyframe

animationCurve.smoothTangents(index, weight)

Kind: instance method of AnimationCurve

ParamTypeDescription
indexnumberIndex of keyframe to be affected
weightnumberHow much smoothing to apply, 1 will be fully smoothed out and 0 will have no effect at all. Value between 0 and 1

animationCurve.copy(other)

The copy is deep

Kind: instance method of AnimationCurve

ParamType
otherAnimationCurve

animationCurve.clone() ⇒ AnimationCurve

Kind: instance method of AnimationCurve

animationCurve.equals(other) ⇒ boolean

Kind: instance method of AnimationCurve

ParamType
otherAnimationCurve

animationCurve.hash() ⇒ number

Kind: instance method of AnimationCurve

AnimationCurve.from(keys) ⇒ AnimationCurve

Utility constructor

Kind: static method of AnimationCurve

ParamType
keysArray.<Keyframe>

AnimationCurve.easeInOut([timeStart], [valueStart], [timeEnd], [valueEnd]) ⇒ AnimationCurve

S-shaped curve that starts slowly, ramps up and flattens out again. Useful for pleasing transitions where exit and entry should not be abrupt.

Kind: static method of AnimationCurve

ParamTypeDefault
[timeStart]number0
[valueStart]number0
[timeEnd]number1
[valueEnd]number1

AnimationCurve.constant([timeStart], [timeEnd], [value]) ⇒ AnimationCurve

A flat-line curve with a specific start and end time

Kind: static method of AnimationCurve

ParamTypeDefault
[timeStart]number0
[timeEnd]number1
[value]number0

AnimationCurve.linear([timeStart], [valueStart], [timeEnd], [valueEnd]) ⇒ AnimationCurve

Curve with two keyframes connected by a straight line

Kind: static method of AnimationCurve

ParamTypeDefault
[timeStart]number0
[valueStart]number0
[timeEnd]number1
[valueEnd]number1