Examples of how to render a Matter.js engine

Renderer example

A basic example for rendering the bodies in a world as wireframes, given a previously created Matter.Engine (see Getting started):

  1. var canvas = document.createElement('canvas'),
  2. context = canvas.getContext('2d');
  3.  
  4. canvas.width = 800;
  5. canvas.height = 600;
  6.  
  7. document.body.appendChild(canvas);
  8.  
  9. (function render() {
  10. var bodies = Composite.allBodies(engine.world);
  11.  
  12. window.requestAnimationFrame(render);
  13.  
  14. context.fillStyle = '#fff';
  15. context.fillRect(0, 0, canvas.width, canvas.height);
  16.  
  17. context.beginPath();
  18.  
  19. for (var i = 0; i < bodies.length; i += 1) {
  20. var vertices = bodies[i].vertices;
  21.  
  22. context.moveTo(vertices[0].x, vertices[0].y);
  23.  
  24. for (var j = 1; j < vertices.length; j += 1) {
  25. context.lineTo(vertices[j].x, vertices[j].y);
  26. }
  27.  
  28. context.lineTo(vertices[0].x, vertices[0].y);
  29. }
  30.  
  31. context.lineWidth = 1;
  32. context.strokeStyle = '#999';
  33. context.stroke();
  34. })();

A good place to start with your own rendering is to take a look at the source of Matter.Render (you can also simply copy it and customise as you require).

Using Matter.Render

There is an included debug renderer called Matter.Render.This module is an optional canvas based renderer for visualising instances of Matter.Engine. It is mostly intended for development and debugging purposes, but may also be suitable starting point for simple games. It includes a number of drawing options including wireframe, vector with support for sprites and viewports.

By default Matter.Render will only show bodies as wireframes (outlines). This is useful for testing and debugging, but to enable full solid rendering (and sprites if you are using them) you must set render.options.wireframes = false.

Documentation

See the documentation for Matter.Render.

Usage

  1. var engine = Engine.create();
  2.  
  3. // ... add some bodies to the world
  4.  
  5. var render = Render.create({
  6. element: document.body,
  7. engine: engine,
  8. options: options
  9. });
  10.  
  11. Engine.run(engine);
  12. Render.run(render);

Where:

  • element is a container element to insert the canvas into
  • engine is a Matter.Engine instance
  • options is an object specifying render settings, see below (optional) You can also just pass a canvas instead of a container element if you wish to create the canvas yourself.

Options

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

  1. var render = Render.create({
  2. element: document.body,
  3. engine: engine,
  4. options: {
  5. width: 800,
  6. height: 600,
  7. pixelRatio: 1,
  8. background: '#fafafa',
  9. wireframeBackground: '#222',
  10. hasBounds: false,
  11. enabled: true,
  12. wireframes: true,
  13. showSleeping: true,
  14. showDebug: false,
  15. showBroadphase: false,
  16. showBounds: false,
  17. showVelocity: false,
  18. showCollisions: false,
  19. showSeparations: false,
  20. showAxes: false,
  21. showPositions: false,
  22. showAngleIndicator: false,
  23. showIds: false,
  24. showShadows: false,
  25. showVertexNumbers: false,
  26. showConvexHulls: false,
  27. showInternalEdges: false,
  28. showMousePosition: false
  29. }
  30. });