-
Notifications
You must be signed in to change notification settings - Fork 36
Add gyroscope controls for camera panning on handheld devices #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
git-chad
wants to merge
5
commits into
main
Choose a base branch
from
tobi/poc-gyroscope-controls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eafad1a
mobile: add gyro controls for camera pan
git-chad 62c0347
fix testhandler refire
git-chad 4aba2bd
fixes
git-chad 80080f1
Merge remote-tracking branch 'origin/main' into tobi/poc-gyroscope-co…
ragojose e1c3108
fix(gyroscope): permission retry, desktop gating, and toggle labels
ragojose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| "use client" | ||
|
|
||
| import { useDeviceOrientation } from "@/hooks/use-device-orientation" | ||
| import { useMedia } from "@/hooks/use-media" | ||
| import { cn } from "@/utils/cn" | ||
|
|
||
| export const GyroscopeToggle = () => { | ||
| const { permission, isEnabled, requestPermission, setIsEnabled } = | ||
| useDeviceOrientation() | ||
|
|
||
| // Desktop browsers expose DeviceOrientationEvent without gyroscope | ||
| // hardware — only offer the toggle on touch-first devices | ||
| const isCoarsePointer = useMedia("(pointer: coarse)") | ||
|
|
||
| if (!isCoarsePointer || permission === "unsupported") return null | ||
|
|
||
| const handleClick = async () => { | ||
| if (permission === "granted") { | ||
| setIsEnabled(!isEnabled) | ||
| } else if (permission === "prompt") { | ||
| await requestPermission() | ||
| } | ||
| } | ||
|
|
||
| const isActive = permission === "granted" && isEnabled | ||
| const isDenied = permission === "denied" | ||
|
|
||
| return ( | ||
| <button | ||
| onClick={handleClick} | ||
| disabled={isDenied} | ||
| className={cn( | ||
| "flex items-center gap-2 rounded-full border px-3 py-1.5 text-xs font-medium transition-colors", | ||
| isActive | ||
| ? "border-brand-g1 bg-brand-g1/10 text-brand-g1" | ||
| : "border-brand-w1/30 text-brand-w2 hover:border-brand-w1/50", | ||
| isDenied && "cursor-not-allowed opacity-50" | ||
| )} | ||
| aria-label={ | ||
| isActive ? "Disable gyroscope camera" : "Enable gyroscope camera" | ||
| } | ||
| > | ||
| <GyroscopeIcon className="h-4 w-4" /> | ||
| <span> | ||
| {isDenied ? "Gyro denied" : isActive ? "Gyro on" : "Gyro off"} | ||
| </span> | ||
| </button> | ||
| ) | ||
| } | ||
|
|
||
| const GyroscopeIcon = ({ className }: { className?: string }) => ( | ||
| <svg | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| width="24" | ||
| height="24" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| className={className} | ||
| > | ||
| <path d="M16.466 7.5C15.643 4.237 13.952 2 12 2 9.239 2 7 6.477 7 12s2.239 10 5 10c.342 0 .677-.069 1-.2" /> | ||
| <path d="m15.194 13.707 3.814 1.86-1.86 3.814" /> | ||
| <path d="M19 15.57c-1.804.885-4.274 1.43-7 1.43-5.523 0-10-2.239-10-5s4.477-5 10-5c4.838 0 8.873 1.718 9.8 4" /> | ||
| </svg> | ||
| ) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import { useCallback, useEffect, useRef } from "react" | ||
|
|
||
| import { useGyroscopeStore } from "@/store/gyroscope-store" | ||
|
|
||
| // iOS Safari exposes requestPermission as a static on the constructor | ||
| type DeviceOrientationEventCtor = typeof DeviceOrientationEvent & { | ||
| requestPermission?: () => Promise<"granted" | "denied" | "default"> | ||
| } | ||
|
|
||
| const SMOOTHING = 0.1 | ||
| const GAMMA_MAX = 30 | ||
| const BETA_RANGE = 25 | ||
| const STORAGE_KEY = "gyroscope-enabled" | ||
|
|
||
| export const useDeviceOrientation = () => { | ||
| const { permission, setPermission, isEnabled, setIsEnabled, setOrientation } = | ||
| useGyroscopeStore() | ||
|
|
||
| const smoothedRef = useRef({ x: 0, y: 0 }) | ||
| const initialBetaRef = useRef<number | null>(null) | ||
| const hasCheckedPermission = useRef(false) | ||
|
|
||
| const handleOrientation = useCallback( | ||
| (event: DeviceOrientationEvent) => { | ||
| const { gamma, beta } = event | ||
|
|
||
| if (gamma === null || beta === null) return | ||
|
|
||
| if (initialBetaRef.current === null) { | ||
| initialBetaRef.current = beta | ||
| } | ||
|
|
||
| const normalizedX = Math.max(-1, Math.min(1, gamma / GAMMA_MAX)) | ||
|
|
||
| const betaOffset = beta - (initialBetaRef.current ?? 45) | ||
| const normalizedY = Math.max(-1, Math.min(1, betaOffset / BETA_RANGE)) | ||
|
|
||
| smoothedRef.current.x += (normalizedX - smoothedRef.current.x) * SMOOTHING | ||
| smoothedRef.current.y += (normalizedY - smoothedRef.current.y) * SMOOTHING | ||
|
|
||
| setOrientation(smoothedRef.current.x, smoothedRef.current.y) | ||
| }, | ||
| [setOrientation] | ||
| ) | ||
|
|
||
| const requestPermission = useCallback(async () => { | ||
| const DeviceOrientationEventTyped = | ||
| DeviceOrientationEvent as DeviceOrientationEventCtor | ||
|
|
||
| if (typeof DeviceOrientationEventTyped.requestPermission === "function") { | ||
| try { | ||
| const result = await DeviceOrientationEventTyped.requestPermission() | ||
| if (result === "granted") { | ||
| setPermission("granted") | ||
| setIsEnabled(true) | ||
| localStorage.setItem(STORAGE_KEY, "true") | ||
| return true | ||
| } else { | ||
| // "default" means the user dismissed the dialog without choosing — | ||
| // keep "prompt" so tapping the toggle can re-request | ||
| setPermission(result === "denied" ? "denied" : "prompt") | ||
| localStorage.removeItem(STORAGE_KEY) | ||
| return false | ||
| } | ||
| } catch { | ||
| setPermission("denied") | ||
| localStorage.removeItem(STORAGE_KEY) | ||
| return false | ||
| } | ||
| } else if ("DeviceOrientationEvent" in window) { | ||
| setPermission("granted") | ||
| setIsEnabled(true) | ||
| localStorage.setItem(STORAGE_KEY, "true") | ||
| return true | ||
| } else { | ||
| setPermission("unsupported") | ||
| return false | ||
| } | ||
| }, [setPermission, setIsEnabled]) | ||
|
|
||
| const resetCalibration = useCallback(() => { | ||
| initialBetaRef.current = null | ||
| smoothedRef.current = { x: 0, y: 0 } | ||
| setOrientation(0, 0) | ||
| }, [setOrientation]) | ||
|
|
||
| useEffect(() => { | ||
| if (hasCheckedPermission.current) return | ||
| hasCheckedPermission.current = true | ||
|
|
||
| if (!("DeviceOrientationEvent" in window)) { | ||
| setPermission("unsupported") | ||
| return | ||
| } | ||
|
|
||
| const wasEnabled = localStorage.getItem(STORAGE_KEY) === "true" | ||
|
|
||
| const DeviceOrientationEventTyped = | ||
| DeviceOrientationEvent as DeviceOrientationEventCtor | ||
|
|
||
| const requiresPermission = | ||
| typeof DeviceOrientationEventTyped.requestPermission === "function" | ||
|
|
||
| if (!requiresPermission) { | ||
| setPermission("granted") | ||
| if (wasEnabled) { | ||
| // Test if actual gyroscope hardware exists by listening for events | ||
| // Desktop browsers have DeviceOrientationEvent but no hardware, so no events fire | ||
| const testHandler = (e: DeviceOrientationEvent) => { | ||
| if (e.gamma !== null) { | ||
| setIsEnabled(true) | ||
| window.removeEventListener("deviceorientation", testHandler, true) | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener("deviceorientation", testHandler, true) | ||
|
|
||
| // If no events fire within 500ms, hardware likely doesn't exist - clean up localStorage | ||
| setTimeout(() => { | ||
| window.removeEventListener("deviceorientation", testHandler, true) | ||
| if (!useGyroscopeStore.getState().isEnabled) { | ||
| localStorage.removeItem(STORAGE_KEY) | ||
| } | ||
| }, 500) | ||
| } | ||
|
git-chad marked this conversation as resolved.
|
||
| } else if (wasEnabled) { | ||
| const testHandler = (e: DeviceOrientationEvent) => { | ||
| if (e.gamma !== null) { | ||
| setPermission("granted") | ||
| setIsEnabled(true) | ||
| window.removeEventListener("deviceorientation", testHandler, true) | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener("deviceorientation", testHandler, true) | ||
|
|
||
| setTimeout(() => { | ||
| window.removeEventListener("deviceorientation", testHandler, true) | ||
| }, 500) | ||
| } | ||
| }, [setPermission, setIsEnabled]) | ||
|
|
||
| useEffect(() => { | ||
| if (permission === "granted") { | ||
| if (isEnabled) { | ||
| localStorage.setItem(STORAGE_KEY, "true") | ||
| } else { | ||
| localStorage.removeItem(STORAGE_KEY) | ||
| } | ||
| } | ||
| }, [isEnabled, permission]) | ||
|
|
||
| useEffect(() => { | ||
| if (!isEnabled || permission !== "granted") return | ||
|
|
||
| window.addEventListener("deviceorientation", handleOrientation, true) | ||
|
|
||
| return () => { | ||
| window.removeEventListener("deviceorientation", handleOrientation, true) | ||
| initialBetaRef.current = null | ||
| smoothedRef.current = { x: 0, y: 0 } | ||
| setOrientation(0, 0) | ||
| } | ||
|
git-chad marked this conversation as resolved.
|
||
| }, [isEnabled, permission, handleOrientation, setOrientation]) | ||
|
|
||
| return { | ||
| permission, | ||
| isEnabled, | ||
| requestPermission, | ||
| setIsEnabled, | ||
| resetCalibration | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { create } from "zustand" | ||
|
|
||
| type GyroscopePermission = "prompt" | "granted" | "denied" | "unsupported" | ||
|
|
||
| interface GyroscopeStore { | ||
| permission: GyroscopePermission | ||
| setPermission: (permission: GyroscopePermission) => void | ||
| isEnabled: boolean | ||
| setIsEnabled: (enabled: boolean) => void | ||
| orientationX: number | ||
| orientationY: number | ||
| setOrientation: (x: number, y: number) => void | ||
| } | ||
|
|
||
| export const useGyroscopeStore = create<GyroscopeStore>((set) => ({ | ||
| permission: "prompt", | ||
| setPermission: (permission) => set({ permission }), | ||
| isEnabled: false, | ||
| setIsEnabled: (enabled) => set({ isEnabled: enabled }), | ||
| orientationX: 0, | ||
| orientationY: 0, | ||
| setOrientation: (x, y) => set({ orientationX: x, orientationY: y }) | ||
| })) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.