React 游戏循环 Hooks
v1.0.0使用 TypeScript 为游戏循环和动画创建自定义 React hooks。当用户请求 React hook、requestAnimationFrame 集成、游戏循环时使用。
0· 0·0 当前·0 累计
安全扫描
OpenClaw
安全
high confidence该技能的代码、指令和元数据与其声明的目的(一个仅用于浏览器的 React hook 游戏循环)一致,未请求额外凭据、安装任意二进制文件或执行可疑操作。
评估建议
This 技能 应用ears to be what it clAIms: a small, browser-only React hook for game loops. Before 安装ing, review the hook to ensure it matches your 应用's type/encoding (TypeScript/React versions) and confirm you only copy the files into projects that 运行 in the browser (not SSR/Node). The hook catches callback errors and 清理s up RAF on unmount, but as with any third-party code, inspect it for fit and license compatibility (MIT) before use.详细分析 ▾
✓ 用途与能力
The name/description (React game loop hooks) matches the included files and declared behavior. The files implement a 请求AnimationFrame-based useGameLoop hook and an example 组件 — nothing unrelated is 请求ed.
✓ 指令范围
技能.md instructs the 代理/user to copy the hook file into their project and documents API, patterns, and an example. Instructions are narrowly scoped to 安装ing and using the hook; they do not instruct reading unrelated files, 访问ing 环境 variables, or contacting external 端点s.
✓ 安装机制
There is no 安装 spec and no 下载/安装-from-URL behavior. The code is provided in the 技能 as设置s and the user is instructed to copy the file into their project — low-risk and transparent.
✓ 凭证需求
The 技能 请求s no 环境 variables, 凭证s, or 系统 config paths. Declared dependencies (React/TypeScript/react-dom) are 应用ropriate and expected for a TypeScript React hook.
✓ 持久化与权限
The 技能 does not 请求 always:true and uses default user-invocable/autonomous 设置tings. It does not modify other 技能s or 系统-wide 设置tings and does not persist 凭证s.
安全有层次,运行前请审查代码。
运行时依赖
无特殊依赖
安装命令
点击复制官方npx clawhub@latest install react-game-loop
镜像加速npx clawhub@latest install react-game-loop --registry https://cn.longxiaskill.com镜像同步中
技能文档
React Game Loop Hooks 生产级自定义 React hooks,基于 requestAnimationFrame 实现游戏循环与动画。
安装 将 hook 文件复制到项目:
# 在 skill 目录下
cp /assets/hooks/useGameLoop.ts src/hooks/
为你的 OpenClaw skill 安装目录。 依赖 React ≥ 16.8(支持 hooks) TypeScript ≥ 4.0(类型安全) react-dom ≥ 16.8(浏览器环境 requestAnimationFrame 类型) 仅限浏览器使用 —— 依赖 requestAnimationFrame,Node.js 或 SSR 环境不可用。
API 参考 useGameLoop(options)
import { useGameLoop } from './hooks/useGameLoop';
参数:
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| onFrame | (deltaTime: number, elapsedTime: number) => void | — | 每帧调用,传入增量(ms)与累计时间(ms)。必填。 |
| maxDeltaTime | number | 100 | 最大增量截断(ms),防止切页或卡顿后跳跃。 |
| 字段 | 类型 | 说明 |
|---|---|---|
| start | () => void | 启动循环 |
| stop | () => void | 停止循环 |
| isRunning | boolean | 当前运行状态 |
const cbRef = useRef(onFrame);
useEffect(() => { cbRef.current = onFrame; }, [onFrame]);
卸载清理 useEffect 必须返回清理函数。RAF/定时器句柄存 ref,卸载时取消。
useEffect(() => {
const id = requestAnimationFrame(tick);
return () => cancelAnimationFrame(id);
}, []);
错误容错 用户回调用 try/catch 包裹,避免错误破坏内部循环状态。
try { callbackRef.current(delta, elapsed); }
catch (err) { console.error('hook callback error:', err); }
增量截断 游戏循环中截断 deltaTime,避免切页或卡顿后跳跃。
const delta = Math.min(rawDelta, maxDeltaTime);
示例
完整用法见 assets/examples/GameTimer.tsx,展示累计时间与 FPS 显示。
参考
assets/hooks/useGameLoop.ts — 基于 requestAnimationFrame 的游戏循环,支持 start/stop/isRunning 控制、增量截断、错误容错
assets/examples/GameTimer.tsx — 基础用法示例