-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinterpolate.ts
More file actions
55 lines (49 loc) · 1.36 KB
/
interpolate.ts
File metadata and controls
55 lines (49 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Interpolation utility functions
*
* This module provides functions for interpolating between values,
* used in the animation key generation process.
*/
/**
* Interpolates between two arrays of numbers
* @param fromList Starting values
* @param toList Ending values
* @param f Interpolation factor (0.0 to 1.0)
* @returns Array of interpolated values
*/
import { InterpolationInputError } from "./errors.ts";
function interpolate(
fromList: number[],
toList: number[],
f: number,
): number[] {
if (fromList.length !== toList.length) {
throw new InterpolationInputError(fromList.length, toList.length);
}
const out: number[] = [];
for (let i = 0; i < fromList.length; i++) {
out.push(interpolateNum(fromList[i], toList[i], f));
}
return out;
}
/**
* Interpolates between two values (numeric or boolean)
* @param fromVal Starting value
* @param toVal Ending value
* @param f Interpolation factor (0.0 to 1.0)
* @returns Interpolated value
*/
function interpolateNum(
fromVal: number | boolean,
toVal: number | boolean,
f: number,
): number {
if (typeof fromVal === "number" && typeof toVal === "number") {
return fromVal * (1 - f) + toVal * f;
}
if (typeof fromVal === "boolean" && typeof toVal === "boolean") {
return f < 0.5 ? (fromVal ? 1 : 0) : toVal ? 1 : 0;
}
return 0;
}
export { interpolate, interpolateNum };