forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added useAnimatedSensor() (software-mansion#2868)
## Description Added API to create an animation based on sensor device data in a very simple way. ### Example code ```js function AnimatedStyleUpdateExample() { 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={[ { width: 100, height: 80, backgroundColor: 'black', margin: 30 }, style, ]} /> </View> ); } ``` https://user-images.githubusercontent.com/36106620/158634922-eaad656e-c837-44d5-8d51-8e7fa27c5a16.mp4
- Loading branch information
Showing
36 changed files
with
915 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "AnimatedSensorModule.h" | ||
#include "MutableValue.h" | ||
#include "ValueWrapper.h" | ||
|
||
namespace reanimated { | ||
|
||
AnimatedSensorModule::AnimatedSensorModule( | ||
const PlatformDepMethodsHolder &platformDepMethodsHolder, | ||
RuntimeManager *runtimeManager) | ||
: platformRegisterSensorFunction_(platformDepMethodsHolder.registerSensor), | ||
platformUnregisterSensorFunction_( | ||
platformDepMethodsHolder.unregisterSensor), | ||
runtimeManager_(runtimeManager) {} | ||
|
||
AnimatedSensorModule::~AnimatedSensorModule() { | ||
// It is called during app reload because app reload doesn't call hooks | ||
// unmounting | ||
for (auto sensorId : sensorsIds_) { | ||
platformUnregisterSensorFunction_(sensorId); | ||
} | ||
} | ||
|
||
jsi::Value AnimatedSensorModule::registerSensor( | ||
jsi::Runtime &rt, | ||
const jsi::Value &sensorType, | ||
const jsi::Value &interval, | ||
const jsi::Value &sensorDataContainer) { | ||
std::shared_ptr<ShareableValue> sensorsData = ShareableValue::adapt( | ||
rt, sensorDataContainer.getObject(rt), runtimeManager_); | ||
auto &mutableObject = | ||
ValueWrapper::asMutableValue(sensorsData->valueContainer); | ||
|
||
std::function<void(double[])> setter; | ||
if (sensorType.asNumber() == SensorType::ROTATION_VECTOR) { | ||
setter = [&, mutableObject](double newValues[]) { | ||
jsi::Runtime &runtime = *runtimeManager_->runtime.get(); | ||
jsi::Object value(runtime); | ||
value.setProperty(runtime, "qw", newValues[0]); | ||
value.setProperty(runtime, "qx", newValues[1]); | ||
value.setProperty(runtime, "qy", newValues[2]); | ||
value.setProperty(runtime, "qz", newValues[3]); | ||
value.setProperty(runtime, "yaw", newValues[4]); | ||
value.setProperty(runtime, "pitch", newValues[5]); | ||
value.setProperty(runtime, "roll", newValues[6]); | ||
mutableObject->setValue(runtime, std::move(value)); | ||
}; | ||
} else { | ||
setter = [&, mutableObject](double newValues[]) { | ||
jsi::Runtime &runtime = *runtimeManager_->runtime.get(); | ||
jsi::Object value(runtime); | ||
value.setProperty(runtime, "x", newValues[0]); | ||
value.setProperty(runtime, "y", newValues[1]); | ||
value.setProperty(runtime, "z", newValues[2]); | ||
mutableObject->setValue(runtime, std::move(value)); | ||
}; | ||
} | ||
|
||
int sensorId = platformRegisterSensorFunction_( | ||
sensorType.asNumber(), interval.asNumber(), setter); | ||
if (sensorId != -1) { | ||
sensorsIds_.insert(sensorId); | ||
} | ||
return jsi::Value(sensorId); | ||
} | ||
|
||
void AnimatedSensorModule::unregisterSensor(const jsi::Value &sensorId) { | ||
// It is called during sensor hook unmounting | ||
sensorsIds_.erase(sensorId.getNumber()); | ||
platformUnregisterSensorFunction_(sensorId.asNumber()); | ||
} | ||
|
||
} // namespace reanimated |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include <jsi/jsi.h> | ||
#include <unordered_set> | ||
|
||
#include "PlatformDepMethodsHolder.h" | ||
#include "RuntimeManager.h" | ||
|
||
namespace reanimated { | ||
|
||
using namespace facebook; | ||
|
||
enum SensorType { | ||
ACCELEROMETER = 1, | ||
GYROSCOPE = 2, | ||
GRAVITY = 3, | ||
MAGNETIC_FIELD = 4, | ||
ROTATION_VECTOR = 5, | ||
}; | ||
|
||
class AnimatedSensorModule { | ||
std::unordered_set<int> sensorsIds_; | ||
RegisterSensorFunction platformRegisterSensorFunction_; | ||
UnregisterSensorFunction platformUnregisterSensorFunction_; | ||
RuntimeManager *runtimeManager_; | ||
|
||
public: | ||
AnimatedSensorModule( | ||
const PlatformDepMethodsHolder &platformDepMethodsHolder, | ||
RuntimeManager *runtimeManager); | ||
~AnimatedSensorModule(); | ||
|
||
jsi::Value registerSensor( | ||
jsi::Runtime &rt, | ||
const jsi::Value &sensorType, | ||
const jsi::Value &interval, | ||
const jsi::Value &sensorDataContainer); | ||
void unregisterSensor(const jsi::Value &sensorId); | ||
}; | ||
|
||
} // namespace reanimated |
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import Animated, { | ||
withTiming, | ||
useAnimatedStyle, | ||
useAnimatedSensor, | ||
SensorType, | ||
} from 'react-native-reanimated'; | ||
import { View, Button, StyleSheet } from 'react-native'; | ||
|
||
export default function AnimatedStyleUpdateExample() { | ||
const animatedSensor = useAnimatedSensor(SensorType.ROTATION, { | ||
interval: 10, | ||
}); | ||
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 }), | ||
width: withTiming(pitch * 200 + 20, { duration: 100 }), | ||
}; | ||
}); | ||
|
||
return ( | ||
<View style={componentStyle.container}> | ||
<Button | ||
title={'log data'} | ||
onPress={() => console.log(animatedSensor.sensor.value)} | ||
/> | ||
<Animated.View style={[componentStyle.square, style]} /> | ||
</View> | ||
); | ||
} | ||
|
||
const componentStyle = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
square: { | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'black', | ||
margin: 30, | ||
}, | ||
}); |
Oops, something went wrong.