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
- AnimationCurve
- new exports.AnimationCurve()
- instance
- .keys :
Array.<Keyframe> - .length ⇒
number - .start_time ⇒
number - .end_time ⇒
number - .duration ⇒
number - .isAnimationCurve :
boolean .getKeyIndexByTime- .add(key) ⇒
number - .addMany(keys)
- .remove(key) ⇒
boolean - .clear()
- .isEmpty() ⇒
boolean - .getKeyIndexLow(time) ⇒
number - .evaluate(time) ⇒
number - .alignTangents(index)
- .smoothTangents(index, weight)
- .copy(other)
- .clone() ⇒
AnimationCurve - .equals(other) ⇒
boolean - .hash() ⇒
number
- .keys :
- static
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
| Param | Type |
|---|---|
| key | Keyframe |
animationCurve.addMany(keys)
Insert multiple keyframes. Input doesn't need to be sorted.
Kind: instance method of AnimationCurve
| Param | Type |
|---|---|
| keys | Array.<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
| Param | Type |
|---|---|
| key | Keyframe |
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
| Param | Type |
|---|---|
| time | number |
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
| Param | Type | Description |
|---|---|---|
| time | number | time in seconds |
animationCurve.alignTangents(index)
Set tangents of a key to match surrounding keys Produces a smoother looking curve
Kind: instance method of AnimationCurve
| Param | Type | Description |
|---|---|---|
| index | number | index of keyframe |
animationCurve.smoothTangents(index, weight)
Kind: instance method of AnimationCurve
| Param | Type | Description |
|---|---|---|
| index | number | Index of keyframe to be affected |
| weight | number | How 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
| Param | Type |
|---|---|
| other | AnimationCurve |
animationCurve.clone() ⇒ AnimationCurve
Kind: instance method of AnimationCurve
animationCurve.equals(other) ⇒ boolean
Kind: instance method of AnimationCurve
| Param | Type |
|---|---|
| other | AnimationCurve |
animationCurve.hash() ⇒ number
Kind: instance method of AnimationCurve
AnimationCurve.from(keys) ⇒ AnimationCurve
Utility constructor
Kind: static method of AnimationCurve
| Param | Type |
|---|---|
| keys | Array.<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
| Param | Type | Default |
|---|---|---|
| [timeStart] | number | 0 |
| [valueStart] | number | 0 |
| [timeEnd] | number | 1 |
| [valueEnd] | number | 1 |
AnimationCurve.constant([timeStart], [timeEnd], [value]) ⇒ AnimationCurve
A flat-line curve with a specific start and end time
Kind: static method of AnimationCurve
| Param | Type | Default |
|---|---|---|
| [timeStart] | number | 0 |
| [timeEnd] | number | 1 |
| [value] | number | 0 |
AnimationCurve.linear([timeStart], [valueStart], [timeEnd], [valueEnd]) ⇒ AnimationCurve
Curve with two keyframes connected by a straight line
Kind: static method of AnimationCurve
| Param | Type | Default |
|---|---|---|
| [timeStart] | number | 0 |
| [valueStart] | number | 0 |
| [timeEnd] | number | 1 |
| [valueEnd] | number | 1 |