跳到正文
Skillpedia
网站与界面(前端)7.2k · #13449

matterjs

它能做什么

这个技能帮助智能体设置 Matter.js,这是一个用于浏览器的二维物理库,并附带可运行的入门场景。适合希望在页面上实现下落物体、拖动互动或其他物理效果的网页开发者。

适用场景

  • ✓当你希望网页上的物体通过简单的物理引擎下落、弹跳或相互碰撞时。
  • ✓当你希望访客在画布场景中用鼠标拖动物体时。
  • ✓当你需要在单页应用中正确清理物理场景时。

技能链接

https://github.com/MengTo/Skills/blob/main/agent-skills/web-design/matterjs/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/MengTo/Skills/blob/main/agent-skills/web-design/matterjs/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“你能帮我在网页中加入一个下落方块的物理场景,并让人们可以用鼠标拖动吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会创建引擎、渲染器和运行器,在世界中添加地面和一些形状,如果你需要,还会加入鼠标控制。它还会说明页面切换时如何关闭该场景。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Matter.js Skill

Workflow

  1. Confirm environment (plain HTML, React, or canvas-only) and rendering approach (Matter.Render for debug vs custom renderer).
  2. Provide a minimal Engine/World/Render/Runner setup and add bodies.
  3. Add interactions (mouse constraint) or constraints only if requested.
  4. Share cleanup steps for SPA or teardown scenarios.

Minimal setup

<script>
  const { Engine, Render, Runner, Bodies, Composite } = Matter;

  const engine = Engine.create();

  const render = Render.create({
    element: document.body,
    engine: engine,
    options: {
      width: 800,
      height: 600,
      wireframes: false
    }
  });

  const runner = Runner.create();
  Runner.run(runner, engine);
  Render.run(render);

  const ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });
  const box = Bodies.rectangle(400, 200, 80, 80);

  Composite.add(engine.world, [ground, box]);
</script>

Common patterns

  • Use Composite.add(engine.world, [...]) to add bodies to the world.
  • Use Render.create({ element, engine }) to create a canvas automatically, or pass a canvas you create yourself.
  • Set render.options.wireframes = false for solid rendering.
  • Use Runner.run(runner, engine) for a simple loop, or call Engine.update in your own loop if you need custom timing.

Mouse interaction (optional)

const { Mouse, MouseConstraint } = Matter;
const mouse = Mouse.create(render.canvas);
const mouseConstraint = MouseConstraint.create(engine, { mouse });
Composite.add(engine.world, mouseConstraint);
render.mouse = mouse;

Cleanup checklist (SPA)

  • Stop the runner with Runner.stop(runner).
  • Remove the render canvas from the DOM.
  • Clear engine and world if needed.

Questions to ask when specs are missing

  • What viewport size and scaling should the canvas use?
  • Are we using Matter.Render or a custom renderer?
  • Do you want mouse/touch drag interaction?
  • Should the simulation loop be paused when offscreen?
常见问题

使用它需要懂物理学吗?

不需要。该技能提供一个最小可用的设置,你可以逐步修改。

它能和 React 一起使用吗?

可以。该技能涵盖纯 HTML、React 以及仅使用画布的方式,并会先询问你使用哪一种。

如果我没有要求,它会添加其他内容吗?

不会。鼠标交互和约束只有在你要求时才会添加。

详情

作者的原始描述

Use when implementing 2D physics interactions with Matter.js, including Engine/World setup, Render/Runner configuration, adding bodies and constraints, and scroll/interaction-friendly canvas scenes.

repo: MengTo/Skills

热度

GitHub 星标
7.2k
排名
#13449
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能