Skip to content

Commit 0ce0d96

Browse files
javachefacebook-github-bot
authored andcommitted
Fix normalization of degrees in AnimatedInterpolation (facebook#36645)
Summary: Pull Request resolved: facebook#36645 This broke while changing the AnimatedInterpolation back in D40571873 and D40632443, as I assumed the native side would be able to correctly handle values such as '1rad'. However these were being sent over as strings, and were thus using the string interpolation path, which does not work here. Instead, handle both `deg` and `rad` explicitly when generating the config in JS. Resolves issue facebook#36608 Changelog: [General][Fixed] Resolves Animated.Value.interpolate results in NaN when output is in radians Reviewed By: yungsters Differential Revision: D44406034 fbshipit-source-id: c821d573d69b3a2111cd846a94d8a00b1b9227ed
1 parent 04df252 commit 0ce0d96

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/react-native/Libraries/Animated/NativeAnimatedHelper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,13 @@ function transformDataType(value: number | string): number | string {
562562
if (typeof value !== 'string') {
563563
return value;
564564
}
565-
if (/deg$/.test(value)) {
565+
566+
// Normalize degrees and radians to a number expressed in radians
567+
if (value.endsWith('deg')) {
566568
const degrees = parseFloat(value) || 0;
567-
const radians = (degrees * Math.PI) / 180.0;
568-
return radians;
569+
return (degrees * Math.PI) / 180.0;
570+
} else if (value.endsWith('rad')) {
571+
return parseFloat(value) || 0;
569572
} else {
570573
return value;
571574
}

packages/react-native/Libraries/Animated/__tests__/Interpolation-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,20 @@ describe('Interpolation', () => {
349349
expect(interpolation(1e-12)).toBe('rgba(0, 0, 0, 0)');
350350
expect(interpolation(2 / 3)).toBe('rgba(0, 0, 0, 0.667)');
351351
});
352+
353+
it.each([
354+
['radians', ['1rad', '2rad'], [1, 2]],
355+
['degrees', ['90deg', '180deg'], [Math.PI / 2, Math.PI]],
356+
['numbers', [1024, Math.PI], [1024, Math.PI]],
357+
['unknown', ['5foo', '10foo'], ['5foo', '10foo']],
358+
])(
359+
'should convert %s to numbers in the native config',
360+
(_, outputRange, expected) => {
361+
const config = new AnimatedInterpolation(
362+
{},
363+
{inputRange: [0, 1], outputRange},
364+
).__getNativeConfig();
365+
expect(config.outputRange).toEqual(expected);
366+
},
367+
);
352368
});

0 commit comments

Comments
 (0)