Matter.js Skill
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.
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?
1---2name: matterjs3description: 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.4---5
6# Matter.js Skill
7
8## Workflow
91. Confirm environment (plain HTML, React, or canvas-only) and rendering approach (Matter.Render for debug vs custom renderer).
102. Provide a minimal Engine/World/Render/Runner setup and add bodies.
113. Add interactions (mouse constraint) or constraints only if requested.
124. Share cleanup steps for SPA or teardown scenarios.
13
14## Minimal setup
15```html
16<script>
17 const { Engine, Render, Runner, Bodies, Composite } = Matter;
18
19 const engine = Engine.create();
20
21 const render = Render.create({
22 element: document.body,
23 engine: engine,
24 options: {
25 width: 800,
26 height: 600,
27 wireframes: false
28 }
29 });
30
31 const runner = Runner.create();
32 Runner.run(runner, engine);
33 Render.run(render);
34
35 const ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });
36 const box = Bodies.rectangle(400, 200, 80, 80);
37
38 Composite.add(engine.world, [ground, box]);
39</script>
40```
41
42## Common patterns
43- Use `Composite.add(engine.world, [...])` to add bodies to the world.
44- Use `Render.create({ element, engine })` to create a canvas automatically, or pass a `canvas` you create yourself.
45- Set `render.options.wireframes = false` for solid rendering.
46- Use `Runner.run(runner, engine)` for a simple loop, or call `Engine.update` in your own loop if you need custom timing.
47
48## Mouse interaction (optional)
49```js
50const { Mouse, MouseConstraint } = Matter;
51const mouse = Mouse.create(render.canvas);
52const mouseConstraint = MouseConstraint.create(engine, { mouse });
53Composite.add(engine.world, mouseConstraint);
54render.mouse = mouse;
55```
56
57## Cleanup checklist (SPA)
58- Stop the runner with `Runner.stop(runner)`.
59- Remove the render canvas from the DOM.
60- Clear engine and world if needed.
61
62## Questions to ask when specs are missing
63- What viewport size and scaling should the canvas use?
64- Are we using Matter.Render or a custom renderer?
65- Do you want mouse/touch drag interaction?
66- Should the simulation loop be paused when offscreen?