Skip to content

Commit

Permalink
[change] update Animated implementation
Browse files Browse the repository at this point in the history
Mirror contents of React Native 0.57.5

Ref #1172
  • Loading branch information
necolas committed Dec 30, 2018
1 parent 596c70f commit cc2652d
Show file tree
Hide file tree
Showing 20 changed files with 278 additions and 143 deletions.
13 changes: 9 additions & 4 deletions packages/react-native-web/src/exports/Animated/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* Copyright (c) 2016-present, Nicolas Gallagher.
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict-local
* @format
*/

import AnimatedImplementation from '../../vendor/react-native/Animated/AnimatedImplementation';
import FlatList from '../FlatList';
import Image from '../Image';
import SectionList from '../SectionList';
import ScrollView from '../ScrollView';
import Text from '../Text';
import View from '../View';

const Animated = {
...AnimatedImplementation,
FlatList: AnimatedImplementation.createAnimatedComponent(FlatList),
Image: AnimatedImplementation.createAnimatedComponent(Image),
ScrollView: AnimatedImplementation.createAnimatedComponent(ScrollView),
View: AnimatedImplementation.createAnimatedComponent(View),
Text: AnimatedImplementation.createAnimatedComponent(Text)
SectionList: AnimatedImplementation.createAnimatedComponent(SectionList),
Text: AnimatedImplementation.createAnimatedComponent(Text),
View: AnimatedImplementation.createAnimatedComponent(View)
};

export default Animated;
2 changes: 1 addition & 1 deletion packages/react-native-web/src/exports/ScrollView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow
*/

import createReactClass from 'create-react-class';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @noflow
* @flow
* @format
*/
'use strict';
Expand Down Expand Up @@ -158,6 +158,7 @@ class AnimatedEvent {
};
}

// $FlowFixMe
_callListeners(...args) {
this._listeners.forEach(listener => listener(...args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import AnimatedModulo from './nodes/AnimatedModulo';
import AnimatedMultiplication from './nodes/AnimatedMultiplication';
import AnimatedNode from './nodes/AnimatedNode';
import AnimatedProps from './nodes/AnimatedProps';
import AnimatedSubtraction from './nodes/AnimatedSubtraction';
import AnimatedTracking from './nodes/AnimatedTracking';
import AnimatedValue from './nodes/AnimatedValue';
import AnimatedValueXY from './nodes/AnimatedValueXY';
Expand Down Expand Up @@ -49,6 +50,13 @@ const add = function(
return new AnimatedAddition(a, b);
};

const subtract = function(
a: AnimatedNode | number,
b: AnimatedNode | number,
): AnimatedSubtraction {
return new AnimatedSubtraction(a, b);
};

const divide = function(
a: AnimatedNode | number,
b: AnimatedNode | number,
Expand Down Expand Up @@ -516,7 +524,7 @@ const AnimatedImplementation = {
/**
* 2D value class for driving 2D animations, such as pan gestures.
*
* See https://facebook.github.io/react-native/releases/next/docs/animatedvaluexy.html
* See https://facebook.github.io/react-native/docs/animatedvaluexy.html
*/
ValueXY: AnimatedValueXY,
/**
Expand Down Expand Up @@ -563,6 +571,14 @@ const AnimatedImplementation = {
*/
add,

/**
* Creates a new Animated value composed by subtracting the second Animated
* value from the first Animated value.
*
* See http://facebook.github.io/react-native/docs/animated.html#subtract
*/
subtract,

/**
* Creates a new Animated value composed by dividing the first Animated value
* by the second Animated value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

import _bezier from './bezier';
Expand Down Expand Up @@ -131,7 +133,7 @@ class Easing {
* http://easings.net/#easeInSine
*/
static sin(t: number) {
return 1 - Math.cos(t * Math.PI / 2);
return 1 - Math.cos((t * Math.PI) / 2);
}

/**
Expand Down Expand Up @@ -164,7 +166,7 @@ class Easing {
*/
static elastic(bounciness: number = 1): (t: number) => number {
const p = bounciness * Math.PI;
return (t) => 1 - Math.pow(Math.cos(t * Math.PI / 2), 3) * Math.cos(t * p);
return t => 1 - Math.pow(Math.cos((t * Math.PI) / 2), 3) * Math.cos(t * p);
}

/**
Expand All @@ -179,7 +181,7 @@ class Easing {
if (s === undefined) {
s = 1.70158;
}
return (t) => t * t * ((s + 1) * t - s);
return t => t * t * ((s + 1) * t - s);
}

/**
Expand Down Expand Up @@ -217,38 +219,32 @@ class Easing {
x1: number,
y1: number,
x2: number,
y2: number
y2: number,
): (t: number) => number {
return _bezier(x1, y1, x2, y2);
}

/**
* Runs an easing function forwards.
*/
static in(
easing: (t: number) => number,
): (t: number) => number {
static in(easing: (t: number) => number): (t: number) => number {
return easing;
}

/**
* Runs an easing function backwards.
*/
static out(
easing: (t: number) => number,
): (t: number) => number {
return (t) => 1 - easing(1 - t);
static out(easing: (t: number) => number): (t: number) => number {
return t => 1 - easing(1 - t);
}

/**
* Makes any easing function symmetrical. The easing function will run
* forwards for half of the duration, then backwards for the rest of the
* duration.
*/
static inOut(
easing: (t: number) => number,
): (t: number) => number {
return (t) => {
static inOut(easing: (t: number) => number): (t: number) => number {
return t => {
if (t < 0.5) {
return easing(t * 2) / 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ const API = {
const STYLES_WHITELIST = {
opacity: true,
transform: true,
borderRadius: true,
borderBottomEndRadius: true,
borderBottomLeftRadius: true,
borderBottomRightRadius: true,
borderBottomStartRadius: true,
borderTopEndRadius: true,
borderTopLeftRadius: true,
borderTopRightRadius: true,
borderTopStartRadius: true,
/* ios styles */
shadowOpacity: true,
shadowRadius: true,
Expand Down Expand Up @@ -200,7 +209,7 @@ function validateTransform(configs: Array<Object>): void {
}

function validateStyles(styles: Object): void {
for (var key in styles) {
for (const key in styles) {
if (!STYLES_WHITELIST.hasOwnProperty(key)) {
throw new Error(
`Style property '${key}' is not supported by native animated module`,
Expand All @@ -210,7 +219,7 @@ function validateStyles(styles: Object): void {
}

function validateInterpolation(config: Object): void {
for (var key in config) {
for (const key in config) {
if (!SUPPORTED_INTERPOLATION_PARAMS.hasOwnProperty(key)) {
throw new Error(
`Interpolation property '${key}' is not supported by native animated module`,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @flow strict
*/

'use strict';
Expand Down Expand Up @@ -41,7 +42,7 @@ function fromBouncinessAndSpeed(
}

function projectNormal(n, start, end) {
return start + (n * (end - start));
return start + n * (end - start);
}

function linearInterpolation(t, start, end) {
Expand All @@ -53,18 +54,20 @@ function fromBouncinessAndSpeed(
}

function b3Friction1(x) {
return (0.0007 * Math.pow(x, 3)) -
(0.031 * Math.pow(x, 2)) + 0.64 * x + 1.28;
return 0.0007 * Math.pow(x, 3) - 0.031 * Math.pow(x, 2) + 0.64 * x + 1.28;
}

function b3Friction2(x) {
return (0.000044 * Math.pow(x, 3)) -
(0.006 * Math.pow(x, 2)) + 0.36 * x + 2;
return 0.000044 * Math.pow(x, 3) - 0.006 * Math.pow(x, 2) + 0.36 * x + 2;
}

function b3Friction3(x) {
return (0.00000045 * Math.pow(x, 3)) -
(0.000332 * Math.pow(x, 2)) + 0.1078 * x + 5.84;
return (
0.00000045 * Math.pow(x, 3) -
0.000332 * Math.pow(x, 2) +
0.1078 * x +
5.84
);
}

function b3Nobounce(tension) {
Expand All @@ -77,14 +80,14 @@ function fromBouncinessAndSpeed(
}
}

var b = normalize(bounciness / 1.7, 0, 20);
let b = normalize(bounciness / 1.7, 0, 20);
b = projectNormal(b, 0, 0.8);
var s = normalize(speed / 1.7, 0, 20);
var bouncyTension = projectNormal(s, 0.5, 200);
var bouncyFriction = quadraticOutInterpolation(
const s = normalize(speed / 1.7, 0, 20);
const bouncyTension = projectNormal(s, 0.5, 200);
const bouncyFriction = quadraticOutInterpolation(
b,
b3Nobounce(bouncyTension),
0.01
0.01,
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ class SpringAnimation extends Animation {
position =
this._toValue -
envelope *
((v0 + zeta * omega0 * x0) / omega1 * Math.sin(omega1 * t) +
(((v0 + zeta * omega0 * x0) / omega1) * Math.sin(omega1 * t) +
x0 * Math.cos(omega1 * t));
// This looks crazy -- it's actually just the derivative of the
// oscillation function
velocity =
zeta *
omega0 *
envelope *
(Math.sin(omega1 * t) * (v0 + zeta * omega0 * x0) / omega1 +
((Math.sin(omega1 * t) * (v0 + zeta * omega0 * x0)) / omega1 +
x0 * Math.cos(omega1 * t)) -
envelope *
(Math.cos(omega1 * t) * (v0 + zeta * omega0 * x0) -
Expand Down
Loading

0 comments on commit cc2652d

Please sign in to comment.