Extensive UI changes
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
This commit is contained in:
parent
6fc0a01c56
commit
ddea7fc512
16 changed files with 675 additions and 493 deletions
|
|
@ -1,19 +1,17 @@
|
|||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useTilt } from "./useTilt";
|
||||
import {
|
||||
createBubbleWorld,
|
||||
screenGravity,
|
||||
DRAW_BUBBLE_RADIUS,
|
||||
} from "../shared/bubble-physics.js";
|
||||
|
||||
let tiltAllowedThisSession = false;
|
||||
type MotionStatus = "off" | "waiting" | "on" | "denied" | "unavailable";
|
||||
export function useBubblePhysics(count: number) {
|
||||
const canvas = useRef<HTMLCanvasElement>(null);
|
||||
const world = useRef<ReturnType<typeof createBubbleWorld> | null>(null);
|
||||
const kick = useRef<() => void>(() => {});
|
||||
const redraw = useRef<() => void>(() => {});
|
||||
const [enabled, setEnabled] = useState(tiltAllowedThisSession);
|
||||
const [motion, setMotion] = useState<MotionStatus>("off");
|
||||
const { enabled } = useTilt();
|
||||
const [reduced, setReduced] = useState(
|
||||
() => matchMedia("(prefers-reduced-motion: reduce)").matches,
|
||||
);
|
||||
|
|
@ -154,8 +152,6 @@ export function useBubblePhysics(count: number) {
|
|||
kick.current();
|
||||
return;
|
||||
}
|
||||
setMotion("waiting");
|
||||
let received = false;
|
||||
let lastBeta = 0;
|
||||
let lastGamma = 0;
|
||||
const apply = () => {
|
||||
|
|
@ -177,57 +173,15 @@ export function useBubblePhysics(count: number) {
|
|||
return;
|
||||
lastBeta = event.beta;
|
||||
lastGamma = event.gamma;
|
||||
if (!received) {
|
||||
received = true;
|
||||
setMotion("on");
|
||||
}
|
||||
apply();
|
||||
};
|
||||
window.addEventListener("deviceorientation", orientation);
|
||||
screen.orientation?.addEventListener("change", apply);
|
||||
const timeout = setTimeout(() => {
|
||||
if (!received) setMotion("unavailable");
|
||||
}, 5000);
|
||||
return () => {
|
||||
clearTimeout(timeout);
|
||||
window.removeEventListener("deviceorientation", orientation);
|
||||
screen.orientation?.removeEventListener("change", apply);
|
||||
};
|
||||
}, [enabled, reduced]);
|
||||
|
||||
const toggleTilt = useCallback(async () => {
|
||||
if (enabled) {
|
||||
setEnabled(false);
|
||||
setMotion("off");
|
||||
tiltAllowedThisSession = false;
|
||||
return;
|
||||
}
|
||||
if (typeof DeviceOrientationEvent === "undefined") {
|
||||
setMotion("unavailable");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const sensor = DeviceOrientationEvent as typeof DeviceOrientationEvent & {
|
||||
requestPermission?: () => Promise<string>;
|
||||
};
|
||||
if (
|
||||
sensor.requestPermission &&
|
||||
(await sensor.requestPermission()) !== "granted"
|
||||
) {
|
||||
setMotion("denied");
|
||||
return;
|
||||
}
|
||||
tiltAllowedThisSession = true;
|
||||
setEnabled(true);
|
||||
} catch {
|
||||
setMotion("denied");
|
||||
}
|
||||
}, [enabled]);
|
||||
const nudge = useCallback(() => {
|
||||
if (!reduced) {
|
||||
world.current?.nudge();
|
||||
kick.current();
|
||||
}
|
||||
}, [reduced]);
|
||||
return { canvas, motion, enabled, reduced, toggleTilt, nudge };
|
||||
return { canvas };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue