This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
99 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 |
---|---|---|
@@ -1,49 +1,49 @@ | ||
import { useEffect, useState, useCallback } from "react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export interface Orientation { | ||
alpha: number; | ||
beta: number; | ||
gamma: number; | ||
alpha: number; | ||
beta: number; | ||
gamma: number; | ||
} | ||
|
||
export const useOrientation = () => { | ||
const [initialOrientation, setInitialOrientation] = | ||
useState<Orientation | null>(null); | ||
const [orientationDiff, setOrientationDiff] = useState<Orientation>({ | ||
alpha: 0, | ||
beta: 0, | ||
gamma: 0, | ||
}); | ||
const [initialOrientation, setInitialOrientation] = | ||
useState<Orientation | null>(null); | ||
const [orientationDiff, setOrientationDiff] = useState<Orientation>({ | ||
alpha: 0, | ||
beta: 0, | ||
gamma: 0, | ||
}); | ||
|
||
const handleOrientation = useCallback( | ||
(event: DeviceOrientationEvent) => { | ||
if (!initialOrientation) { | ||
setInitialOrientation({ | ||
alpha: event.alpha || 0, | ||
beta: event.beta || 0, | ||
gamma: event.gamma || 0, | ||
}); | ||
} else { | ||
setOrientationDiff({ | ||
alpha: (event.alpha || 0) - initialOrientation.alpha, | ||
beta: (event.beta || 0) - initialOrientation.beta, | ||
gamma: (event.gamma || 0) - initialOrientation.gamma, | ||
}); | ||
} | ||
}, | ||
[initialOrientation] | ||
); | ||
const handleOrientation = useCallback( | ||
(event: DeviceOrientationEvent) => { | ||
if (!initialOrientation) { | ||
setInitialOrientation({ | ||
alpha: event.alpha || 0, | ||
beta: event.beta || 0, | ||
gamma: event.gamma || 0, | ||
}); | ||
} else { | ||
setOrientationDiff({ | ||
alpha: (event.alpha || 0) - initialOrientation.alpha, | ||
beta: (event.beta || 0) - initialOrientation.beta, | ||
gamma: (event.gamma || 0) - initialOrientation.gamma, | ||
}); | ||
} | ||
}, | ||
[initialOrientation], | ||
); | ||
|
||
const reset = useCallback(() => { | ||
setInitialOrientation(null); | ||
}, []); | ||
const reset = useCallback(() => { | ||
setInitialOrientation(null); | ||
}, []); | ||
|
||
useEffect(() => { | ||
window.addEventListener("deviceorientation", handleOrientation); | ||
return () => { | ||
window.removeEventListener("deviceorientation", handleOrientation); | ||
}; | ||
}, [handleOrientation]); | ||
useEffect(() => { | ||
window.addEventListener("deviceorientation", handleOrientation); | ||
return () => { | ||
window.removeEventListener("deviceorientation", handleOrientation); | ||
}; | ||
}, [handleOrientation]); | ||
|
||
return { orientationDiff, reset }; | ||
return { orientationDiff, reset }; | ||
}; |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import { create } from "zustand"; | ||
import type { MutableRefObject } from "react"; | ||
import { create } from "zustand"; | ||
|
||
type State = { | ||
socketRef: MutableRefObject<WebSocket | undefined> | null; | ||
socketRef: MutableRefObject<WebSocket | undefined> | null; | ||
}; | ||
|
||
type Action = { | ||
setRef: (ref: MutableRefObject<WebSocket | undefined>) => void; | ||
setRef: (ref: MutableRefObject<WebSocket | undefined>) => void; | ||
}; | ||
|
||
export const useSocketRefStore = create<State & Action>()((set) => ({ | ||
socketRef: null, | ||
setRef: (ref) => set(() => ({ socketRef: ref })), | ||
socketRef: null, | ||
setRef: (ref) => set(() => ({ socketRef: ref })), | ||
})); |
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
export interface Schema { | ||
id: string; | ||
interval: number; | ||
angle: angle; | ||
acceleration: acceleration; | ||
distance: distance; | ||
message_type: message_type; | ||
id: string; | ||
interval: number; | ||
angle: angle; | ||
acceleration: acceleration; | ||
distance: distance; | ||
message_type: message_type; | ||
} | ||
|
||
interface angle { | ||
x: number; | ||
y: number; | ||
x: number; | ||
y: number; | ||
} | ||
|
||
interface acceleration { | ||
x: number; | ||
y: number; | ||
z: number; | ||
x: number; | ||
y: number; | ||
z: number; | ||
} | ||
|
||
interface distance { | ||
x: number; | ||
y: number; | ||
z: number; | ||
x: number; | ||
y: number; | ||
z: number; | ||
} | ||
|
||
export enum message_type { | ||
pointer = "pointer", | ||
action = "action", | ||
pointer = "pointer", | ||
action = "action", | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export const generateUUID = (): string => { | ||
const timestamp = Date.now().toString(16); | ||
const randomHex = Math.floor(Math.random() * 0xffffff).toString(16); | ||
return `${timestamp}-${randomHex}`; | ||
const timestamp = Date.now().toString(16); | ||
const randomHex = Math.floor(Math.random() * 0xffffff).toString(16); | ||
return `${timestamp}-${randomHex}`; | ||
}; |