11import { Shape } from "../types" ;
22import { split , splitLine , mod } from "../util" ;
33
4- // TODO allow percentage > 1
54// OPT? loop
5+ // TODO smooth between
66
77const interpolateAngle = ( percentage : number , a : number , b : number ) : number => {
88 const tau = Math . PI * 2 ;
@@ -20,17 +20,18 @@ const interpolateAngle = (percentage: number, a: number, b: number): number => {
2020
2121const interpolateBetween = ( percentage : number , a : Shape , b : Shape ) : Shape => {
2222 if ( a . length !== b . length ) throw new Error ( "shapes have different number of points" ) ;
23+ const clamped = Math . min ( 1 , Math . max ( 0 , percentage ) ) ;
2324 const shape : Shape = [ ] ;
2425 for ( let i = 0 ; i < a . length ; i ++ ) {
2526 shape . push ( {
2627 ...splitLine ( percentage , a [ i ] , b [ i ] ) ,
2728 handleIn : {
2829 angle : interpolateAngle ( percentage , a [ i ] . handleIn . angle , b [ i ] . handleIn . angle ) ,
29- length : split ( percentage , a [ i ] . handleIn . length , b [ i ] . handleIn . length ) ,
30+ length : split ( clamped , a [ i ] . handleIn . length , b [ i ] . handleIn . length ) ,
3031 } ,
3132 handleOut : {
3233 angle : interpolateAngle ( percentage , a [ i ] . handleOut . angle , b [ i ] . handleOut . angle ) ,
33- length : split ( percentage , a [ i ] . handleOut . length , b [ i ] . handleOut . length ) ,
34+ length : split ( clamped , a [ i ] . handleOut . length , b [ i ] . handleOut . length ) ,
3435 } ,
3536 } ) ;
3637 }
@@ -41,6 +42,6 @@ export const interpolateBetweenLoop = (percentage: number, a: Shape, b: Shape):
4142 if ( percentage < 0.5 ) {
4243 return interpolateBetween ( 2 * percentage , a , b ) ;
4344 } else {
44- return interpolateBetween ( 2 * percentage - 1 , b , a ) ;
45+ return interpolateBetween ( - 2 * percentage + 2 , a , b ) ;
4546 }
4647} ;
0 commit comments