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 authored and soto-blvd committed Feb 15, 2022
1 parent da420c9 commit 47232e6
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 2 deletions.
61 changes: 61 additions & 0 deletions Libraries/Animated/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 @@ -48,6 +50,12 @@ export type CompositeAnimation = {
...
};

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 @@ -76,10 +84,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 @@ -576,6 +605,15 @@ module.exports = {
* See https://reactnative.dev/docs/animated.html#timing
*/
timing,

/**
* 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,

/**
* Animates a value according to an analytical spring model based on
* damped harmonic oscillation.
Expand Down Expand Up @@ -616,6 +654,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 @@ -624,6 +677,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/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/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
8 changes: 6 additions & 2 deletions Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
#import <React/RCTAdditionAnimatedNode.h>
#import <React/RCTAnimatedNode.h>
#import <React/RCTAnimationDriver.h>
#import <React/RCTClampAnimatedNode.h>
#import <React/RCTDecayAnimation.h>
#import <React/RCTDiffClampAnimatedNode.h>
#import <React/RCTDivisionAnimatedNode.h>
#import <React/RCTEventAnimation.h>
#import <React/RCTExponentiationAnimatedNode.h>
#import <React/RCTFrameAnimation.h>
#import <React/RCTDecayAnimation.h>
#import <React/RCTInterpolationAnimatedNode.h>
#import <React/RCTModuloAnimatedNode.h>
#import <React/RCTMultiplicationAnimatedNode.h>
#import <React/RCTPropsAnimatedNode.h>
#import <React/RCTSpringAnimation.h>
#import <React/RCTStyleAnimatedNode.h>
#import <React/RCTSubtractionAnimatedNode.h>
#import <React/RCTTrackingAnimatedNode.h>
#import <React/RCTTransformAnimatedNode.h>
#import <React/RCTValueAnimatedNode.h>
#import <React/RCTTrackingAnimatedNode.h>

// We do some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName.
// To make things simpler just get rid of the parts we change in the event names we use here.
Expand Down Expand Up @@ -89,8 +91,10 @@ - (void)createAnimatedNode:(nonnull NSNumber *)tag
@"props" : [RCTPropsAnimatedNode class],
@"interpolation" : [RCTInterpolationAnimatedNode class],
@"addition" : [RCTAdditionAnimatedNode class],
@"clamp": [RCTClampAnimatedNode class],
@"diffclamp": [RCTDiffClampAnimatedNode class],
@"division" : [RCTDivisionAnimatedNode class],
@"exponentiation" : [RCTExponentiationAnimatedNode class],
@"multiplication" : [RCTMultiplicationAnimatedNode class],
@"modulus" : [RCTModuloAnimatedNode class],
@"subtraction" : [RCTSubtractionAnimatedNode class],
Expand Down
Loading

0 comments on commit 47232e6

Please sign in to comment.