Examples of how to run a Matter.js engine

Runner example

Browser

A basic example for running a previously created Matter.Engine (see Getting started):

  1. (function run() {
  2. window.requestAnimationFrame(run);
  3. Engine.update(engine, 1000 / 60);
  4. })();

See also the source of Matter.Runner that provides some more advanced features.

Node

When using node you can use setInterval instead of window.requestAnimationFrame:

  1. setInterval(function() {
  2. Engine.update(engine, 1000 / 60);
  3. }, 1000 / 60);

Using Matter.Runner

There is an included runner called Matter.Runner.This module is an optional utility which provides a game loop, that handles continuously updating a Matter.Engine for you. It is intended for development and debugging purposes, but may also be suitable for simple games.

Documentation

See the documentation for Matter.Runner.

Usage

The simplest way is to use the Engine.run helper:

  1. var engine = Engine.create();
  2. Engine.run(engine);

Alternatively you can create a runner directly:

  1. var runner = Runner.create();
  2. Runner.run(runner, engine);

Options

A number of options may be passed to Matter.Runner.create:

  1. var runner = Runner.create({
  2. delta: 1000 / 60,
  3. isFixed: false,
  4. enabled: true
  5. });