From 24417941794557b9865d9a60a4ae21de37d6393a Mon Sep 17 00:00:00 2001 From: rocman Date: Mon, 23 May 2016 20:13:45 +0800 Subject: [PATCH 1/3] add a mapping ability to the interpolate method --- Libraries/Animated/src/Interpolation.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Libraries/Animated/src/Interpolation.js b/Libraries/Animated/src/Interpolation.js index 32d363ccbbed51..d1a6a0ddb14031 100644 --- a/Libraries/Animated/src/Interpolation.js +++ b/Libraries/Animated/src/Interpolation.js @@ -75,6 +75,7 @@ class Interpolation { ); var range = findRange(input, inputRange); + var mapping = config.mapping || passThrough; return interpolate( input, inputRange[range], @@ -84,11 +85,16 @@ class Interpolation { easing, extrapolateLeft, extrapolateRight, + mapping, ); }; } } +function passThrough(input: any): any { + return input; +} + function interpolate( input: number, inputMin: number, @@ -98,13 +104,14 @@ function interpolate( easing: ((input: number) => number), extrapolateLeft: ExtrapolateType, extrapolateRight: ExtrapolateType, + mapping: ((input: number) => any) ) { var result = input; // Extrapolate if (result < inputMin) { if (extrapolateLeft === 'identity') { - return result; + return mapping(result); } else if (extrapolateLeft === 'clamp') { result = inputMin; } else if (extrapolateLeft === 'extend') { @@ -114,7 +121,7 @@ function interpolate( if (result > inputMax) { if (extrapolateRight === 'identity') { - return result; + return mapping(result); } else if (extrapolateRight === 'clamp') { result = inputMax; } else if (extrapolateRight === 'extend') { @@ -123,14 +130,14 @@ function interpolate( } if (outputMin === outputMax) { - return outputMin; + return mapping(outputMin); } if (inputMin === inputMax) { if (input <= inputMin) { - return outputMin; + return mapping(outputMin); } - return outputMax; + return mapping(outputMin); } // Input Range @@ -154,7 +161,7 @@ function interpolate( result = result * (outputMax - outputMin) + outputMin; } - return result; + return mapping(result); } function colorToRgba(input: string): string { @@ -233,7 +240,7 @@ function createInterpolationFromStringOutputRange( // 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...' return outputRange[0].replace(stringShapeRegex, () => { const val = +interpolations[i++](input); - const rounded = shouldRound && i < 4 ? Math.round(val) : Math.round(val * 1000) / 1000; + const rounded = shouldRound && i < 4 ? Math.round(val) : val; return String(rounded); }); }; From 06885230a962b6a8ef93000d6526e1d74757ea5a Mon Sep 17 00:00:00 2001 From: rocman Date: Mon, 23 May 2016 20:16:19 +0800 Subject: [PATCH 2/3] rollback one line --- Libraries/Animated/src/Interpolation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Animated/src/Interpolation.js b/Libraries/Animated/src/Interpolation.js index d1a6a0ddb14031..d8c8d66e0fee67 100644 --- a/Libraries/Animated/src/Interpolation.js +++ b/Libraries/Animated/src/Interpolation.js @@ -240,7 +240,7 @@ function createInterpolationFromStringOutputRange( // 'rgba(${interpolations[0](input)}, ${interpolations[1](input)}, ...' return outputRange[0].replace(stringShapeRegex, () => { const val = +interpolations[i++](input); - const rounded = shouldRound && i < 4 ? Math.round(val) : val; + const rounded = shouldRound && i < 4 ? Math.round(val) : Math.round(val * 1000) / 1000; return String(rounded); }); }; From 78e47b4981aa8f189fcb83dd1be7e5266e10274a Mon Sep 17 00:00:00 2001 From: rocman Date: Wed, 25 May 2016 09:49:55 +0800 Subject: [PATCH 3/3] mapping(outputMin) -> mapping(outputMax) --- Libraries/Animated/src/Interpolation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Animated/src/Interpolation.js b/Libraries/Animated/src/Interpolation.js index d8c8d66e0fee67..1539f309f4c327 100644 --- a/Libraries/Animated/src/Interpolation.js +++ b/Libraries/Animated/src/Interpolation.js @@ -137,7 +137,7 @@ function interpolate( if (input <= inputMin) { return mapping(outputMin); } - return mapping(outputMin); + return mapping(outputMax); } // Input Range