Skip to content

Commit 107eb5c

Browse files
committed
update types
1 parent 5297b39 commit 107eb5c

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/index.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,37 @@ function animate(
123123

124124
// Performs animation on each element of array individually
125125
function animateArrayFromProgress(
126-
startArray: number[],
127-
endArray: number[],
126+
startArray: Vector,
127+
endArray: Vector,
128128
progressAmount: number
129-
): number[] {
130-
// Array Subtraction
131-
const arrayDelta = endArray.map(
132-
(item, index) => item - startArray[index]
133-
);
134-
// Multiply difference by progress
135-
const deltaProgressed = arrayDelta.map(item => item * progressAmount);
136-
// Add to current key and return
137-
return startArray.map((item, index) => item + deltaProgressed[index]);
129+
): Vector {
130+
if (startArray.length !== endArray.length) {
131+
// Check that the key values are the same dimension
132+
throw new TypeError(
133+
`Keyframe ${curKeyNum} and ${curKeyNum +
134+
1} values must be of the same dimension. Received ${
135+
startArray.length
136+
} and ${endArray.length}`
137+
);
138+
} else {
139+
// Arrays are the same length
140+
// TypeScript doesn't know if the Array elements exist in a .map()
141+
// so we need to provide a fallback
142+
// Array Subtraction
143+
const arrayDelta: Vector = endArray.map((dimension, index) => {
144+
const curKeyDim = dimension || 0;
145+
const nextKeyDim = startArray[index] || 0;
146+
return curKeyDim - nextKeyDim;
147+
}) as Vector;
148+
// Multiply difference by progress
149+
const deltaProgressed = arrayDelta.map(
150+
item => (item || 0) * progressAmount
151+
);
152+
// Add to current key and return
153+
return startArray.map(
154+
(item, index) => item || 0 + deltaProgressed[index]
155+
) as Vector;
156+
}
138157
}
139158

140159
// Animate between values according to progress

0 commit comments

Comments
 (0)