UNPKG

react-native-reanimated

Version:

More powerful alternative to Animated library for React Native.

161 lines (126 loc) 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _NativeReanimated = require("../NativeReanimated/NativeReanimated"); var _commonTypes = require("../commonTypes"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class JSReanimated extends _NativeReanimated.NativeReanimated { constructor() { super(false); _defineProperty(this, "nextSensorId", 0); _defineProperty(this, "sensors", new Map()); } makeShareableClone(value) { return { __hostObjectShareableJSRef: value }; } installCoreFunctions(_callGuard, _valueUnpacker) {// noop } scheduleOnUI(worklet) { // @ts-ignore web implementation has still not been updated after the rewrite, this will be addressed once the web implementation updates are ready requestAnimationFrame(worklet); } registerEventHandler(_eventHash, _eventHandler) { // noop return ''; } unregisterEventHandler(_) {// noop } enableLayoutAnimations() { console.warn('[Reanimated] enableLayoutAnimations is not available for WEB yet'); } registerSensor(sensorType, interval, iosReferenceFrame, eventHandler) { if (!(this.getSensorName(sensorType) in window)) { return -1; } const sensor = this.initializeSensor(sensorType, interval); let callback; if (sensorType === _commonTypes.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, interfaceOrientation: 0 }); }; } else { callback = () => { const { x, y, z } = sensor; eventHandler({ x, y, z, interfaceOrientation: 0 }); }; } sensor.addEventListener('reading', callback); sensor.start(); this.sensors.set(this.nextSensorId, sensor); return this.nextSensorId++; } unregisterSensor(id) { const sensor = this.sensors.get(id); if (sensor !== undefined) { sensor.stop(); this.sensors.delete(id); } } subscribeForKeyboardEvents(_) { console.warn('[Reanimated] useAnimatedKeyboard is not available on web yet.'); return -1; } unsubscribeFromKeyboardEvents(_) {// noop } initializeSensor(sensorType, interval) { const config = interval <= 0 ? { referenceFrame: 'device' } : { frequency: 1000 / interval }; switch (sensorType) { case _commonTypes.SensorType.ACCELEROMETER: return new window.Accelerometer(config); case _commonTypes.SensorType.GYROSCOPE: return new window.Gyroscope(config); case _commonTypes.SensorType.GRAVITY: return new window.GravitySensor(config); case _commonTypes.SensorType.MAGNETIC_FIELD: return new window.Magnetometer(config); case _commonTypes.SensorType.ROTATION: return new window.AbsoluteOrientationSensor(config); } } getSensorName(sensorType) { switch (sensorType) { case _commonTypes.SensorType.ACCELEROMETER: return 'Accelerometer'; case _commonTypes.SensorType.GRAVITY: return 'GravitySensor'; case _commonTypes.SensorType.GYROSCOPE: return 'Gyroscope'; case _commonTypes.SensorType.MAGNETIC_FIELD: return 'Magnetometer'; case _commonTypes.SensorType.ROTATION: return 'AbsoluteOrientationSensor'; } } } exports.default = JSReanimated; //# sourceMappingURL=JSReanimated.js.map