react-native-reanimated 2.3.0-alpha.1 → 2.3.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +15 -8
- package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +170 -166
- package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +36 -34
- package/Common/cpp/Registries/EventHandlerRegistry.cpp +13 -6
- package/Common/cpp/Registries/MapperRegistry.cpp +19 -15
- package/Common/cpp/Registries/WorkletsCache.cpp +19 -9
- package/Common/cpp/SharedItems/FrozenObject.cpp +19 -8
- package/Common/cpp/SharedItems/MutableValue.cpp +38 -25
- package/Common/cpp/SharedItems/MutableValueSetterProxy.cpp +11 -7
- package/Common/cpp/SharedItems/RemoteObject.cpp +9 -5
- package/Common/cpp/SharedItems/ShareableValue.cpp +216 -143
- package/Common/cpp/Tools/JSIStoreValueUser.cpp +36 -24
- package/Common/cpp/Tools/Mapper.cpp +41 -29
- package/Common/cpp/Tools/RuntimeDecorator.cpp +103 -84
- package/Common/cpp/Tools/Scheduler.cpp +14 -6
- package/Common/cpp/Tools/WorkletEventHandler.cpp +1 -1
- package/Common/cpp/headers/LayoutAnimations/LayoutAnimationsProxy.h +12 -11
- package/Common/cpp/headers/NativeModules/NativeReanimatedModule.h +45 -34
- package/Common/cpp/headers/NativeModules/NativeReanimatedModuleSpec.h +28 -16
- package/Common/cpp/headers/Registries/EventHandlerRegistry.h +14 -8
- package/Common/cpp/headers/Registries/MapperRegistry.h +4 -4
- package/Common/cpp/headers/Registries/WorkletsCache.h +9 -7
- package/Common/cpp/headers/SharedItems/FrozenObject.h +15 -11
- package/Common/cpp/headers/SharedItems/HostFunctionHandler.h +6 -4
- package/Common/cpp/headers/SharedItems/MutableValue.h +21 -12
- package/Common/cpp/headers/SharedItems/MutableValueSetterProxy.h +10 -7
- package/Common/cpp/headers/SharedItems/RemoteObject.h +16 -8
- package/Common/cpp/headers/SharedItems/RuntimeManager.h +39 -21
- package/Common/cpp/headers/SharedItems/ShareableValue.h +31 -27
- package/Common/cpp/headers/SharedItems/SharedParent.h +7 -4
- package/Common/cpp/headers/SharedItems/ValueWrapper.h +80 -61
- package/Common/cpp/headers/SpecTools/ErrorHandler.h +17 -19
- package/Common/cpp/headers/Tools/JSIStoreValueUser.h +18 -13
- package/Common/cpp/headers/Tools/Mapper.h +24 -18
- package/Common/cpp/headers/Tools/PlatformDepMethodsHolder.h +12 -7
- package/Common/cpp/headers/Tools/ReanimatedHiddenHeaders.h +7 -7
- package/Common/cpp/headers/Tools/RuntimeDecorator.h +29 -23
- package/Common/cpp/headers/Tools/Scheduler.h +28 -33
- package/Common/cpp/headers/Tools/WorkletEventHandler.h +9 -7
- package/Common/cpp/hidden_headers/Logger.h +14 -14
- package/Common/cpp/hidden_headers/LoggerInterface.h +8 -9
- package/Common/cpp/hidden_headers/SpeedChecker.h +7 -8
- package/README.md +1 -1
- package/RNReanimated.podspec +4 -4
- package/android/build.gradle +80 -3
- package/android/react-native-reanimated-63-hermes.aar +0 -0
- package/android/react-native-reanimated-63-jsc.aar +0 -0
- package/android/react-native-reanimated-64-hermes.aar +0 -0
- package/android/react-native-reanimated-64-jsc.aar +0 -0
- package/android/react-native-reanimated-65-hermes.aar +0 -0
- package/android/react-native-reanimated-65-jsc.aar +0 -0
- package/android/react-native-reanimated-66-hermes.aar +0 -0
- package/android/react-native-reanimated-66-jsc.aar +0 -0
- package/ios/LayoutReanimation/REAAnimationsManager.h +19 -10
- package/ios/LayoutReanimation/REAAnimationsManager.m +195 -200
- package/ios/LayoutReanimation/REASnapshot.h +13 -0
- package/ios/LayoutReanimation/REASnapshot.m +29 -0
- package/ios/LayoutReanimation/REAUIManager.h +16 -0
- package/ios/LayoutReanimation/REAUIManager.mm +348 -0
- package/ios/Nodes/REAAlwaysNode.m +14 -15
- package/ios/Nodes/REABezierNode.m +8 -7
- package/ios/Nodes/REABlockNode.m +1 -1
- package/ios/Nodes/REACallFuncNode.h +0 -1
- package/ios/Nodes/REACallFuncNode.m +10 -8
- package/ios/Nodes/REAClockNodes.m +22 -20
- package/ios/Nodes/REAConcatNode.h +0 -1
- package/ios/Nodes/REAConcatNode.m +2 -2
- package/ios/Nodes/REACondNode.m +7 -5
- package/ios/Nodes/READebugNode.m +5 -4
- package/ios/Nodes/REAEventNode.m +1 -1
- package/ios/Nodes/REAFunctionNode.h +0 -1
- package/ios/Nodes/REAFunctionNode.m +2 -2
- package/ios/Nodes/REAJSCallNode.m +4 -5
- package/ios/Nodes/REANode.h +3 -4
- package/ios/Nodes/REANode.m +9 -11
- package/ios/Nodes/REAOperatorNode.m +68 -67
- package/ios/Nodes/REAParamNode.h +1 -3
- package/ios/Nodes/REAParamNode.m +15 -17
- package/ios/Nodes/REAPropsNode.h +1 -3
- package/ios/Nodes/REAPropsNode.m +19 -22
- package/ios/Nodes/REASetNode.h +0 -1
- package/ios/Nodes/REASetNode.m +7 -5
- package/ios/Nodes/REAStyleNode.h +0 -1
- package/ios/Nodes/REAStyleNode.m +2 -4
- package/ios/Nodes/REATransformNode.h +0 -1
- package/ios/Nodes/REATransformNode.m +3 -5
- package/ios/Nodes/REAValueNode.m +5 -7
- package/ios/REAEventDispatcher.m +3 -3
- package/ios/REAModule.h +0 -2
- package/ios/REAModule.m +29 -40
- package/ios/REANodesManager.h +11 -19
- package/ios/REANodesManager.m +105 -130
- package/ios/REAUtils.h +5 -3
- package/ios/Transitioning/RCTConvert+REATransition.m +45 -29
- package/ios/Transitioning/REAAllTransitions.h +2 -2
- package/ios/Transitioning/REAAllTransitions.m +14 -25
- package/ios/Transitioning/REATransition.h +8 -7
- package/ios/Transitioning/REATransition.m +19 -16
- package/ios/Transitioning/REATransitionAnimation.h +1 -1
- package/ios/Transitioning/REATransitionAnimation.m +2 -3
- package/ios/Transitioning/REATransitionManager.m +3 -3
- package/ios/native/NativeMethods.h +15 -5
- package/ios/native/NativeProxy.h +4 -3
- package/ios/native/NativeProxy.mm +19 -5
- package/ios/native/REAIOSErrorHandler.h +13 -10
- package/ios/native/REAIOSLogger.h +8 -8
- package/ios/native/REAIOSScheduler.h +5 -5
- package/ios/native/REAIOSScheduler.mm +7 -5
- package/ios/native/REAInitializer.h +37 -0
- package/ios/native/REAInitializer.mm +63 -0
- package/ios/native/UIResponder+Reanimated.mm +9 -48
- package/lib/ConfigHelper.js +1 -0
- package/lib/ReanimatedModule.native.js +10 -2
- package/lib/createAnimatedComponent.js +64 -30
- package/lib/reanimated1/core/AnimatedAlways.js +1 -1
- package/lib/reanimated1/core/AnimatedBezier.js +1 -1
- package/lib/reanimated1/core/AnimatedBlock.js +1 -1
- package/lib/reanimated1/core/AnimatedCall.js +1 -1
- package/lib/reanimated1/core/AnimatedCallFunc.js +1 -1
- package/lib/reanimated1/core/AnimatedConcat.js +1 -1
- package/lib/reanimated1/core/AnimatedCond.js +1 -1
- package/lib/reanimated1/core/AnimatedDebug.js +1 -1
- package/lib/reanimated1/core/AnimatedEvent.js +1 -1
- package/lib/reanimated1/core/AnimatedFunction.js +1 -1
- package/lib/reanimated1/core/AnimatedOperator.js +1 -1
- package/lib/reanimated1/core/AnimatedParam.js +1 -1
- package/lib/reanimated1/core/AnimatedProps.js +2 -2
- package/lib/reanimated1/core/AnimatedSet.js +1 -1
- package/lib/reanimated1/core/AnimatedStartClock.js +1 -1
- package/lib/reanimated1/core/AnimatedStopClock.js +1 -1
- package/lib/reanimated1/core/AnimatedStyle.js +1 -1
- package/lib/reanimated1/core/AnimatedTransform.js +1 -1
- package/lib/reanimated1/core/Core.test.js +30 -0
- package/lib/reanimated1/core/InternalAnimatedValue.js +1 -1
- package/lib/reanimated1/core/__mocks__/AnimatedProps.js +1 -1
- package/lib/reanimated1/derived/interpolate.js +1 -1
- package/lib/reanimated2/Colors.js +16 -12
- package/lib/reanimated2/NativeMethods.js +3 -14
- package/lib/reanimated2/{NativeReanimated.native.js → NativeReanimated/NativeReanimated.js} +19 -24
- package/lib/reanimated2/NativeReanimated/index.js +15 -0
- package/lib/reanimated2/PlatformChecker.js +16 -0
- package/lib/reanimated2/PropAdapters.js +14 -4
- package/lib/reanimated2/UpdateProps.js +14 -11
- package/lib/reanimated2/ViewDescriptorsSet.js +73 -0
- package/lib/reanimated2/WorkletEventHandler.js +15 -13
- package/lib/reanimated2/animation/commonTypes.js +1 -0
- package/lib/reanimated2/animation/decay.js +71 -0
- package/lib/reanimated2/animation/delay.js +64 -0
- package/lib/reanimated2/animation/index.js +8 -0
- package/lib/reanimated2/animation/repeat.js +69 -0
- package/lib/reanimated2/animation/sequence.js +72 -0
- package/lib/reanimated2/animation/spring.js +101 -0
- package/lib/reanimated2/animation/styleAnimation.js +166 -0
- package/lib/reanimated2/animation/timing.js +64 -0
- package/lib/reanimated2/animation/util.js +157 -0
- package/lib/reanimated2/commonTypes.js +1 -0
- package/lib/reanimated2/core.js +35 -39
- package/lib/reanimated2/globals.d.ts +47 -1
- package/lib/reanimated2/hook/Hooks.js +9 -0
- package/lib/reanimated2/hook/commonTypes.js +1 -0
- package/lib/reanimated2/hook/index.js +8 -0
- package/lib/reanimated2/hook/useAnimatedGestureHandler.js +50 -0
- package/lib/reanimated2/hook/useAnimatedReaction.js +37 -0
- package/lib/reanimated2/hook/useAnimatedRef.js +25 -0
- package/lib/reanimated2/hook/useAnimatedScrollHandler.js +41 -0
- package/lib/reanimated2/hook/useAnimatedStyle.js +376 -0
- package/lib/reanimated2/hook/useDerivedValue.js +36 -0
- package/lib/reanimated2/hook/useSharedValue.js +17 -0
- package/lib/reanimated2/hook/utils.js +175 -0
- package/lib/reanimated2/index.js +3 -2
- package/lib/reanimated2/interpolation.js +95 -86
- package/lib/reanimated2/jestUtils.js +0 -12
- package/lib/reanimated2/js-reanimated/JSReanimated.js +9 -10
- package/lib/reanimated2/js-reanimated/index.js +57 -1
- package/lib/reanimated2/layoutReanimation/LayoutAnimationRepository.js +58 -0
- package/lib/reanimated2/layoutReanimation/animationBuilder/BaseAnimationBuilder.js +69 -0
- package/lib/reanimated2/layoutReanimation/animationBuilder/ComplexAnimationBuilder.js +125 -0
- package/lib/reanimated2/layoutReanimation/animationBuilder/Keyframe.js +193 -0
- package/lib/reanimated2/layoutReanimation/animationBuilder/commonTypes.js +1 -0
- package/lib/reanimated2/layoutReanimation/animationBuilder/index.js +3 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Bounce.js +325 -207
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Default.js +36 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Fade.js +235 -187
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Flip.js +327 -269
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.js +146 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Pinwheel.js +85 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Roll.js +119 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Rotate.js +289 -0
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Slide.js +159 -121
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Stretch.js +83 -65
- package/lib/reanimated2/layoutReanimation/defaultAnimations/Zoom.js +371 -277
- package/lib/reanimated2/layoutReanimation/defaultAnimations/index.js +4 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/CurvedTransition.js +78 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/EntryExitTransition.js +128 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/FadingTransition.js +37 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/JumpingTransition.js +41 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/LinearTransition.js +34 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/SequencedTransition.js +46 -0
- package/lib/reanimated2/layoutReanimation/defaultTransitions/index.js +6 -0
- package/lib/reanimated2/layoutReanimation/index.js +3 -2
- package/lib/reanimated2/platform-specific/RNRenderer.web.js +2 -0
- package/lib/reanimated2/utils.js +9 -0
- package/libSo/fbjni/jni/arm64-v8a/libfbjni.so +0 -0
- package/libSo/fbjni/jni/armeabi-v7a/libfbjni.so +0 -0
- package/libSo/fbjni/jni/x86/libfbjni.so +0 -0
- package/libSo/fbjni/jni/x86_64/libfbjni.so +0 -0
- package/package.json +21 -10
- package/plugin.js +244 -85
- package/react-native-reanimated.d.ts +531 -453
- package/src/ConfigHelper.js +1 -0
- package/src/ReanimatedModule.native.js +10 -2
- package/src/createAnimatedComponent.js +64 -30
- package/src/reanimated1/core/AnimatedAlways.js +1 -1
- package/src/reanimated1/core/AnimatedBezier.js +1 -1
- package/src/reanimated1/core/AnimatedBlock.js +1 -1
- package/src/reanimated1/core/AnimatedCall.js +1 -1
- package/src/reanimated1/core/AnimatedCallFunc.js +1 -1
- package/src/reanimated1/core/AnimatedConcat.js +1 -1
- package/src/reanimated1/core/AnimatedCond.js +1 -1
- package/src/reanimated1/core/AnimatedDebug.js +1 -1
- package/src/reanimated1/core/AnimatedEvent.js +1 -1
- package/src/reanimated1/core/AnimatedFunction.js +1 -1
- package/src/reanimated1/core/AnimatedOperator.js +1 -1
- package/src/reanimated1/core/AnimatedParam.js +1 -1
- package/src/reanimated1/core/AnimatedProps.js +2 -2
- package/src/reanimated1/core/AnimatedSet.js +1 -1
- package/src/reanimated1/core/AnimatedStartClock.js +1 -1
- package/src/reanimated1/core/AnimatedStopClock.js +1 -1
- package/src/reanimated1/core/AnimatedStyle.js +1 -1
- package/src/reanimated1/core/AnimatedTransform.js +1 -1
- package/src/reanimated1/core/Core.test.js +30 -0
- package/src/reanimated1/core/InternalAnimatedValue.js +1 -1
- package/src/reanimated1/core/__mocks__/AnimatedProps.js +1 -1
- package/src/reanimated1/derived/interpolate.js +1 -1
- package/src/reanimated2/Colors.ts +30 -27
- package/src/reanimated2/Easing.ts +3 -1
- package/src/reanimated2/NativeMethods.ts +26 -16
- package/src/reanimated2/NativeReanimated/NativeReanimated.ts +78 -0
- package/src/reanimated2/NativeReanimated/index.ts +18 -0
- package/src/reanimated2/PlatformChecker.ts +21 -0
- package/src/reanimated2/PropAdapters.ts +52 -34
- package/src/reanimated2/UpdateProps.ts +40 -20
- package/src/reanimated2/ViewDescriptorsSet.ts +108 -0
- package/src/reanimated2/WorkletEventHandler.ts +41 -23
- package/src/reanimated2/animation/commonTypes.ts +48 -0
- package/src/reanimated2/animation/decay.ts +141 -0
- package/src/reanimated2/animation/delay.ts +103 -0
- package/src/reanimated2/animation/index.ts +16 -0
- package/src/reanimated2/animation/repeat.ts +128 -0
- package/src/reanimated2/animation/sequence.ts +110 -0
- package/src/reanimated2/animation/spring.ts +165 -0
- package/src/reanimated2/animation/styleAnimation.ts +253 -0
- package/src/reanimated2/animation/timing.ts +109 -0
- package/src/reanimated2/animation/util.ts +242 -0
- package/src/reanimated2/commonTypes.ts +81 -0
- package/src/reanimated2/core.ts +128 -82
- package/src/reanimated2/globals.d.ts +47 -1
- package/src/reanimated2/hook/Hooks.ts +16 -0
- package/src/reanimated2/hook/commonTypes.ts +12 -0
- package/src/reanimated2/hook/index.ts +25 -0
- package/src/reanimated2/hook/useAnimatedGestureHandler.ts +110 -0
- package/src/reanimated2/hook/useAnimatedReaction.ts +49 -0
- package/src/reanimated2/hook/useAnimatedRef.ts +34 -0
- package/src/reanimated2/hook/useAnimatedScrollHandler.ts +83 -0
- package/src/reanimated2/hook/useAnimatedStyle.ts +538 -0
- package/src/reanimated2/hook/useDerivedValue.ts +48 -0
- package/src/reanimated2/hook/useSharedValue.ts +22 -0
- package/src/reanimated2/hook/utils.ts +259 -0
- package/src/reanimated2/index.ts +3 -2
- package/src/reanimated2/interpolation.ts +170 -101
- package/src/reanimated2/jestUtils.ts +0 -12
- package/src/reanimated2/js-reanimated/JSReanimated.ts +47 -22
- package/src/reanimated2/js-reanimated/Mapper.ts +1 -1
- package/src/reanimated2/js-reanimated/index.ts +71 -0
- package/src/reanimated2/layoutReanimation/LayoutAnimationRepository.tsx +62 -0
- package/src/reanimated2/layoutReanimation/animationBuilder/BaseAnimationBuilder.ts +91 -0
- package/src/reanimated2/layoutReanimation/animationBuilder/ComplexAnimationBuilder.ts +169 -0
- package/src/reanimated2/layoutReanimation/animationBuilder/Keyframe.ts +263 -0
- package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +78 -0
- package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +16 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Bounce.ts +243 -121
- package/src/reanimated2/layoutReanimation/defaultAnimations/Default.ts +43 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Fade.ts +95 -53
- package/src/reanimated2/layoutReanimation/defaultAnimations/Flip.ts +114 -64
- package/src/reanimated2/layoutReanimation/defaultAnimations/Lightspeed.ts +179 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Pinwheel.ts +93 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Roll.ts +132 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Rotate.ts +333 -0
- package/src/reanimated2/layoutReanimation/defaultAnimations/Slide.ts +84 -50
- package/src/reanimated2/layoutReanimation/defaultAnimations/Stretch.ts +41 -23
- package/src/reanimated2/layoutReanimation/defaultAnimations/Zoom.ts +234 -154
- package/src/reanimated2/layoutReanimation/defaultAnimations/index.ts +4 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/CurvedTransition.ts +104 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/EntryExitTransition.ts +213 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/FadingTransition.ts +60 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/JumpingTransition.ts +58 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/LinearTransition.ts +41 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/SequencedTransition.ts +78 -0
- package/src/reanimated2/layoutReanimation/defaultTransitions/index.ts +6 -0
- package/src/reanimated2/layoutReanimation/index.ts +3 -2
- package/src/reanimated2/platform-specific/RNRenderer.web.ts +2 -0
- package/src/reanimated2/utils.ts +21 -0
- package/android/react-native-reanimated-62-hermes.aar +0 -0
- package/android/react-native-reanimated-62-jsc.aar +0 -0
- package/ios/LayoutReanimation/REAAnimationRootView.h +0 -12
- package/ios/LayoutReanimation/REAAnimationRootView.m +0 -16
- package/ios/LayoutReanimation/REAAnimationRootViewManager.h +0 -9
- package/ios/LayoutReanimation/REAAnimationRootViewManager.m +0 -49
- package/ios/LayoutReanimation/REAReactBatchObserver.h +0 -20
- package/ios/LayoutReanimation/REAReactBatchObserver.m +0 -125
- package/ios/LayoutReanimation/REASnapshooter.h +0 -19
- package/ios/LayoutReanimation/REASnapshooter.m +0 -64
- package/ios/LayoutReanimation/REAViewTraverser.h +0 -13
- package/ios/LayoutReanimation/REAViewTraverser.m +0 -40
- package/lib/reanimated2/Hooks.js +0 -732
- package/lib/reanimated2/NativeReanimated.js +0 -4
- package/lib/reanimated2/animations.js +0 -713
- package/lib/reanimated2/js-reanimated/index.web.js +0 -20
- package/lib/reanimated2/layoutReanimation/AnimatedRoot.js +0 -55
- package/lib/reanimated2/layoutReanimation/defaultAnimationsBuilder.js +0 -374
- package/src/reanimated2/Hooks.ts +0 -918
- package/src/reanimated2/NativeReanimated.native.ts +0 -61
- package/src/reanimated2/NativeReanimated.ts +0 -5
- package/src/reanimated2/animations.ts +0 -868
- package/src/reanimated2/js-reanimated/index.js +0 -0
- package/src/reanimated2/js-reanimated/index.web.ts +0 -29
- package/src/reanimated2/layoutReanimation/AnimatedRoot.tsx +0 -68
- package/src/reanimated2/layoutReanimation/defaultAnimationsBuilder.ts +0 -440
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { defineAnimation } from './util';
|
|
2
|
+
import {
|
|
3
|
+
Animation,
|
|
4
|
+
AnimationCallback,
|
|
5
|
+
AnimationObject,
|
|
6
|
+
PrimitiveValue,
|
|
7
|
+
Timestamp,
|
|
8
|
+
} from './commonTypes';
|
|
9
|
+
import { Platform } from 'react-native';
|
|
10
|
+
|
|
11
|
+
interface DecayConfig {
|
|
12
|
+
deceleration?: number;
|
|
13
|
+
velocityFactor?: number;
|
|
14
|
+
clamp?: number[];
|
|
15
|
+
velocity?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface DefaultDecayConfig {
|
|
19
|
+
deceleration: number;
|
|
20
|
+
velocityFactor: number;
|
|
21
|
+
clamp?: number[];
|
|
22
|
+
velocity: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface DecayAnimation extends Animation<DecayAnimation> {
|
|
26
|
+
lastTimestamp: Timestamp;
|
|
27
|
+
startTimestamp: Timestamp;
|
|
28
|
+
initialVelocity: number;
|
|
29
|
+
velocity: number;
|
|
30
|
+
current: PrimitiveValue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface InnerDecayAnimation
|
|
34
|
+
extends Omit<DecayAnimation, 'current'>,
|
|
35
|
+
AnimationObject {
|
|
36
|
+
current: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function withDecay(
|
|
40
|
+
userConfig: DecayConfig,
|
|
41
|
+
callback?: AnimationCallback
|
|
42
|
+
): Animation<DecayAnimation> {
|
|
43
|
+
'worklet';
|
|
44
|
+
|
|
45
|
+
return defineAnimation<DecayAnimation>(0, () => {
|
|
46
|
+
'worklet';
|
|
47
|
+
const config: DefaultDecayConfig = {
|
|
48
|
+
deceleration: 0.998,
|
|
49
|
+
velocityFactor: Platform.OS !== 'web' ? 1 : 1000,
|
|
50
|
+
velocity: 0,
|
|
51
|
+
};
|
|
52
|
+
if (userConfig) {
|
|
53
|
+
Object.keys(userConfig).forEach(
|
|
54
|
+
(key) =>
|
|
55
|
+
((config as any)[key] = userConfig[key as keyof typeof userConfig])
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const VELOCITY_EPS = Platform.OS !== 'web' ? 1 : 1 / 20;
|
|
60
|
+
const SLOPE_FACTOR = 0.1;
|
|
61
|
+
|
|
62
|
+
function decay(animation: InnerDecayAnimation, now: number): boolean {
|
|
63
|
+
const {
|
|
64
|
+
lastTimestamp,
|
|
65
|
+
startTimestamp,
|
|
66
|
+
initialVelocity,
|
|
67
|
+
current,
|
|
68
|
+
velocity,
|
|
69
|
+
} = animation;
|
|
70
|
+
|
|
71
|
+
const deltaTime = Math.min(now - lastTimestamp, 64);
|
|
72
|
+
const v =
|
|
73
|
+
velocity *
|
|
74
|
+
Math.exp(
|
|
75
|
+
-(1 - config.deceleration) * (now - startTimestamp) * SLOPE_FACTOR
|
|
76
|
+
);
|
|
77
|
+
animation.current =
|
|
78
|
+
current + (v * config.velocityFactor * deltaTime) / 1000; // /1000 because time is in ms not in s
|
|
79
|
+
animation.velocity = v;
|
|
80
|
+
animation.lastTimestamp = now;
|
|
81
|
+
|
|
82
|
+
if (config.clamp) {
|
|
83
|
+
if (initialVelocity < 0 && animation.current <= config.clamp[0]) {
|
|
84
|
+
animation.current = config.clamp[0];
|
|
85
|
+
return true;
|
|
86
|
+
} else if (
|
|
87
|
+
initialVelocity > 0 &&
|
|
88
|
+
animation.current >= config.clamp[1]
|
|
89
|
+
) {
|
|
90
|
+
animation.current = config.clamp[1];
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return Math.abs(v) < VELOCITY_EPS;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function validateConfig(): void {
|
|
99
|
+
if (config.clamp) {
|
|
100
|
+
if (!Array.isArray(config.clamp)) {
|
|
101
|
+
throw Error(
|
|
102
|
+
`config.clamp must be an array but is ${typeof config.clamp}`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (config.clamp.length !== 2) {
|
|
106
|
+
throw Error(
|
|
107
|
+
`clamp array must contain 2 items but is given ${config.clamp.length}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (config.velocityFactor <= 0) {
|
|
112
|
+
throw Error(
|
|
113
|
+
`config.velocityFactor must be greather then 0 but is ${config.velocityFactor}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function onStart(
|
|
119
|
+
animation: DecayAnimation,
|
|
120
|
+
value: number,
|
|
121
|
+
now: Timestamp
|
|
122
|
+
): void {
|
|
123
|
+
animation.current = value;
|
|
124
|
+
animation.lastTimestamp = now;
|
|
125
|
+
animation.startTimestamp = now;
|
|
126
|
+
animation.initialVelocity = config.velocity;
|
|
127
|
+
validateConfig();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
onFrame: decay,
|
|
132
|
+
onStart,
|
|
133
|
+
callback,
|
|
134
|
+
velocity: config.velocity ?? 0,
|
|
135
|
+
initialVelocity: 0,
|
|
136
|
+
current: 0,
|
|
137
|
+
lastTimestamp: 0,
|
|
138
|
+
startTimestamp: 0,
|
|
139
|
+
} as DecayAnimation;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { defineAnimation } from './util';
|
|
2
|
+
import {
|
|
3
|
+
Animation,
|
|
4
|
+
NextAnimation,
|
|
5
|
+
Timestamp,
|
|
6
|
+
HigherOrderAnimation,
|
|
7
|
+
PrimitiveValue,
|
|
8
|
+
} from './commonTypes';
|
|
9
|
+
|
|
10
|
+
export interface DelayAnimation
|
|
11
|
+
extends Animation<DelayAnimation>,
|
|
12
|
+
HigherOrderAnimation {
|
|
13
|
+
startTime: Timestamp;
|
|
14
|
+
started: boolean;
|
|
15
|
+
previousAnimation: DelayAnimation | null;
|
|
16
|
+
current: PrimitiveValue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function withDelay(
|
|
20
|
+
delayMs: number,
|
|
21
|
+
_nextAnimation: NextAnimation<DelayAnimation>
|
|
22
|
+
): Animation<DelayAnimation> {
|
|
23
|
+
'worklet';
|
|
24
|
+
return defineAnimation<DelayAnimation>(_nextAnimation, () => {
|
|
25
|
+
'worklet';
|
|
26
|
+
const nextAnimation =
|
|
27
|
+
typeof _nextAnimation === 'function' ? _nextAnimation() : _nextAnimation;
|
|
28
|
+
|
|
29
|
+
function delay(animation: DelayAnimation, now: Timestamp): boolean {
|
|
30
|
+
const { startTime, started, previousAnimation } = animation;
|
|
31
|
+
|
|
32
|
+
if (now - startTime > delayMs) {
|
|
33
|
+
if (!started) {
|
|
34
|
+
nextAnimation.onStart(
|
|
35
|
+
nextAnimation,
|
|
36
|
+
animation.current,
|
|
37
|
+
now,
|
|
38
|
+
previousAnimation as DelayAnimation
|
|
39
|
+
);
|
|
40
|
+
animation.previousAnimation = null;
|
|
41
|
+
animation.started = true;
|
|
42
|
+
}
|
|
43
|
+
const finished = nextAnimation.onFrame(nextAnimation, now);
|
|
44
|
+
animation.current = nextAnimation.current;
|
|
45
|
+
return finished;
|
|
46
|
+
} else if (previousAnimation) {
|
|
47
|
+
const finished =
|
|
48
|
+
previousAnimation.finished ||
|
|
49
|
+
previousAnimation.onFrame(previousAnimation, now);
|
|
50
|
+
animation.current = previousAnimation.current;
|
|
51
|
+
if (finished) {
|
|
52
|
+
animation.previousAnimation = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function onStart(
|
|
59
|
+
animation: DelayAnimation,
|
|
60
|
+
value: PrimitiveValue,
|
|
61
|
+
now: Timestamp,
|
|
62
|
+
previousAnimation: DelayAnimation
|
|
63
|
+
): void {
|
|
64
|
+
animation.startTime = now;
|
|
65
|
+
animation.started = false;
|
|
66
|
+
animation.current = value;
|
|
67
|
+
if (previousAnimation === animation) {
|
|
68
|
+
animation.previousAnimation = previousAnimation.previousAnimation;
|
|
69
|
+
} else {
|
|
70
|
+
animation.previousAnimation = previousAnimation;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const callback = (finished?: boolean): void => {
|
|
75
|
+
if (nextAnimation.callback) {
|
|
76
|
+
nextAnimation.callback(finished);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
isHigherOrder: true,
|
|
82
|
+
onFrame: delay,
|
|
83
|
+
onStart,
|
|
84
|
+
current: nextAnimation.current,
|
|
85
|
+
callback,
|
|
86
|
+
previousAnimation: null,
|
|
87
|
+
startTime: 0,
|
|
88
|
+
started: false,
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @deprecated Kept for backward compatibility. Will be removed soon.
|
|
95
|
+
*/
|
|
96
|
+
export function delay(
|
|
97
|
+
delayMs: number,
|
|
98
|
+
_nextAnimation: NextAnimation<DelayAnimation>
|
|
99
|
+
): Animation<DelayAnimation> {
|
|
100
|
+
'worklet';
|
|
101
|
+
console.warn('Method `delay` is deprecated. Please use `withDelay` instead');
|
|
102
|
+
return withDelay(delayMs, _nextAnimation);
|
|
103
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
AnimationObject,
|
|
3
|
+
Animation,
|
|
4
|
+
HigherOrderAnimation,
|
|
5
|
+
AnimationCallback,
|
|
6
|
+
NextAnimation,
|
|
7
|
+
Timestamp,
|
|
8
|
+
} from './commonTypes';
|
|
9
|
+
export { cancelAnimation, defineAnimation, initialUpdaterRun } from './util';
|
|
10
|
+
export { withTiming } from './timing';
|
|
11
|
+
export { withSpring } from './spring';
|
|
12
|
+
export { withDecay } from './decay';
|
|
13
|
+
export { withDelay } from './delay';
|
|
14
|
+
export { withRepeat } from './repeat';
|
|
15
|
+
export { withSequence } from './sequence';
|
|
16
|
+
export { withStyleAnimation } from './styleAnimation';
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { defineAnimation } from './util';
|
|
2
|
+
import {
|
|
3
|
+
Animation,
|
|
4
|
+
AnimationCallback,
|
|
5
|
+
NextAnimation,
|
|
6
|
+
PrimitiveValue,
|
|
7
|
+
Timestamp,
|
|
8
|
+
HigherOrderAnimation,
|
|
9
|
+
} from './commonTypes';
|
|
10
|
+
|
|
11
|
+
export interface RepeatAnimation
|
|
12
|
+
extends Animation<RepeatAnimation>,
|
|
13
|
+
HigherOrderAnimation {
|
|
14
|
+
reps: number;
|
|
15
|
+
startValue: PrimitiveValue;
|
|
16
|
+
toValue?: PrimitiveValue;
|
|
17
|
+
previousAnimation?: RepeatAnimation;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface InnerRepeatAnimation
|
|
21
|
+
extends Omit<RepeatAnimation, 'toValue' | 'startValue'> {
|
|
22
|
+
toValue: number;
|
|
23
|
+
startValue: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function withRepeat(
|
|
27
|
+
_nextAnimation: NextAnimation<RepeatAnimation>,
|
|
28
|
+
numberOfReps = 2,
|
|
29
|
+
reverse = false,
|
|
30
|
+
callback?: AnimationCallback
|
|
31
|
+
): Animation<RepeatAnimation> {
|
|
32
|
+
'worklet';
|
|
33
|
+
|
|
34
|
+
return defineAnimation<RepeatAnimation>(_nextAnimation, () => {
|
|
35
|
+
'worklet';
|
|
36
|
+
|
|
37
|
+
const nextAnimation: RepeatAnimation =
|
|
38
|
+
typeof _nextAnimation === 'function' ? _nextAnimation() : _nextAnimation;
|
|
39
|
+
|
|
40
|
+
function repeat(animation: InnerRepeatAnimation, now: Timestamp): boolean {
|
|
41
|
+
const finished = nextAnimation.onFrame(nextAnimation, now);
|
|
42
|
+
animation.current = nextAnimation.current;
|
|
43
|
+
if (finished) {
|
|
44
|
+
animation.reps += 1;
|
|
45
|
+
// call inner animation's callback on every repetition
|
|
46
|
+
// as the second argument the animation's current value is passed
|
|
47
|
+
if (nextAnimation.callback) {
|
|
48
|
+
nextAnimation.callback(true /* finished */, animation.current);
|
|
49
|
+
}
|
|
50
|
+
if (numberOfReps > 0 && animation.reps >= numberOfReps) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const startValue = reverse
|
|
55
|
+
? (nextAnimation.current as number)
|
|
56
|
+
: animation.startValue;
|
|
57
|
+
if (reverse) {
|
|
58
|
+
nextAnimation.toValue = animation.startValue;
|
|
59
|
+
animation.startValue = startValue;
|
|
60
|
+
}
|
|
61
|
+
nextAnimation.onStart(
|
|
62
|
+
nextAnimation,
|
|
63
|
+
startValue,
|
|
64
|
+
now,
|
|
65
|
+
nextAnimation.previousAnimation as RepeatAnimation
|
|
66
|
+
);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const repCallback = (finished: boolean): void => {
|
|
73
|
+
if (callback) {
|
|
74
|
+
callback(finished);
|
|
75
|
+
}
|
|
76
|
+
// when cancelled call inner animation's callback
|
|
77
|
+
if (!finished && nextAnimation.callback) {
|
|
78
|
+
nextAnimation.callback(false /* finished */);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
function onStart(
|
|
83
|
+
animation: RepeatAnimation,
|
|
84
|
+
value: PrimitiveValue,
|
|
85
|
+
now: Timestamp,
|
|
86
|
+
previousAnimation: RepeatAnimation
|
|
87
|
+
): void {
|
|
88
|
+
animation.startValue = value;
|
|
89
|
+
animation.reps = 0;
|
|
90
|
+
nextAnimation.onStart(nextAnimation, value, now, previousAnimation);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
isHigherOrder: true,
|
|
95
|
+
onFrame: repeat,
|
|
96
|
+
onStart,
|
|
97
|
+
reps: 0,
|
|
98
|
+
current: nextAnimation.current,
|
|
99
|
+
callback: repCallback,
|
|
100
|
+
startValue: 0,
|
|
101
|
+
} as RepeatAnimation;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @deprecated Kept for backward compatibility. Will be removed soon.
|
|
107
|
+
*/
|
|
108
|
+
export function repeat(
|
|
109
|
+
_nextAnimation: NextAnimation<RepeatAnimation>,
|
|
110
|
+
numberOfReps = 2,
|
|
111
|
+
reverse = false,
|
|
112
|
+
callback?: AnimationCallback
|
|
113
|
+
): Animation<RepeatAnimation> {
|
|
114
|
+
'worklet';
|
|
115
|
+
console.warn(
|
|
116
|
+
'Method `repeat` is deprecated. Please use `withRepeat` instead'
|
|
117
|
+
);
|
|
118
|
+
return withRepeat(_nextAnimation, numberOfReps, reverse, callback);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function loop(
|
|
122
|
+
nextAnimation: NextAnimation<RepeatAnimation>,
|
|
123
|
+
numberOfLoops = 1
|
|
124
|
+
): Animation<RepeatAnimation> {
|
|
125
|
+
'worklet';
|
|
126
|
+
console.warn('Method `loop` is deprecated. Please use `withRepeat` instead');
|
|
127
|
+
return repeat(nextAnimation, Math.round(numberOfLoops * 2), true);
|
|
128
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { defineAnimation } from './util';
|
|
2
|
+
import {
|
|
3
|
+
Animation,
|
|
4
|
+
Timestamp,
|
|
5
|
+
NextAnimation,
|
|
6
|
+
PrimitiveValue,
|
|
7
|
+
HigherOrderAnimation,
|
|
8
|
+
AnimationObject,
|
|
9
|
+
} from './commonTypes';
|
|
10
|
+
|
|
11
|
+
export interface SequenceAnimation
|
|
12
|
+
extends Animation<SequenceAnimation>,
|
|
13
|
+
HigherOrderAnimation {
|
|
14
|
+
animationIndex: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function withSequence(
|
|
18
|
+
..._animations: NextAnimation<AnimationObject>[]
|
|
19
|
+
): Animation<SequenceAnimation> {
|
|
20
|
+
'worklet';
|
|
21
|
+
return defineAnimation<SequenceAnimation>(
|
|
22
|
+
_animations[0] as SequenceAnimation,
|
|
23
|
+
() => {
|
|
24
|
+
'worklet';
|
|
25
|
+
const animations = _animations.map((a) => {
|
|
26
|
+
const result = typeof a === 'function' ? a() : a;
|
|
27
|
+
result.finished = false;
|
|
28
|
+
return result;
|
|
29
|
+
});
|
|
30
|
+
const firstAnimation = animations[0];
|
|
31
|
+
|
|
32
|
+
const callback = (finished: boolean): void => {
|
|
33
|
+
if (finished) {
|
|
34
|
+
// we want to call the callback after every single animation
|
|
35
|
+
// not after all of them
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
// this is going to be called only if sequence has been cancelled
|
|
39
|
+
animations.forEach((animation) => {
|
|
40
|
+
if (typeof animation.callback === 'function' && !animation.finished) {
|
|
41
|
+
animation.callback(finished);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function sequence(animation: SequenceAnimation, now: Timestamp): boolean {
|
|
47
|
+
const currentAnim = animations[animation.animationIndex];
|
|
48
|
+
const finished = currentAnim.onFrame(currentAnim, now);
|
|
49
|
+
animation.current = currentAnim.current;
|
|
50
|
+
if (finished) {
|
|
51
|
+
// we want to call the callback after every single animation
|
|
52
|
+
if (currentAnim.callback) {
|
|
53
|
+
currentAnim.callback(true /* finished */);
|
|
54
|
+
}
|
|
55
|
+
currentAnim.finished = true;
|
|
56
|
+
animation.animationIndex += 1;
|
|
57
|
+
if (animation.animationIndex < animations.length) {
|
|
58
|
+
const nextAnim = animations[animation.animationIndex];
|
|
59
|
+
nextAnim.onStart(nextAnim, currentAnim.current, now, currentAnim);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function onStart(
|
|
68
|
+
animation: SequenceAnimation,
|
|
69
|
+
value: PrimitiveValue,
|
|
70
|
+
now: Timestamp,
|
|
71
|
+
previousAnimation: SequenceAnimation
|
|
72
|
+
): void {
|
|
73
|
+
if (animations.length === 1) {
|
|
74
|
+
throw Error(
|
|
75
|
+
'withSequence() animation require more than one animation as argument'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
animation.animationIndex = 0;
|
|
79
|
+
if (previousAnimation === undefined) {
|
|
80
|
+
previousAnimation = animations[
|
|
81
|
+
animations.length - 1
|
|
82
|
+
] as SequenceAnimation;
|
|
83
|
+
}
|
|
84
|
+
firstAnimation.onStart(firstAnimation, value, now, previousAnimation);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
isHigherOrder: true,
|
|
89
|
+
onFrame: sequence,
|
|
90
|
+
onStart,
|
|
91
|
+
animationIndex: 0,
|
|
92
|
+
current: firstAnimation.current,
|
|
93
|
+
callback,
|
|
94
|
+
} as SequenceAnimation;
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @deprecated Kept for backward compatibility. Will be removed soon.
|
|
101
|
+
*/
|
|
102
|
+
export function sequence(
|
|
103
|
+
..._animations: NextAnimation<SequenceAnimation>[]
|
|
104
|
+
): Animation<SequenceAnimation> {
|
|
105
|
+
'worklet';
|
|
106
|
+
console.warn(
|
|
107
|
+
'Method `sequence` is deprecated. Please use `withSequence` instead'
|
|
108
|
+
);
|
|
109
|
+
return withSequence(..._animations);
|
|
110
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { defineAnimation } from './util';
|
|
2
|
+
import {
|
|
3
|
+
Animation,
|
|
4
|
+
AnimationCallback,
|
|
5
|
+
PrimitiveValue,
|
|
6
|
+
Timestamp,
|
|
7
|
+
} from './commonTypes';
|
|
8
|
+
|
|
9
|
+
interface SpringConfig {
|
|
10
|
+
// [key: string]: any;
|
|
11
|
+
mass?: number;
|
|
12
|
+
stiffness?: number;
|
|
13
|
+
overshootClamping?: boolean;
|
|
14
|
+
restDisplacementThreshold?: number;
|
|
15
|
+
restSpeedThreshold?: number;
|
|
16
|
+
velocity?: number;
|
|
17
|
+
damping?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SpringAnimation extends Animation<SpringAnimation> {
|
|
21
|
+
current: PrimitiveValue;
|
|
22
|
+
toValue: PrimitiveValue;
|
|
23
|
+
velocity: number;
|
|
24
|
+
lastTimestamp: Timestamp;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface InnerSpringAnimation
|
|
28
|
+
extends Omit<SpringAnimation, 'toValue' | 'current'> {
|
|
29
|
+
toValue: number;
|
|
30
|
+
current: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function withSpring(
|
|
34
|
+
toValue: PrimitiveValue,
|
|
35
|
+
userConfig?: SpringConfig,
|
|
36
|
+
callback?: AnimationCallback
|
|
37
|
+
): Animation<SpringAnimation> {
|
|
38
|
+
'worklet';
|
|
39
|
+
|
|
40
|
+
return defineAnimation<SpringAnimation>(toValue, () => {
|
|
41
|
+
'worklet';
|
|
42
|
+
|
|
43
|
+
// TODO: figure out why we can't use spread or Object.assign here
|
|
44
|
+
// when user config is "frozen object" we can't enumerate it (perhaps
|
|
45
|
+
// something is wrong with the object prototype).
|
|
46
|
+
const config: Required<SpringConfig> = {
|
|
47
|
+
damping: 10,
|
|
48
|
+
mass: 1,
|
|
49
|
+
stiffness: 100,
|
|
50
|
+
overshootClamping: false,
|
|
51
|
+
restDisplacementThreshold: 0.01,
|
|
52
|
+
restSpeedThreshold: 2,
|
|
53
|
+
velocity: 0,
|
|
54
|
+
};
|
|
55
|
+
if (userConfig) {
|
|
56
|
+
Object.keys(userConfig).forEach(
|
|
57
|
+
(key) =>
|
|
58
|
+
((config as any)[key] = userConfig[key as keyof typeof userConfig])
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function spring(animation: InnerSpringAnimation, now: Timestamp): boolean {
|
|
63
|
+
const { toValue, lastTimestamp, current, velocity } = animation;
|
|
64
|
+
|
|
65
|
+
const deltaTime = Math.min(now - lastTimestamp, 64);
|
|
66
|
+
animation.lastTimestamp = now;
|
|
67
|
+
|
|
68
|
+
const c = config.damping;
|
|
69
|
+
const m = config.mass;
|
|
70
|
+
const k = config.stiffness;
|
|
71
|
+
|
|
72
|
+
const v0 = -velocity;
|
|
73
|
+
const x0 = toValue - current;
|
|
74
|
+
|
|
75
|
+
const zeta = c / (2 * Math.sqrt(k * m)); // damping ratio
|
|
76
|
+
const omega0 = Math.sqrt(k / m); // undamped angular frequency of the oscillator (rad/ms)
|
|
77
|
+
const omega1 = omega0 * Math.sqrt(1 - zeta ** 2); // exponential decay
|
|
78
|
+
|
|
79
|
+
const t = deltaTime / 1000;
|
|
80
|
+
|
|
81
|
+
const sin1 = Math.sin(omega1 * t);
|
|
82
|
+
const cos1 = Math.cos(omega1 * t);
|
|
83
|
+
|
|
84
|
+
// under damped
|
|
85
|
+
const underDampedEnvelope = Math.exp(-zeta * omega0 * t);
|
|
86
|
+
const underDampedFrag1 =
|
|
87
|
+
underDampedEnvelope *
|
|
88
|
+
(sin1 * ((v0 + zeta * omega0 * x0) / omega1) + x0 * cos1);
|
|
89
|
+
|
|
90
|
+
const underDampedPosition = toValue - underDampedFrag1;
|
|
91
|
+
// This looks crazy -- it's actually just the derivative of the oscillation function
|
|
92
|
+
const underDampedVelocity =
|
|
93
|
+
zeta * omega0 * underDampedFrag1 -
|
|
94
|
+
underDampedEnvelope *
|
|
95
|
+
(cos1 * (v0 + zeta * omega0 * x0) - omega1 * x0 * sin1);
|
|
96
|
+
|
|
97
|
+
// critically damped
|
|
98
|
+
const criticallyDampedEnvelope = Math.exp(-omega0 * t);
|
|
99
|
+
const criticallyDampedPosition =
|
|
100
|
+
toValue - criticallyDampedEnvelope * (x0 + (v0 + omega0 * x0) * t);
|
|
101
|
+
|
|
102
|
+
const criticallyDampedVelocity =
|
|
103
|
+
criticallyDampedEnvelope *
|
|
104
|
+
(v0 * (t * omega0 - 1) + t * x0 * omega0 * omega0);
|
|
105
|
+
|
|
106
|
+
const isOvershooting = () => {
|
|
107
|
+
if (config.overshootClamping && config.stiffness !== 0) {
|
|
108
|
+
return current < toValue
|
|
109
|
+
? animation.current > toValue
|
|
110
|
+
: animation.current < toValue;
|
|
111
|
+
} else {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const isVelocity = Math.abs(velocity) < config.restSpeedThreshold;
|
|
117
|
+
const isDisplacement =
|
|
118
|
+
config.stiffness === 0 ||
|
|
119
|
+
Math.abs(toValue - current) < config.restDisplacementThreshold;
|
|
120
|
+
|
|
121
|
+
if (zeta < 1) {
|
|
122
|
+
animation.current = underDampedPosition;
|
|
123
|
+
animation.velocity = underDampedVelocity;
|
|
124
|
+
} else {
|
|
125
|
+
animation.current = criticallyDampedPosition;
|
|
126
|
+
animation.velocity = criticallyDampedVelocity;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (isOvershooting() || (isVelocity && isDisplacement)) {
|
|
130
|
+
if (config.stiffness !== 0) {
|
|
131
|
+
animation.velocity = 0;
|
|
132
|
+
animation.current = toValue;
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function onStart(
|
|
140
|
+
animation: SpringAnimation,
|
|
141
|
+
value: number,
|
|
142
|
+
now: Timestamp,
|
|
143
|
+
previousAnimation: SpringAnimation
|
|
144
|
+
): void {
|
|
145
|
+
animation.current = value;
|
|
146
|
+
if (previousAnimation) {
|
|
147
|
+
animation.velocity =
|
|
148
|
+
previousAnimation.velocity || animation.velocity || 0;
|
|
149
|
+
animation.lastTimestamp = previousAnimation.lastTimestamp || now;
|
|
150
|
+
} else {
|
|
151
|
+
animation.lastTimestamp = now;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
onFrame: spring,
|
|
157
|
+
onStart,
|
|
158
|
+
toValue,
|
|
159
|
+
velocity: config.velocity || 0,
|
|
160
|
+
current: toValue,
|
|
161
|
+
callback,
|
|
162
|
+
lastTimestamp: 0,
|
|
163
|
+
} as SpringAnimation;
|
|
164
|
+
});
|
|
165
|
+
}
|