Skip to content

Commit

Permalink
Add more Animated node types
Browse files Browse the repository at this point in the history
  • Loading branch information
lnikkila committed Apr 21, 2019
1 parent 822bdd4 commit f854b6a
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 6 deletions.
60 changes: 60 additions & 0 deletions Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

const {AnimatedEvent, attachNativeEvent} = require('./AnimatedEvent');
const AnimatedAddition = require('./nodes/AnimatedAddition');
const AnimatedClamp = require('./nodes/AnimatedClamp');
const AnimatedDiffClamp = require('./nodes/AnimatedDiffClamp');
const AnimatedDivision = require('./nodes/AnimatedDivision');
const AnimatedExponentiation = require('./nodes/AnimatedExponentiation');
const AnimatedInterpolation = require('./nodes/AnimatedInterpolation');
const AnimatedModulo = require('./nodes/AnimatedModulo');
const AnimatedMultiplication = require('./nodes/AnimatedMultiplication');
Expand Down Expand Up @@ -47,6 +49,12 @@ export type CompositeAnimation = {
_isUsingNativeDriver: () => boolean,
};

const abs = function(
a: AnimatedNode | number
): AnimatedAddition {
return new AnimatedExponentiation(AnimatedExponentiation(a, 2), 0.5);
};

const add = function(
a: AnimatedNode | number,
b: AnimatedNode | number,
Expand Down Expand Up @@ -75,10 +83,31 @@ const multiply = function(
return new AnimatedMultiplication(a, b);
};

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

const sqrt = function(
a: AnimatedNode | number
): AnimatedExponentiation {
return new AnimatedExponentiation(a, 0.5);
};

const modulo = function(a: AnimatedNode, modulus: number): AnimatedModulo {
return new AnimatedModulo(a, modulus);
};

const clamp = function(
a: AnimatedNode,
min: AnimatedNode | number,
max: AnimatedNode | number,
): AnimatedClamp {
return new AnimatedClamp(a, min, max);
};

const diffClamp = function(
a: AnimatedNode,
min: number,
Expand Down Expand Up @@ -569,6 +598,14 @@ module.exports = {
*/
spring,

/**
* Creates a new Animated value by taking the absolute value of another
* Animated value.
*
* See http://facebook.github.io/react-native/docs/animated.html#abs
*/
abs,

/**
* Creates a new Animated value composed from two Animated values added
* together.
Expand Down Expand Up @@ -601,6 +638,21 @@ module.exports = {
*/
multiply,

/**
* Creates a new Animated value by raising the first value to the power of
* the second value.
*
* See http://facebook.github.io/react-native/docs/animated.html#pow
*/
pow,

/**
* Creates a new Animated value by taking the square root of the given value.
*
* See http://facebook.github.io/react-native/docs/animated.html#sqrt
*/
sqrt,

/**
* Creates a new Animated value that is the (non-negative) modulo of the
* provided Animated value.
Expand All @@ -609,6 +661,14 @@ module.exports = {
*/
modulo,

/**
* Create a new Animated value that is limited between minimum and
* maximum values.
*
* See http://facebook.github.io/react-native/docs/animated.html#clamp
*/
clamp,

/**
* Create a new Animated value that is limited between 2 values. It uses the
* difference between the last value so even if the value is far from the
Expand Down
73 changes: 73 additions & 0 deletions Libraries/Animated/src/nodes/AnimatedClamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule AnimatedClamp
* @flow
* @format
*/
'use strict';

const AnimatedInterpolation = require('./AnimatedInterpolation');
const AnimatedNode = require('./AnimatedNode');
const AnimatedValue = require('./AnimatedValue');
const AnimatedWithChildren = require('./AnimatedWithChildren');

import type {InterpolationConfigType} from './AnimatedInterpolation';

class AnimatedClamp extends AnimatedWithChildren {
_a: AnimatedNode;
_min: AnimatedNode;
_max: AnimatedNode;

constructor(a: AnimatedNode, min: AnimatedNode | number, max: AnimatedNode | number) {
super();

this._a = a;
this._min = typeof min === 'number' ? new AnimatedValue(min) : min;
this._max = typeof max === 'number' ? new AnimatedValue(max) : max;
}

__makeNative() {
this._a.__makeNative();
this._min.__makeNative();
this._max.__makeNative();
super.__makeNative();
}

interpolate(config: InterpolationConfigType): AnimatedInterpolation {
return new AnimatedInterpolation(this, config);
}

__getValue(): number {
return Math.min(Math.max(this._a.__getValue(), this._min.__getValue()), this._max.__getValue());
}

__attach(): void {
this._a.__addChild(this);
this._min.__addChild(this);
this._max.__addChild(this);
}

__detach(): void {
this._a.__removeChild(this);
this._min.__removeChild(this);
this._max.__removeChild(this);
super.__detach();
}

__getNativeConfig(): any {
return {
type: 'clamp',
input: this._a.__getNativeTag(),
min: this._min.__getNativeTag(),
max: this._max.__getNativeTag(),
};
}
}

module.exports = AnimatedClamp;
65 changes: 65 additions & 0 deletions Libraries/Animated/src/nodes/AnimatedExponentiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule AnimatedExponentiation
* @flow
* @format
*/
'use strict';

const AnimatedInterpolation = require('./AnimatedInterpolation');
const AnimatedNode = require('./AnimatedNode');
const AnimatedValue = require('./AnimatedValue');
const AnimatedWithChildren = require('./AnimatedWithChildren');

import type {InterpolationConfigType} from './AnimatedInterpolation';

class AnimatedExponentiation extends AnimatedWithChildren {
_a: AnimatedNode;
_b: AnimatedNode;

constructor(a: AnimatedNode | number, b: AnimatedNode | number) {
super();
this._a = typeof a === 'number' ? new AnimatedValue(a) : a;
this._b = typeof b === 'number' ? new AnimatedValue(b) : b;
}

__makeNative() {
this._a.__makeNative();
this._b.__makeNative();
super.__makeNative();
}

__getValue(): number {
return Math.pow(this._a.__getValue(), this._b.__getValue());
}

interpolate(config: InterpolationConfigType): AnimatedInterpolation {
return new AnimatedInterpolation(this, config);
}

__attach(): void {
this._a.__addChild(this);
this._b.__addChild(this);
}

__detach(): void {
this._a.__removeChild(this);
this._b.__removeChild(this);
super.__detach();
}

__getNativeConfig(): any {
return {
type: 'exponentiation',
input: [this._a.__getNativeTag(), this._b.__getNativeTag()],
};
}
}

module.exports = AnimatedExponentiation;
12 changes: 12 additions & 0 deletions Libraries/NativeAnimation/Nodes/RCTClampAnimatedNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

#import "RCTValueAnimatedNode.h"

@interface RCTClampAnimatedNode : RCTValueAnimatedNode

@end
46 changes: 46 additions & 0 deletions Libraries/NativeAnimation/Nodes/RCTClampAnimatedNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "RCTClampAnimatedNode.h"

@implementation RCTClampAnimatedNode
{
NSNumber *_inputNodeTag;
NSNumber *_minNodeTag;
NSNumber *_maxNodeTag;
}

- (instancetype)initWithTag:(NSNumber *)tag
config:(NSDictionary<NSString *, id> *)config
{
if (self = [super initWithTag:tag config:config]) {
_inputNodeTag = config[@"input"];
_minNodeTag = config[@"min"];
_maxNodeTag = config[@"max"];
}

return self;
}

- (void)performUpdate
{
[super performUpdate];

RCTValueAnimatedNode *input = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:_inputNodeTag];
RCTValueAnimatedNode *min = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:_minNodeTag];
RCTValueAnimatedNode *max = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:_maxNodeTag];

if ([input isKindOfClass:[RCTValueAnimatedNode class]] &&
[min isKindOfClass:[RCTValueAnimatedNode class]] &&
[max isKindOfClass:[RCTValueAnimatedNode class]]) {
self.value = MIN(MAX(input.value, min.value), max.value);
}
}

@end
14 changes: 14 additions & 0 deletions Libraries/NativeAnimation/Nodes/RCTExponentiationAnimatedNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "RCTValueAnimatedNode.h"

@interface RCTExponentiationAnimatedNode : RCTValueAnimatedNode

@end
29 changes: 29 additions & 0 deletions Libraries/NativeAnimation/Nodes/RCTExponentiationAnimatedNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import "RCTExponentiationAnimatedNode.h"

@implementation RCTExponentiationAnimatedNode

- (void)performUpdate
{
[super performUpdate];

NSArray<NSNumber *> *inputNodes = self.config[@"input"];
if (inputNodes.count > 1) {
RCTValueAnimatedNode *parent1 = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:inputNodes[0]];
RCTValueAnimatedNode *parent2 = (RCTValueAnimatedNode *)[self.parentNodes objectForKey:inputNodes[1]];
if ([parent1 isKindOfClass:[RCTValueAnimatedNode class]] &&
[parent2 isKindOfClass:[RCTValueAnimatedNode class]]) {
self.value = pow(parent1.value, parent2.value);
}
}
}

@end
Loading

0 comments on commit f854b6a

Please sign in to comment.