toosoon-utils
Version:
Utility functions & classes
39 lines (38 loc) • 1.17 kB
JavaScript
import { catmullRom } from '../../geometry';
import Curve from './Curve';
/**
* Utility class for manipulating splines
*
* @exports
* @class SplineCurve
* @extends Curve
*/
export default class SplineCurve extends Curve {
type = 'SplineCurve';
points = [];
/**
* @param {Point[]} [points] Array of points defining the curve
*/
constructor(points = []) {
super();
this.points = points;
}
/**
* Interpolate a point on the Spline curve
*
* @param {number} t Normalized time value to interpolate
* @returns {Point} Interpolated coordinates on the curve
*/
getPoint(t) {
const points = this.points;
const p = (points.length - 1) * t;
const index = Math.floor(p);
const weight = p - index;
const p1 = points[index === 0 ? index : index - 1];
const cp1 = points[index];
const cp2 = points[index > points.length - 2 ? points.length - 1 : index + 1];
const p2 = points[index > points.length - 3 ? points.length - 1 : index + 2];
const point = catmullRom(weight, ...p1, ...cp1, ...cp2, ...p2);
return point;
}
}