1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export enum Extrapolation {
|
11 | IDENTITY = 'identity',
|
12 | CLAMP = 'clamp',
|
13 | EXTEND = 'extend',
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | type ExtrapolationAsString = 'identity' | 'clamp' | 'extend';
|
20 |
|
21 | interface InterpolationNarrowedInput {
|
22 | leftEdgeInput: number;
|
23 | rightEdgeInput: number;
|
24 | leftEdgeOutput: number;
|
25 | rightEdgeOutput: number;
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export interface ExtrapolationConfig {
|
32 | extrapolateLeft?: Extrapolation | string;
|
33 | extrapolateRight?: Extrapolation | string;
|
34 | }
|
35 |
|
36 | interface RequiredExtrapolationConfig {
|
37 | extrapolateLeft: Extrapolation;
|
38 | extrapolateRight: Extrapolation;
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | export type ExtrapolationType =
|
45 | | ExtrapolationConfig
|
46 | | Extrapolation
|
47 | | ExtrapolationAsString
|
48 | | undefined;
|
49 |
|
50 | function getVal(
|
51 | type: Extrapolation,
|
52 | coef: number,
|
53 | val: number,
|
54 | leftEdgeOutput: number,
|
55 | rightEdgeOutput: number,
|
56 | x: number
|
57 | ): number {
|
58 | 'worklet';
|
59 |
|
60 | switch (type) {
|
61 | case Extrapolation.IDENTITY:
|
62 | return x;
|
63 | case Extrapolation.CLAMP:
|
64 | if (coef * val < coef * leftEdgeOutput) {
|
65 | return leftEdgeOutput;
|
66 | }
|
67 | return rightEdgeOutput;
|
68 | case Extrapolation.EXTEND:
|
69 | default:
|
70 | return val;
|
71 | }
|
72 | }
|
73 |
|
74 | function isExtrapolate(value: string): value is Extrapolation {
|
75 | 'worklet';
|
76 |
|
77 | return (
|
78 |
|
79 | value === Extrapolation.EXTEND ||
|
80 | value === Extrapolation.CLAMP ||
|
81 | value === Extrapolation.IDENTITY
|
82 |
|
83 | );
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 | function validateType(type: ExtrapolationType): RequiredExtrapolationConfig {
|
89 | 'worklet';
|
90 |
|
91 | const extrapolationConfig: RequiredExtrapolationConfig = {
|
92 | extrapolateLeft: Extrapolation.EXTEND,
|
93 | extrapolateRight: Extrapolation.EXTEND,
|
94 | };
|
95 |
|
96 | if (!type) {
|
97 | return extrapolationConfig;
|
98 | }
|
99 |
|
100 | if (typeof type === 'string') {
|
101 | if (!isExtrapolate(type)) {
|
102 | throw new Error(
|
103 | `[Reanimated] Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolatation.CLAMP, Extrapolatation.EXTEND, Extrapolatation.IDENTITY]\n Valid example:
|
104 | interpolate(value, [inputRange], [outputRange], "clamp")`
|
105 | );
|
106 | }
|
107 | extrapolationConfig.extrapolateLeft = type;
|
108 | extrapolationConfig.extrapolateRight = type;
|
109 | return extrapolationConfig;
|
110 | }
|
111 |
|
112 |
|
113 | if (
|
114 | (type.extrapolateLeft && !isExtrapolate(type.extrapolateLeft)) ||
|
115 | (type.extrapolateRight && !isExtrapolate(type.extrapolateRight))
|
116 | ) {
|
117 | throw new Error(
|
118 | `[Reanimated] Unsupported value for "interpolate" \nSupported values: ["extend", "clamp", "identity", Extrapolatation.CLAMP, Extrapolatation.EXTEND, Extrapolatation.IDENTITY]\n Valid example:
|
119 | interpolate(value, [inputRange], [outputRange], {
|
120 | extrapolateLeft: Extrapolation.CLAMP,
|
121 | extrapolateRight: Extrapolation.IDENTITY
|
122 | }})`
|
123 | );
|
124 | }
|
125 |
|
126 | Object.assign(extrapolationConfig, type);
|
127 | return extrapolationConfig;
|
128 | }
|
129 |
|
130 | function internalInterpolate(
|
131 | x: number,
|
132 | narrowedInput: InterpolationNarrowedInput,
|
133 | extrapolationConfig: RequiredExtrapolationConfig
|
134 | ) {
|
135 | 'worklet';
|
136 | const { leftEdgeInput, rightEdgeInput, leftEdgeOutput, rightEdgeOutput } =
|
137 | narrowedInput;
|
138 | if (rightEdgeInput - leftEdgeInput === 0) {
|
139 | return leftEdgeOutput;
|
140 | }
|
141 | const progress = (x - leftEdgeInput) / (rightEdgeInput - leftEdgeInput);
|
142 | const val = leftEdgeOutput + progress * (rightEdgeOutput - leftEdgeOutput);
|
143 | const coef = rightEdgeOutput >= leftEdgeOutput ? 1 : -1;
|
144 |
|
145 | if (coef * val < coef * leftEdgeOutput) {
|
146 | return getVal(
|
147 | extrapolationConfig.extrapolateLeft,
|
148 | coef,
|
149 | val,
|
150 | leftEdgeOutput,
|
151 | rightEdgeOutput,
|
152 | x
|
153 | );
|
154 | } else if (coef * val > coef * rightEdgeOutput) {
|
155 | return getVal(
|
156 | extrapolationConfig.extrapolateRight,
|
157 | coef,
|
158 | val,
|
159 | leftEdgeOutput,
|
160 | rightEdgeOutput,
|
161 | x
|
162 | );
|
163 | }
|
164 |
|
165 | return val;
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | export function interpolate(
|
179 | x: number,
|
180 | inputRange: readonly number[],
|
181 | outputRange: readonly number[],
|
182 | type?: ExtrapolationType
|
183 | ): number {
|
184 | 'worklet';
|
185 | if (inputRange.length < 2 || outputRange.length < 2) {
|
186 | throw new Error(
|
187 | '[Reanimated] Interpolation input and output ranges should contain at least two values.'
|
188 | );
|
189 | }
|
190 |
|
191 | const extrapolationConfig = validateType(type);
|
192 | const length = inputRange.length;
|
193 | const narrowedInput: InterpolationNarrowedInput = {
|
194 | leftEdgeInput: inputRange[0],
|
195 | rightEdgeInput: inputRange[1],
|
196 | leftEdgeOutput: outputRange[0],
|
197 | rightEdgeOutput: outputRange[1],
|
198 | };
|
199 | if (length > 2) {
|
200 | if (x > inputRange[length - 1]) {
|
201 | narrowedInput.leftEdgeInput = inputRange[length - 2];
|
202 | narrowedInput.rightEdgeInput = inputRange[length - 1];
|
203 | narrowedInput.leftEdgeOutput = outputRange[length - 2];
|
204 | narrowedInput.rightEdgeOutput = outputRange[length - 1];
|
205 | } else {
|
206 | for (let i = 1; i < length; ++i) {
|
207 | if (x <= inputRange[i]) {
|
208 | narrowedInput.leftEdgeInput = inputRange[i - 1];
|
209 | narrowedInput.rightEdgeInput = inputRange[i];
|
210 | narrowedInput.leftEdgeOutput = outputRange[i - 1];
|
211 | narrowedInput.rightEdgeOutput = outputRange[i];
|
212 | break;
|
213 | }
|
214 | }
|
215 | }
|
216 | }
|
217 |
|
218 | return internalInterpolate(x, narrowedInput, extrapolationConfig);
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | export function clamp(value: number, min: number, max: number) {
|
231 | 'worklet';
|
232 | return Math.min(Math.max(value, min), max);
|
233 | }
|