Created
September 4, 2021 12:24
-
-
Save raspasov/d42656703b6d0c4e000ee83a82369b71 to your computer and use it in GitHub Desktop.
Reanimated v2 shim for CLJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useImperativeHandle, useRef, forwardRef} from 'react'; | |
import Animated, { | |
useSharedValue, | |
useAnimatedStyle, | |
withTiming, | |
withSpring | |
} from 'react-native-reanimated'; | |
function Box(props, ref) { | |
const aRef = useRef(); | |
useImperativeHandle(ref, function () { | |
let animConfig = { | |
damping: 10.0, | |
mass: 1, | |
stiffness: 100, | |
overshootClamping: true, | |
restDisplacementThreshold: 0.001, | |
restSpeedThreshold: 0.1 | |
} | |
return { | |
moveBox: function (globalX, globalY) { | |
translateYAnim.value = withSpring(globalY, animConfig); | |
translateXAnim.value = withSpring(globalX, animConfig); | |
//no smoothing | |
// translateYAnim.value = globalY; | |
// translateXAnim.value = globalX; | |
}, | |
allReanimatedValues: function () { | |
return { | |
width: widthAnim, | |
height: heightAnim, | |
translateX: translateXAnim, | |
translateY: translateYAnim | |
} | |
} | |
}; | |
}); | |
const init_translate_y = props['init-translate-y'] || 0; | |
const init_translate_x = props['init-translate-x'] || 0; | |
const init_height = props['init-height'] || 0; | |
const init_width = props['init-width'] || 0; | |
const style = props['style'] | |
const translateYAnim = useSharedValue(init_translate_y); | |
const translateXAnim = useSharedValue(init_translate_x); | |
const heightAnim = useSharedValue(init_height); | |
const widthAnim = useSharedValue(init_width); | |
const animatedStyles = useAnimatedStyle(function () { | |
return { | |
height: heightAnim.value, | |
width: widthAnim.value, | |
transform: [{translateY: translateYAnim.value}, {translateX: translateXAnim.value}], | |
}; | |
}); | |
return ( | |
<Animated.View ref={aRef} style={[animatedStyles, style]}> | |
{props.children} | |
</Animated.View>) | |
} | |
exports.Box = forwardRef(Box); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment