optimazed version

This commit is contained in:
2026-04-29 19:27:19 +09:00
parent b13342f47e
commit 708fa4e9ab

View File

@@ -10,12 +10,12 @@ export const cameraInput = {
let video; let video;
let faceDetector; let faceDetector;
let smoothedX = 0.5;
// 🔥 TWEAK THESE FOR FEEL: // 🔥 NARROW CENTER ZONE:
const SMOOTHING_FACTOR = 0.6; // Higher = more responsive/less lag (try 0.5 - 0.8) // 0.48 and 0.52 means the center is only 4% of the screen width.
const LEFT_THRESHOLD = 0.45; // Closer to 0.5 = smaller center zone // Adjust these if it's still too hard to trigger movement.
const RIGHT_THRESHOLD = 0.55; // Closer to 0.5 = smaller center zone const LEFT_THRESHOLD = 0.48;
const RIGHT_THRESHOLD = 0.52;
export async function initCameraControl() { export async function initCameraControl() {
if (cameraInput.active) return video; if (cameraInput.active) return video;
@@ -26,7 +26,11 @@ export async function initCameraControl() {
video.muted = true; video.muted = true;
const stream = await navigator.mediaDevices.getUserMedia({ const stream = await navigator.mediaDevices.getUserMedia({
video: { width: 160, height: 120, frameRate: 15 } video: {
width: 160,
height: 120,
frameRate: { ideal: 20 } // Slightly higher for faster reaction
}
}); });
video.srcObject = stream; video.srcObject = stream;
@@ -57,23 +61,20 @@ function startDetectionLoop() {
if (result.detections.length > 0) { if (result.detections.length > 0) {
const box = result.detections[0].boundingBox; const box = result.detections[0].boundingBox;
const centerX = box.originX + box.width / 2; // Calculate raw center point (0 to 1)
const normalizedX = centerX / 160; const rawX = (box.originX + box.width / 2) / 160;
// Snappier smoothing cameraInput.x = rawX;
smoothedX += (normalizedX - smoothedX) * SMOOTHING_FACTOR;
cameraInput.x = smoothedX;
// Updated Logic with smaller center zone // INSTANT LOGIC (No smoothing)
if (smoothedX < LEFT_THRESHOLD) { if (rawX < LEFT_THRESHOLD) {
updateZones(false, true, 'right'); // Mirrored updateZones(false, true, 'right'); // Mirrored: Face on left of cam = move right
} else if (smoothedX > RIGHT_THRESHOLD) { } else if (rawX > RIGHT_THRESHOLD) {
updateZones(true, false, 'left'); // Mirrored updateZones(true, false, 'left'); // Mirrored: Face on right of cam = move left
} else { } else {
updateZones(false, false, 'center'); updateZones(false, false, 'center');
} }
} else { } else {
// Snap to center if face is lost
updateZones(false, false, 'center'); updateZones(false, false, 'center');
} }