mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
4a227ce2ab
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/32736 The ability to pass an additional property bag to further configure NativeAnimated is useful for a few reasons, e.g., experimentation with multiple implementations of the NativeAnimated module. The specific use case this solves is on react-native-windows, where there are two underlying animation graph options, one "optimized" animation graph that uses UI.Composition, and another similar to the approach to iOS and Android that uses a frame rendering callback. The platform configuration can be supplied to the animation node when `useNativeDriver` is set to `true`, and we pass the platform configuration object to the connected AnimatedValue and all it's children. Changelog: [General][Added] - Option to supply `platformConfig` to NativeAnimated Reviewed By: yungsters Differential Revision: D32988285 fbshipit-source-id: ab8a7bbf197573fc9e9a4737c255f124321295ac
128 lines
3.2 KiB
JavaScript
128 lines
3.2 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const Animation = require('./Animation');
|
|
|
|
const {shouldUseNativeDriver} = require('../NativeAnimatedHelper');
|
|
|
|
import type {PlatformConfig} from '../AnimatedPlatformConfig';
|
|
import type AnimatedValue from '../nodes/AnimatedValue';
|
|
import type {AnimationConfig, EndCallback} from './Animation';
|
|
|
|
export type DecayAnimationConfig = {
|
|
...AnimationConfig,
|
|
velocity:
|
|
| number
|
|
| {
|
|
x: number,
|
|
y: number,
|
|
...
|
|
},
|
|
deceleration?: number,
|
|
};
|
|
|
|
export type DecayAnimationConfigSingle = {
|
|
...AnimationConfig,
|
|
velocity: number,
|
|
deceleration?: number,
|
|
};
|
|
|
|
class DecayAnimation extends Animation {
|
|
_startTime: number;
|
|
_lastValue: number;
|
|
_fromValue: number;
|
|
_deceleration: number;
|
|
_velocity: number;
|
|
_onUpdate: (value: number) => void;
|
|
_animationFrame: any;
|
|
_useNativeDriver: boolean;
|
|
_platformConfig: ?PlatformConfig;
|
|
|
|
constructor(config: DecayAnimationConfigSingle) {
|
|
super();
|
|
this._deceleration = config.deceleration ?? 0.998;
|
|
this._velocity = config.velocity;
|
|
this._useNativeDriver = shouldUseNativeDriver(config);
|
|
this._platformConfig = config.platformConfig;
|
|
this.__isInteraction = config.isInteraction ?? !this._useNativeDriver;
|
|
this.__iterations = config.iterations ?? 1;
|
|
}
|
|
|
|
__getNativeAnimationConfig(): {|
|
|
deceleration: number,
|
|
iterations: number,
|
|
platformConfig: ?PlatformConfig,
|
|
type: $TEMPORARY$string<'decay'>,
|
|
velocity: number,
|
|
|} {
|
|
return {
|
|
type: 'decay',
|
|
deceleration: this._deceleration,
|
|
velocity: this._velocity,
|
|
iterations: this.__iterations,
|
|
platformConfig: this._platformConfig,
|
|
};
|
|
}
|
|
|
|
start(
|
|
fromValue: number,
|
|
onUpdate: (value: number) => void,
|
|
onEnd: ?EndCallback,
|
|
previousAnimation: ?Animation,
|
|
animatedValue: AnimatedValue,
|
|
): void {
|
|
this.__active = true;
|
|
this._lastValue = fromValue;
|
|
this._fromValue = fromValue;
|
|
this._onUpdate = onUpdate;
|
|
this.__onEnd = onEnd;
|
|
this._startTime = Date.now();
|
|
if (this._useNativeDriver) {
|
|
this.__startNativeAnimation(animatedValue);
|
|
} else {
|
|
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this));
|
|
}
|
|
}
|
|
|
|
onUpdate(): void {
|
|
const now = Date.now();
|
|
|
|
const value =
|
|
this._fromValue +
|
|
(this._velocity / (1 - this._deceleration)) *
|
|
(1 - Math.exp(-(1 - this._deceleration) * (now - this._startTime)));
|
|
|
|
this._onUpdate(value);
|
|
|
|
if (Math.abs(this._lastValue - value) < 0.1) {
|
|
this.__debouncedOnEnd({finished: true});
|
|
return;
|
|
}
|
|
|
|
this._lastValue = value;
|
|
if (this.__active) {
|
|
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
this._animationFrame = requestAnimationFrame(this.onUpdate.bind(this));
|
|
}
|
|
}
|
|
|
|
stop(): void {
|
|
super.stop();
|
|
this.__active = false;
|
|
global.cancelAnimationFrame(this._animationFrame);
|
|
this.__debouncedOnEnd({finished: false});
|
|
}
|
|
}
|
|
|
|
module.exports = DecayAnimation;
|