Skip to content

Commit 9047edf

Browse files
committed
updated remapRange to accept arrays, adding tests, removing default export to be more like ESM (can always import * as lathe)
1 parent 3006197 commit 9047edf

7 files changed

Lines changed: 2533 additions & 224 deletions

File tree

docs/src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

index.d.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** Contains the minimum and maximum of a range */
2+
declare type Range = [number, number];
13
/**
24
* Maps bipolar numbers [-1, 1] to the unipolar closed unit interval [0, 1]
35
*/
@@ -6,6 +8,10 @@ export declare const biToUni: (v: number) => number;
68
* Maps unipolar numbers [0, 1] to the bipolar closed interval [-1, 1]
79
*/
810
export declare const uniToBi: (v: number) => number;
11+
/**
12+
* Maps a number from one range to another, optionally clamping it to the input range
13+
*/
14+
export declare const remapRange: (input: number, inputRange: Range, outputRange?: Range, clampInput?: boolean) => number;
915
/**
1016
* Clamps overflowing numbers within the closed interval [min, max]
1117
*/
@@ -43,7 +49,7 @@ export declare const doubleExponentialSigmoid: (x: number, a: number) => number;
4349
* @see http://www.flong.com/archive/texts/code/shapers_exp/index.html
4450
*/
4551
export declare const doubleExponentialSeat: (input: number, a: number) => number;
46-
export declare const cubicSlope: (input: number, bias: number) => number;
52+
export declare const cubicSlope: (input: number, bias: number, tension?: number) => number;
4753
/**
4854
* @see http://www.flong.com/archive/texts/code/shapers_bez/index.html
4955
*/
@@ -103,28 +109,4 @@ export declare const mirrorAcrossOrigin: (input: number, fn: any, ...args: any[]
103109
* Calls function `fn` reflected across the point at `x`, `y`
104110
*/
105111
export declare function inflectionThroughPoint<Fn extends (...args: any[]) => number>(input: any, x: any, y: any, fn: Fn, ...args: Omit<Parameters<Fn>, "input">): any;
106-
declare const functions: {
107-
biToUni: (v: number) => number;
108-
uniToBi: (v: number) => number;
109-
clamp: (x: number, min: number, max: number) => number;
110-
lerp: (input: number, a: number, b: number) => number;
111-
tanh: (x: any, gain?: number) => number;
112-
quadraticThroughAGivenPoint: (input: number, x: number, y: number, clamped?: boolean) => number;
113-
quadraticBezier: (input: number, x: number, y: number) => number;
114-
quadraticSlope: (input: number, bias: number) => number;
115-
doubleExponentialSigmoid: (x: number, a: number) => number;
116-
doubleExponentialSeat: (input: number, a: number) => number;
117-
quantize: (input: number, step: number, algorithm?: "round" | "ceil" | "floor") => number;
118-
fold: (input: number, gain: number) => number;
119-
pcurve: (x: number, a: number, b: number) => number;
120-
sineFold: (input: number, gain: number) => number;
121-
circularArc: (input: number, bias: number) => number;
122-
logistic: (input: number, gain: number) => number;
123-
smoothStep: (input: number, edge0: number, edge1: number) => number;
124-
linearStep: (input: number, x: number, y: number) => number;
125-
polyline: (input: number, midpointX: number, midpointY: number) => number;
126-
mirrorAcrossY: (input: number, fn: any, ...args: any[]) => any;
127-
mirrorAcrossOrigin: (input: number, fn: any, ...args: any[]) => any;
128-
inflectionThroughPoint: typeof inflectionThroughPoint;
129-
};
130-
export default functions;
112+
export {};

index.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const biToUni = (v) => v / 2 + 0.5;
1010
* Maps unipolar numbers [0, 1] to the bipolar closed interval [-1, 1]
1111
*/
1212
const uniToBi = (v) => v * 2 - 1;
13+
/**
14+
* Maps a number from one range to another, optionally clamping it to the input range
15+
*/
16+
const remapRange = (input, inputRange, outputRange = [0, 1], clampInput = true) => {
17+
const [inMin, inMax] = inputRange;
18+
const [outMin, outMax] = outputRange;
19+
const clampedInput = clampInput ? clamp(input, inMin, inMax) : input;
20+
const normalized = (clampedInput - inMin) / (inMax - inMin);
21+
return lerp(normalized, outMin, outMax);
22+
};
1323
/**
1424
* Clamps overflowing numbers within the closed interval [min, max]
1525
*/
@@ -135,8 +145,20 @@ const doubleExponentialSeat = (input, a) => {
135145
};
136146
const cubicSlope = (input,
137147
/** range from 0..1 */
138-
bias) => {
139-
return cubicBezier(input, 1 - bias, bias, 1 - bias, bias);
148+
bias,
149+
/** range from 0..1 */
150+
tension = 0.5) => {
151+
// Calculate pointA based on bias
152+
const pointA = [
153+
tension * (1 - bias),
154+
tension * (bias) // y goes from 0 to 1 as bias goes 0->1
155+
];
156+
// Calculate pointB as the mirrored point of pointA
157+
const pointB = [
158+
1 - pointA[1],
159+
1 - pointA[0]
160+
];
161+
return cubicBezier(input, pointA[0], pointA[1], pointB[0], pointB[1]);
140162
};
141163
/**
142164
* @see http://www.flong.com/archive/texts/code/shapers_bez/index.html
@@ -344,28 +366,4 @@ function inflectionThroughPoint(input, x, y, fn, ...args) {
344366
return (1 - fn(1 - (input - x) / (1 - x + epsilon), ...args)) * (1 - y) + y;
345367
// input scaled to 0 1
346368
}
347-
}
348-
const functions = {
349-
biToUni,
350-
uniToBi,
351-
clamp,
352-
lerp,
353-
tanh,
354-
quadraticThroughAGivenPoint,
355-
quadraticBezier,
356-
quadraticSlope,
357-
doubleExponentialSigmoid,
358-
doubleExponentialSeat,
359-
quantize,
360-
fold,
361-
pcurve,
362-
sineFold,
363-
circularArc,
364-
logistic,
365-
smoothStep,
366-
linearStep,
367-
polyline,
368-
mirrorAcrossY,
369-
mirrorAcrossOrigin,
370-
inflectionThroughPoint,
371-
};export{biToUni,circularArc,clamp,cubicBezier,cubicSlope,functions as default,doubleCubicSeat,doubleCubicSeatWithLinearBlend,doubleExponentialSeat,doubleExponentialSigmoid,ease,fold,inflectionThroughPoint,lerp,linearStep,logistic,mirrorAcrossOrigin,mirrorAcrossY,pcurve,polyline,quadraticBezier,quadraticSlope,quadraticThroughAGivenPoint,quantize,sineFold,smoothStep,tanh,uniToBi};
369+
}export{biToUni,circularArc,clamp,cubicBezier,cubicSlope,doubleCubicSeat,doubleCubicSeatWithLinearBlend,doubleExponentialSeat,doubleExponentialSigmoid,ease,fold,inflectionThroughPoint,lerp,linearStep,logistic,mirrorAcrossOrigin,mirrorAcrossY,pcurve,polyline,quadraticBezier,quadraticSlope,quadraticThroughAGivenPoint,quantize,remapRange,sineFold,smoothStep,tanh,uniToBi};

0 commit comments

Comments
 (0)