Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
feat: WebSocketの諸々
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Kizuku committed Aug 10, 2024
1 parent c5e8fd7 commit aeaf96b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/hooks/useOrientation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useEffect, useState, useCallback } from "react";

export interface Orientation {
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 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);
}, []);

useEffect(() => {
window.addEventListener("deviceorientation", handleOrientation);
return () => {
window.removeEventListener("deviceorientation", handleOrientation);
};
}, [handleOrientation]);

return { orientationDiff, reset };
};
2 changes: 1 addition & 1 deletion src/store/useSocketRefStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { create } from "zustand";
import { MutableRefObject } from "react";
import type { MutableRefObject } from "react";

type State = {
socketRef: MutableRefObject<WebSocket | undefined> | null;
Expand Down
30 changes: 30 additions & 0 deletions src/type/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Schema {
id: string;
interval: number;
angle: angle;
acceleration: acceleration;
distance: distance;
message_type: message_type;
}

interface angle {
x: number;
y: number;
}

interface acceleration {
x: number;
y: number;
z: number;
}

interface distance {
x: number;
y: number;
z: number;
}

export enum message_type {
pointer = "pointer",
action = "action",
}
31 changes: 31 additions & 0 deletions src/utils/parmission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 角度・加速度センサーのパーミッションをリクエストする関数
*/
export const requestPermission = async () => {
// @ts-expect-error only safari propaty
if (typeof DeviceMotionEvent.requestPermission === "function") {
try {
// @ts-expect-error only safari propaty
const permissionState = await DeviceMotionEvent.requestPermission();
if (permissionState !== "granted") {
console.warn("Device Motion permission denied.");
}
} catch (error) {
console.error("Error requesting Device Motion permission:", error);
}
}

// @ts-expect-error only safari propaty
if (typeof DeviceOrientationEvent.requestPermission === "function") {
try {
const permissionState =
// @ts-expect-error only safari propaty
await DeviceOrientationEvent.requestPermission();
if (permissionState !== "granted") {
console.warn("Device Orientation permission denied.");
}
} catch (error) {
console.error("Error requesting Device Orientation permission:", error);
}
}
};

0 comments on commit aeaf96b

Please sign in to comment.