UNPKG

animare

Version:

Advanced animation library for modern JavaScript.

14 lines 945 B
import{isObjectVector}from'./utils.js';/** * Linearly interpolates between two values. * * ⚠️ **WARNING** ⚠️ This function throws an error if the values are invalid. * * @param start - The start value or vector. * @param end - The end value or vector. * @param t - The interpolation factor (0.0 to 1.0). * @returns The interpolated value or vector. */export function lerp(start,end,t){// Single number interpolation if(typeof start==='number'&&typeof end==='number'){return start+t*(end-start);}// Array-based vector interpolation if(Array.isArray(start)&&Array.isArray(end)){return start.map((s,i)=>s+t*(end[i]-s));}// Object-based vector interpolation if(isObjectVector(start)&&isObjectVector(end)){const result=Object.assign({});for(const key in start){if(key in start&&key in end){const k=key;// x | y | z | w result[k]=start[k]+t*(end[k]-start[k]);}}return result;}throw new Error('Invalid input types for lerp function');}