matterjs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMatter.js Skill
Matter.js 技能
Workflow
工作流程
- Confirm environment (plain HTML, React, or canvas-only) and rendering approach (Matter.Render for debug vs custom renderer).
- Provide a minimal Engine/World/Render/Runner setup and add bodies.
- Add interactions (mouse constraint) or constraints only if requested.
- Share cleanup steps for SPA or teardown scenarios.
- 确认运行环境(纯HTML、React或仅画布)以及渲染方式(使用Matter.Render进行调试或自定义渲染器)。
- 提供最小化的Engine/World/Render/Runner配置并添加物体。
- 仅在需求明确时添加交互(鼠标约束)或其他约束。
- 分享SPA场景下的清理步骤或销毁流程。
Minimal setup
最小化配置
html
<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>html
<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 to add bodies to the world.
Composite.add(engine.world, [...]) - Use to create a canvas automatically, or pass a
Render.create({ element, engine })you create yourself.canvas - Set for solid rendering.
render.options.wireframes = false - Use for a simple loop, or call
Runner.run(runner, engine)in your own loop if you need custom timing.Engine.update
- 使用 向物理世界添加物体。
Composite.add(engine.world, [...]) - 使用 自动创建画布,或传入你自行创建的
Render.create({ element, engine })元素。canvas - 设置 以启用实体渲染。
render.options.wireframes = false - 使用 实现简单循环,若需要自定义时序控制,可在自己的循环中调用
Runner.run(runner, engine)。Engine.update
Mouse interaction (optional)
鼠标交互(可选)
js
const { Mouse, MouseConstraint } = Matter;
const mouse = Mouse.create(render.canvas);
const mouseConstraint = MouseConstraint.create(engine, { mouse });
Composite.add(engine.world, mouseConstraint);
render.mouse = mouse;js
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)
SPA场景清理清单
- Stop the runner with .
Runner.stop(runner) - Remove the render canvas from the DOM.
- Clear engine and world if needed.
- 调用 停止运行器。
Runner.stop(runner) - 从DOM中移除渲染画布。
- 必要时清理engine和world实例。
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?
- 画布应使用何种视口尺寸和缩放比例?
- 我们将使用Matter.Render还是自定义渲染器?
- 是否需要鼠标/触摸拖拽交互?
- 当场景处于屏幕外时,是否需要暂停模拟循环?