-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathuseGazeTracking.js
More file actions
98 lines (78 loc) · 3.05 KB
/
useGazeTracking.js
File metadata and controls
98 lines (78 loc) · 3.05 KB
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
import { useState, useEffect, useCallback } from 'react';
// Grid configuration (must match your generation parameters)
const P_MIN = -15;
const P_MAX = 15;
const STEP = 3;
const SIZE = 256;
/**
* Converts normalized coordinates [-1, 1] to grid coordinates
*/
function quantizeToGrid(val) {
const raw = P_MIN + (val + 1) * (P_MAX - P_MIN) / 2; // [-1,1] -> [-15,15]
const snapped = Math.round(raw / STEP) * STEP;
return Math.max(P_MIN, Math.min(P_MAX, snapped));
}
/**
* Converts grid coordinates to filename format
*/
function gridToFilename(px, py) {
const sanitize = (val) => val.toString().replace('-', 'm').replace('.', 'p');
return `gaze_px${sanitize(px)}_py${sanitize(py)}_${SIZE}.webp`;
}
/**
* Custom hook for gaze tracking
* @param {React.RefObject} containerRef - Reference to the container element
* @param {string} basePath - Base path to face images (default: '/faces/')
* @returns {Object} { currentImage, isLoading, error }
*/
export function useGazeTracking(containerRef, basePath = '/faces/') {
const [currentImage, setCurrentImage] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const updateGaze = useCallback((clientX, clientY) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
// Convert to normalized coordinates [-1, 1]
const nx = (clientX - centerX) / (rect.width / 2);
const ny = (clientY - centerY) / (rect.height / 2);
// Clamp to [-1, 1] range
const clampedX = Math.max(-1, Math.min(1, nx));
const clampedY = Math.max(-1, Math.min(1, ny));
// Convert to grid coordinates
const px = quantizeToGrid(clampedX);
const py = quantizeToGrid(clampedY);
// Generate filename
const filename = gridToFilename(px, py);
const imagePath = `${basePath}${filename}`;
setCurrentImage(imagePath);
}, [basePath]);
const handleMouseMove = useCallback((e) => {
updateGaze(e.clientX, e.clientY);
}, [updateGaze]);
const handleTouchMove = useCallback((e) => {
if (e.touches.length > 0) {
const touch = e.touches[0];
updateGaze(touch.clientX, touch.clientY);
}
}, [updateGaze]);
useEffect(() => {
const container = containerRef.current;
if (!container) return;
// Add event listeners
container.addEventListener('mousemove', handleMouseMove);
container.addEventListener('touchmove', handleTouchMove, { passive: true });
// Set initial center gaze
const rect = container.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
updateGaze(centerX, centerY);
return () => {
container.removeEventListener('mousemove', handleMouseMove);
container.removeEventListener('touchmove', handleTouchMove);
};
}, [handleMouseMove, handleTouchMove, updateGaze]);
return { currentImage, isLoading, error };
}
export default useGazeTracking;