# CurveSet

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/core/math/curve-set.js#L24

A curve set is a collection of curves that share a time axis and are evaluated together, such as
the three channels of a color or the components of a vector changing over time.

Build one from an array of `[time, value, ...]` key arrays, one per curve, or from a number of
empty curves. Setting [type](https://api.playcanvas.com/engine/classes/CurveSet.md#type) applies that interpolation to every curve in the set, and
[value](https://api.playcanvas.com/engine/classes/CurveSet.md#value) returns the value of each curve at a time as one array. Reach an individual
[Curve](https://api.playcanvas.com/engine/classes/Curve.md) with [get](https://api.playcanvas.com/engine/classes/CurveSet.md#get).

**Example**

```ts
// Animate an RGB color over time and sample it at the midpoint
const colorOverTime = new CurveSet([
    [0, 1, 1, 0],   // red:   1 at t = 0, 0 at t = 1
    [0, 0, 1, 1],   // green: 0 at t = 0, 1 at t = 1
    [0, 0, 1, 0]    // blue:  0 throughout
]);
const [r, g, b] = colorOverTime.value(0.5);
```

## Constructors

### constructor

```ts
new CurveSet(...args: any[])
```

Creates a new CurveSet instance.

**Parameters**

- `args` (`any[]`): Variable arguments with several possible formats:
  - No arguments: Creates a CurveSet with a single default curve.
  - Single number argument: Creates a CurveSet with the specified number of default curves.
  - Single array argument: An array of arrays, where each sub-array contains keys (pairs of
  numbers with the time first and value second).
  - Multiple arguments: Each argument becomes a separate curve.

**Example**

```ts
// Create from an array of arrays of keys
const curveSet = new CurveSet([
    [
        0, 0,        // At 0 time, value of 0
        0.33, 2,     // At 0.33 time, value of 2
        0.66, 2.6,   // At 0.66 time, value of 2.6
        1, 3         // At 1 time, value of 3
    ],
    [
        0, 34,
        0.33, 35,
        0.66, 36,
        1, 37
    ]
]);
```

## Properties

### curves

```ts
curves: Curve[] = []
```

The array of curves in the set.

## Accessors

### length

```ts
get length(): number
```

Gets the number of curves in the curve set.

### type

```ts
get type(): number
set type(value: number)
```

Gets the interpolation scheme applied to all curves in the curve set.

## Methods

### add

```ts
add(data?: number[]): Curve
```

Appends a new curve to the curve set. The new curve adopts the curve set's current
[CurveSet#type](https://api.playcanvas.com/engine/classes/CurveSet.md#type) interpolation scheme, so that all curves in the set continue to share
the same type.

**Parameters**

- `data` (`number[]`, optional): An array of keys (pairs of numbers with the time first and value
  second) for the new curve.

**Returns** [`Curve`](https://api.playcanvas.com/engine/classes/Curve.md): The newly created curve.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1]]);
const curve = curveSet.add([0, 0, 1, 0.5]); // append a second curve
```

### clear

```ts
clear(): CurveSet
```

Removes all curves from the curve set, leaving it empty.

**Returns** [`CurveSet`](https://api.playcanvas.com/engine/classes/CurveSet.md): The curve set instance.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1], [0, 0, 1, 0.5]]);
curveSet.clear(); // the set now has no curves
```

### clearKeys

```ts
clearKeys(): CurveSet
```

Removes all keys from every curve in the set, while keeping the curves themselves. The number
of curves is unchanged, so [CurveSet#value](https://api.playcanvas.com/engine/classes/CurveSet.md#value) still returns an array of the same length.

**Returns** [`CurveSet`](https://api.playcanvas.com/engine/classes/CurveSet.md): The curve set instance.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1], [0, 0, 1, 0.5]]);
curveSet.clearKeys(); // both curves are now empty, but the set still has 2 curves
```

### clone

```ts
clone(): CurveSet
```

Returns a clone of the specified curve set object.

**Returns** [`CurveSet`](https://api.playcanvas.com/engine/classes/CurveSet.md): A clone of the specified curve set.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1]]);
const clonedCurveSet = curveSet.clone();
```

### get

```ts
get(index: number): Curve
```

Return a specific curve in the curve set.

**Parameters**

- `index` (`number`): The index of the curve to return.

**Returns** [`Curve`](https://api.playcanvas.com/engine/classes/Curve.md): The curve at the specified index.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1], [0, 0, 1, 0.5]]);
const curve = curveSet.get(0); // returns the first curve
```

### remove

```ts
remove(indexOrCurve: number | Curve): Curve | null
```

Removes a curve from the curve set.

**Parameters**

- `indexOrCurve` (`number |` [`Curve`](https://api.playcanvas.com/engine/classes/Curve.md)): The index of the curve to remove, or the curve instance
  itself.

**Returns** [`Curve`](https://api.playcanvas.com/engine/classes/Curve.md) `| null`: The removed curve, or null if it was not found.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1], [0, 0, 1, 0.5]]);
curveSet.remove(0);             // remove by index
curveSet.remove(curveSet.get(0)); // or remove by reference
```

### value

```ts
value(time: number, result?: number[]): number[]
```

Returns the interpolated value of all curves in the curve set at the specified time.

**Parameters**

- `time` (`number`): The time at which to calculate the value.
- `result` (`number[]`, optional, default `[]`): The interpolated curve values at the specified time. If this
  parameter is not supplied, the function allocates a new array internally to return the
  result.

**Returns** `number[]`: The interpolated curve values at the specified time.

**Example**

```ts
const curveSet = new CurveSet([[0, 0, 1, 1], [0, 0, 1, 0.5]]);
const values = curveSet.value(0.5); // returns interpolated values for all curves at time 0.5
```
