useAnimatedSensor
With the useAnimatedSensor hook, you can easily create cool interactive animations based on data from sensors in the device such as gyroscope, accelerometer etc.
useAnimatedSensor(sensorType: [SensorType], config?: [UserConfig]) -> [AnimatedSensor]
Arguments
sensorType - [SensorType]
You can select the sensor available in [SensorType] enum.
config - [UserConfig]
Optionally, you can pass configuration to customize the sensor behavior.
Returns
Hook useAnimatedSensor returns an instance of [AnimatedSensor];
Types
AnimatedSensor: [object]
Properties:
sensor: [SharedValue] contains [3DVector] or [RotationVector] ornull
contains actual sensor measurements as a shared valueunregister: [function]
allows you to stop listening to sensor updatesisAvailable: [boolean]
the flag contains information on the availability of sensors in a deviceconfig: [UserConfig]
the configuration provided by a user
SensorType: [enum]
SensorType is an enum that contains possibly supported sensors.
Values:
ACCELEROMETER
measurements output as [3DVector]. Measured in m/s², excluding gravity.GYROSCOPE
measurements output as [3DVector]. Measured in rad/s.GRAVITY
measurements output as [3DVector]. Measured in m/s².MAGNETIC_FIELD
measurements output as [3DVector]. Measured in μT.ROTATION
measurements output as [RotationVector]. [qx, qy, qz, qw] is a normalized quaternion. [yaw, pitch, roll] are rotations measured in radians along respective axes. We follow the iOS convention.
UserConfig: [object]
Properties:
interval: [number | auto]- interval in milliseconds between shared value updates. Pass'auto'to select interval based on device frame rate. Default:'auto'.iosReferenceFrame: [[IOSReferenceFrame](#iosreferenceframe-enum)]- reference frame to use on iOS. Default:Auto.adjustToInterfaceOrientation: [boolean]- whether to adjust measurements to the current interface orientation. For example, in the landscape orientation axes x and y may need to be reversed when drawn on the screen. It'strueby default.
IOSReferenceFrame: [enum]
IOSReferenceFrame is an enum describing reference frame to use on iOS. It follows Apple's documentation. Possible values:
XArbitraryZVerticalXArbitraryCorrectedZVerticalXMagneticNorthZVerticalXTrueNorthZVerticalAuto- on devices without magnetometer (for example iPods)XArbitraryZVertical, on devices with magnetometerXArbitraryCorrectedZVertical
3DVector: [object]
Properties:
x: numbery: numberz: numberinterfaceOrientation: [[InterfaceOrientation](#interfaceorientation-enum)]
RotationVector: [object]
Properties:
qw: numberqx: numberqy: numberqz: numberyaw: numberpitch: numberroll: numberinterfaceOrientation: [[InterfaceOrientation](#interfaceorientation-enum)]
InterfaceOrientation: [enum]
Values:
ROTATION_0- default rotation on Android, portrait orientation on iOSROTATION_90- 90 degrees rotation on Android, landscape right orientation on iOS (landscape and home button on the right)ROTATION_180- 180 degrees rotation on Android, upside down orientation on iOSROTATION_270- 270 degrees rotation on Android, landscape left orientation on iOS (landscape and home button on the left)
Example
function UseAnimatedSensorExample() {
const animatedSensor = useAnimatedSensor(SensorType.ROTATION, { interval: 10 }); // <- initialization
const style = useAnimatedStyle(() => {
const yaw = Math.abs(animatedSensor.sensor.value.yaw);
const pitch = Math.abs(animatedSensor.sensor.value.pitch);
return {
height: withTiming(yaw * 200 + 20, { duration: 100 }), // <- usage
width: withTiming(pitch * 200 + 20, { duration: 100 }), // <- usage
};
});
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Animated.View
style={[{ backgroundColor: 'black' }, style]}
/>
</View>
);
}
Live example
Tips
caution
On iOS, if you want to read sensor data you need to enable location services on your device (Settings > Privacy > Location Services).