-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
JSReanimated.ts
152 lines (135 loc) · 4.18 KB
/
JSReanimated.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { NativeReanimated } from '../NativeReanimated/NativeReanimated';
import {
SensorType,
ShareableRef,
Value3D,
ValueRotation,
} from '../commonTypes';
import { isJest } from '../PlatformChecker';
import { WebSensor } from './WebSensor';
export default class JSReanimated extends NativeReanimated {
nextSensorId = 0;
sensors = new Map<number, WebSensor>();
constructor() {
super(false);
if (isJest()) {
// on node < 16 jest have problems mocking performance.now method which
// results in the tests failing, since date precision isn't that important
// for tests, we use Date.now instead
this.getTimestamp = () => Date.now();
} else {
this.getTimestamp = () => window.performance.now();
}
}
makeShareableClone<T>(value: T): ShareableRef<T> {
return { __hostObjectShareableJSRef: value };
}
installCoreFunctions(
_callGuard: <T extends Array<any>, U>(
fn: (...args: T) => U,
...args: T
) => void,
_valueUnpacker: <T>(value: T) => T
): void {
// noop
}
scheduleOnUI<T>(worklet: ShareableRef<T>) {
// @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<T>(
_eventHash: string,
_eventHandler: ShareableRef<T>
): string {
// noop
return '';
}
unregisterEventHandler(_: string): void {
// noop
}
enableLayoutAnimations() {
console.warn(
'[Reanimated] enableLayoutAnimations is not available for WEB yet'
);
}
registerSensor(
sensorType: SensorType,
interval: number,
eventHandler: (data: Value3D | ValueRotation) => void
): number {
if (!(this.getSensorName(sensorType) in window)) {
return -1;
}
const sensor: WebSensor = this.initializeSensor(sensorType, interval);
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 });
}
});
sensor.start();
this.sensors.set(this.nextSensorId, sensor);
return this.nextSensorId++;
}
unregisterSensor(id: number): void {
const sensor: WebSensor | undefined = this.sensors.get(id);
if (sensor !== undefined) {
sensor.stop();
this.sensors.delete(id);
}
}
subscribeForKeyboardEvents(_: ShareableRef<number>): number {
console.warn(
'[Reanimated] useAnimatedKeyboard is not available on web yet.'
);
return -1;
}
unsubscribeFromKeyboardEvents(_: number): void {
// noop
}
initializeSensor(sensorType: SensorType, interval: number): WebSensor {
const config =
interval === -1
? { referenceFrame: 'device' }
: { frequency: 1 / interval };
switch (sensorType) {
case SensorType.ACCELEROMETER:
return new window.Accelerometer(config);
case SensorType.GYROSCOPE:
return new window.Gyroscope(config);
case SensorType.GRAVITY:
return new window.GravitySensor(config);
case SensorType.MAGNETIC_FIELD:
return new window.Magnetometer(config);
case SensorType.ROTATION:
return new window.AbsoluteOrientationSensor(config);
}
}
getSensorName(sensorType: SensorType): string {
switch (sensorType) {
case SensorType.ACCELEROMETER:
return 'Accelerometer';
case SensorType.GRAVITY:
return 'GravitySensor';
case SensorType.GYROSCOPE:
return 'Gyroscope';
case SensorType.MAGNETIC_FIELD:
return 'Magnetometer';
case SensorType.ROTATION:
return 'AbsoluteOrientationSensor';
}
}
}