|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +import * as React from 'react'; |
| 12 | +import {Text, Animated, StyleSheet} from 'react-native'; |
| 13 | +import type {RNTesterModuleExample} from '../../../types/RNTesterTypes'; |
| 14 | +import ToggleNativeDriver from '../../Animated/utils/ToggleNativeDriver'; |
| 15 | + |
| 16 | +const WIDTH = 200; |
| 17 | +const HEIGHT = 250; |
| 18 | + |
| 19 | +const styles = StyleSheet.create({ |
| 20 | + container: { |
| 21 | + backgroundColor: 'black', |
| 22 | + marginTop: 20, |
| 23 | + width: WIDTH, |
| 24 | + height: HEIGHT, |
| 25 | + alignSelf: 'center', |
| 26 | + }, |
| 27 | + text: { |
| 28 | + color: 'white', |
| 29 | + position: 'absolute', |
| 30 | + top: HEIGHT / 2, |
| 31 | + }, |
| 32 | + animatingBox: { |
| 33 | + backgroundColor: 'blue', |
| 34 | + width: 1, |
| 35 | + height: 1, |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +function CompatibilityAnimatedPointerMove(): React.Node { |
| 40 | + const xCoord = React.useRef(new Animated.Value(0)).current; |
| 41 | + const yCoord = React.useRef(new Animated.Value(0)).current; |
| 42 | + const [useNativeDriver, setUseNativeDriver] = React.useState(true); |
| 43 | + |
| 44 | + return ( |
| 45 | + <> |
| 46 | + <ToggleNativeDriver |
| 47 | + style={{paddingHorizontal: 30}} |
| 48 | + value={useNativeDriver} |
| 49 | + onValueChange={setUseNativeDriver} |
| 50 | + /> |
| 51 | + <Animated.View |
| 52 | + onPointerMove={Animated.event( |
| 53 | + [{nativeEvent: {offsetX: xCoord, offsetY: yCoord}}], |
| 54 | + {useNativeDriver}, |
| 55 | + )} |
| 56 | + pointerEvents="box-only" |
| 57 | + style={styles.container}> |
| 58 | + <Text style={styles.text}>Move pointer over me</Text> |
| 59 | + <Animated.View |
| 60 | + style={{ |
| 61 | + backgroundColor: 'blue', |
| 62 | + width: 1, |
| 63 | + height: 1, |
| 64 | + transform: [ |
| 65 | + { |
| 66 | + translateX: xCoord.interpolate({ |
| 67 | + inputRange: [0, WIDTH], |
| 68 | + outputRange: ([0, WIDTH / 2]: number[]), |
| 69 | + }), |
| 70 | + }, |
| 71 | + { |
| 72 | + translateY: yCoord.interpolate({ |
| 73 | + inputRange: [0, HEIGHT], |
| 74 | + outputRange: ([0, HEIGHT / 2]: number[]), |
| 75 | + }), |
| 76 | + }, |
| 77 | + { |
| 78 | + scaleX: xCoord.interpolate({ |
| 79 | + inputRange: [0, WIDTH], |
| 80 | + outputRange: ([0, WIDTH]: number[]), |
| 81 | + }), |
| 82 | + }, |
| 83 | + { |
| 84 | + scaleY: yCoord.interpolate({ |
| 85 | + inputRange: [0, HEIGHT], |
| 86 | + outputRange: ([0, HEIGHT]: number[]), |
| 87 | + }), |
| 88 | + }, |
| 89 | + ], |
| 90 | + }} |
| 91 | + /> |
| 92 | + </Animated.View> |
| 93 | + </> |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +export default ({ |
| 98 | + name: 'compatibility_animatedevent_pointer_move', |
| 99 | + description: |
| 100 | + 'An AnimatedEvent example on onPointerMove. The blue box should scale to pointer event offset values within black box', |
| 101 | + title: 'AnimatedEvent with pointermove', |
| 102 | + render(): React.Node { |
| 103 | + return <CompatibilityAnimatedPointerMove />; |
| 104 | + }, |
| 105 | +}: RNTesterModuleExample); |
0 commit comments