Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useAnimatedSensor for Web #3955

Merged
merged 8 commits into from Jan 17, 2023
Merged

Conversation

mstach60161
Copy link
Contributor

@mstach60161 mstach60161 commented Jan 16, 2023

Summary

  • Added useAnimatedSensor for the web implementation

Test plan

  • Used chrome on the android device to check if the sensor returns correct data

src/reanimated2/js-reanimated/JSReanimated.ts Outdated Show resolved Hide resolved
src/reanimated2/js-reanimated/JSReanimated.ts Outdated Show resolved Hide resolved
src/reanimated2/js-reanimated/JSReanimated.ts Outdated Show resolved Hide resolved
src/reanimated2/js-reanimated/JSReanimated.ts Outdated Show resolved Hide resolved
src/reanimated2/js-reanimated/JSReanimated.ts Outdated Show resolved Hide resolved
@mstach60161 mstach60161 marked this pull request as ready for review January 17, 2023 11:20
Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@mstach60161 mstach60161 merged commit e751b3c into main Jan 17, 2023
@mstach60161 mstach60161 deleted the @mstach60161/animatedSensor-for-web branch January 17, 2023 12:03
const config =
interval === -1
? { referenceFrame: 'device' }
: { frequency: 1 / interval };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be{ frequency: 1000 / interval }, because the interval contains milliseconds.

Comment on lines +74 to +93
sensor.addEventListener('reading', () => {
if (sensorType === SensorType.ROTATION) {
const [qw, qx, qy, qz] = sensor.quaternion;

// reference: https://stackoverflow.com/questions/5782658/extracting-yaw-from-a-quaternion
const yaw = Math.atan2(
2.0 * (qy * qz + qw * qx),
qw * qw - qx * qx - qy * qy + qz * qz
);
const pitch = Math.sin(-2.0 * (qx * qz - qw * qy));
const roll = Math.atan2(
2.0 * (qx * qy + qw * qz),
qw * qw + qx * qx - qy * qy - qz * qz
);
eventHandler({ qw, qx, qy, qz, yaw, pitch, roll });
} else {
const { x, y, z } = sensor;
eventHandler({ x, y, z });
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this way we can get rid off unnecessary if condition inside of callback:

let callback;
if (sensorType === SensorType.ROTATION) {
  callback = () => {
    const [qw, qx, qy, qz] = sensor.quaternion;

    // reference: https://stackoverflow.com/questions/5782658/extracting-yaw-from-a-quaternion
    const yaw = Math.atan2(
      2.0 * (qy * qz + qw * qx),
      qw * qw - qx * qx - qy * qy + qz * qz
    );
    const pitch = Math.sin(-2.0 * (qx * qz - qw * qy));
    const roll = Math.atan2(
      2.0 * (qx * qy + qw * qz),
      qw * qw + qx * qx - qy * qy - qz * qz
    );
    eventHandler({ qw, qx, qy, qz, yaw, pitch, roll });
  };
} else {
  callback = () => {
    const { x, y, z } = sensor;
    eventHandler({ x, y, z });
  };
}
sensor.addEventListener('reading', callback);

const config =
interval === -1
? { referenceFrame: 'device' }
: { frequency: 1 / interval };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if interval is equal 0 ?

piaskowyk pushed a commit that referenced this pull request Jan 18, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

<!-- Explain the motivation for this PR. Include "Fixes #<number>" if
applicable. -->

Applied requested changes to #3955 after late code review. 
- Removed unnecessary if condition inside callback.
- Fixed unit conversion bug

## Test plan

- Chrome on the android device.
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

- Added `useAnimatedSensor` for the web implementation

## Test plan

- Used chrome on the android device to check if the sensor returns
correct data
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

<!-- Explain the motivation for this PR. Include "Fixes #<number>" if
applicable. -->

Applied requested changes to software-mansion#3955 after late code review. 
- Removed unnecessary if condition inside callback.
- Fixed unit conversion bug

## Test plan

- Chrome on the android device.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants