testsAndMisc/bucket_catch/packages/frontend/src/hooks/useBasketControl.ts
Krzysztof kuhy Rudnicki 5a9296d8aa Add bucket_catch: osu!catch browser game with 100% frontend test coverage
Frontend (React 19 + Vite 6 + TypeScript strict):
- DropZone, ModeSelect, GameCanvas, PuzzleCanvas, ScoreScreen, PuzzleResult
- File-drop game with AABB collision; download (JSZip) and upload (NestJS) modes
- Puzzle mode: NxN image slice via OffscreenCanvas; Union-Find spatial clustering
  guarantees 100% catch rate is always achievable regardless of piece speeds
- ESLint typescript-eslint strict-type-checked (zero errors)
- 145 Vitest tests; 100% coverage on statements/branches/functions/lines

Backend (NestJS 11):
- POST /files/upload (multer disk storage) and GET /health

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZ8QTmreFcaqrsvVb38Grd
2026-06-27 12:21:35 +02:00

33 lines
956 B
TypeScript

import { useEffect, useRef } from "react";
const BASKET_HALF_WIDTH = 60;
/**
* Tracks mouse X on the canvas and keeps a ref updated with the clamped basket
* centre X. Uses a ref (not state) to avoid re-renders inside the game loop.
*/
export function useBasketControl(
canvasRef: React.RefObject<HTMLCanvasElement | null>,
): React.RefObject<number> {
const basketXRef = useRef<number>(window.innerWidth / 2);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const onMouseMove = (e: MouseEvent): void => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
basketXRef.current = Math.max(
BASKET_HALF_WIDTH,
Math.min(canvas.width - BASKET_HALF_WIDTH, x),
);
};
canvas.addEventListener("mousemove", onMouseMove);
return () => { canvas.removeEventListener("mousemove", onMouseMove); };
}, [canvasRef]);
return basketXRef;
}